# Buggy code
def _assign_uwsgi_mule_message_handler(
    self, obj, method, configured, message_callback=None, flush=True, **kwargs
):
    """Assign object to a handler by sending a setup message to the appropriate handler pool (farm), where a handler
    (mule) will receive the message and assign itself.

    :param obj:             Same as :method:`ConfiguresHandlers.assign_handler()`.
    :param method:          Same as :method:`ConfiguresHandlers._assign_db_preassign_handler()`.
    :param configured:      Same as :method:`ConfiguresHandlers.assign_handler()`.
    :param queue_callback:  Callback returning a setup message to be sent via the stack messaging interface's
                            ``send_message()`` method. No arguments are passed.
    :type queue_callback:   callable

    :raises HandlerAssignmentSkip: if the configured or default handler is not a known handler pool (farm)
    :returns: str -- The assigned handler pool.
    """
    assert message_callback is not None, (
        "Cannot perform '%s' handler assignment: `message_callback` is None"
        % HANDLER_ASSIGNMENT_METHODS.UWSGI_MULE_MESSAGE
    )
    tag = configured or self.DEFAULT_HANDLER_TAG
    pool = self.pool_for_tag.get(tag)
    if pool is None:
        log.debug(
            "(%s) No handler pool (uWSGI farm) for '%s' found", obj.log_str(), tag
        )
        raise HandlerAssignmentSkip()
    else:
        if flush:
            _timed_flush_obj(obj)
        message = message_callback()
        self.app.application_stack.send_message(pool, message)
    return pool


# Traceback (AttributeError)
galaxy.web_stack.transport DEBUG 2021-01-15 16:38:33,341 [p:29115,w:0,m:1] [UWSGIFarmMessageTransport.dispatcher_thread] Released lock
galaxy.web_stack.transport DEBUG 2021-01-15 16:38:33,341 [p:29117,w:0,m:3] [UWSGIFarmMessageTransport.dispatcher_thread] Acquired message lock, waiting for new message
galaxy.web_stack.transport DEBUG 2021-01-15 16:38:33,342 [p:29117,w:0,m:3] [UWSGIFarmMessageTransport.dispatcher_thread] Received message: {"target": "job_handler", "params": {"task": "setup", "job_id": null}, "__classname__": "JobHandlerMessage"}
galaxy.web_stack.transport ERROR 2021-01-15 16:38:33,349 [p:29117,w:0,m:3] [UWSGIFarmMessageTransport.dispatcher_thread] Exception in mule message handling
Traceback (most recent call last):
File "lib/galaxy/web_stack/transport.py", line 119, in _dispatch_messages
self.dispatcher.dispatch(msg)
File "lib/galaxy/web_stack/message.py", line 46, in dispatch
self.__funcs[msg.target](msg)
File "lib/galaxy/web_stack/message.py", line 147, in default_handler
getattr(self, '_handle_%s_msg' % msg.task)(**msg.params)
File "lib/galaxy/jobs/handler.py", line 823, in _handle_setup_msg
if job.handler is None:
AttributeError: 'NoneType' object has no attribute 'handler'

# Fixed code
def _assign_uwsgi_mule_message_handler(
    self, obj, method, configured, message_callback=None, flush=True, **kwargs
):
    """Assign object to a handler by sending a setup message to the appropriate handler pool (farm), where a handler
    (mule) will receive the message and assign itself.

    :param obj:             Same as :method:`ConfiguresHandlers.assign_handler()`.
    :param method:          Same as :method:`ConfiguresHandlers._assign_db_preassign_handler()`.
    :param configured:      Same as :method:`ConfiguresHandlers.assign_handler()`.
    :param queue_callback:  Callback returning a setup message to be sent via the stack messaging interface's
                            ``send_message()`` method. No arguments are passed.
    :type queue_callback:   callable

    :raises HandlerAssignmentSkip: if the configured or default handler is not a known handler pool (farm)
    :returns: str -- The assigned handler pool.
    """
    assert message_callback is not None, (
        "Cannot perform '%s' handler assignment: `message_callback` is None"
        % HANDLER_ASSIGNMENT_METHODS.UWSGI_MULE_MESSAGE
    )
    tag = configured or self.DEFAULT_HANDLER_TAG
    pool = self.pool_for_tag.get(tag)
    if pool is None:
        log.debug(
            "(%s) No handler pool (uWSGI farm) for '%s' found", obj.log_str(), tag
        )
        raise HandlerAssignmentSkip()
    else:
        if flush or not obj.id:
            _timed_flush_obj(obj)
        message = message_callback()
        self.app.application_stack.send_message(pool, message)
    return pool