Skip to content

"onReply" not getting called race condition #6

@mm-gmbd

Description

@mm-gmbd

Per-message onReply handler not getting called due to race condition because private method _onAckReceived is deleting message from sentQueue before _onReplyReceived has a chance to process.

The "ack" is always sent back, and with the current implementation it will always delete the message from the sentQueue before the _onReplyReceived handler is called.

The logs I get from the following added server.logs are as follows:

  1. "1 Deleting message id 2 from the sent queue"
  2. "Received reply for message id 2"
  3. "_sentQueue contents: {}"

// A handler for message acknowledgement
    //
    // Parameters:          Acknowledgement message containing original message id
    //
    // Returns:             Nothing
    function _onAckReceived(payload) {
        local id = payload["id"]
        if (id in _sentQueue) {
            local msg = _sentQueue[id]

            _isFunc(msg._onAck) && msg._onAck(msg)
            _isFunc(_onAck) && _onAck(msg)

            // Delete the acked message from the queue
            server.log("1 Deleting message id "+id+" from the sent queue")
            delete _sentQueue[id]
        }
    }

function _onReplyReceived(payload) {
        server.log("Received reply for message id "+payload["id"]);
        local id = payload["id"]
        server.log("_sentQueue contents: "+JSONEncoder.encode(_sentQueue))
        if (id in _sentQueue) {
            server.log("It's in the sent queue!")
            local msg = _sentQueue[id]
            server.log("And its name is: "+msg.payload.name);

            // Make sure to call acknowledgement handlers first
            _isFunc(msg._onAck) && msg._onAck(msg)
            _isFunc(_onAck) && _onAck(msg)

            // Then call the global handlers
            server.log("Calling the onReply handlers!")
            _isFunc(msg._onReply) && msg._onReply(msg, payload["data"])
            _isFunc(_onReply) && _onReply(msg, payload["data"])

            server.log("2 Deleting message id "+id+" from the sent queue")
            delete _sentQueue[id]
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions