Skip to content

Conversation

@tungpun
Copy link

@tungpun tungpun commented Nov 10, 2025

Relates to

Risks

Low - This change removes a duplicate message emit that was causing race conditions. The message is still properly emitted through serverInstance.createMessage(), so there is no risk of losing message processing functionality. The change only affects the /central-channels/:channelId/messages endpoint in the channels router.

Background

What does this PR do?

This PR removes a duplicate internalMessageBus.emit('new_message', messageForBus) call from the /central-channels/:channelId/messages endpoint in packages/server/src/api/messaging/channels.ts.

The message is already emitted as a root message when serverInstance.createMessage(newRootMessageData) is called (see packages/server/src/index.ts lines 1702-1723), which automatically publishes the message to the internal message bus for agent consumption.

What kind of change is this?

Bug fixes (non-breaking change which fixes an issue)

Why are we doing this? Any context or related work?

When a message was created through the /central-channels/:channelId/messages endpoint, it was being emitted to the internal message bus twice:

  1. Once in serverInstance.createMessage() (which is the correct place)
  2. Once again explicitly in the channels router endpoint

This double emission caused a race condition where the message processing race check in DefaultMessageService.processMessage() (see packages/core/src/services/default-message-service.ts lines 361-374) would fail, resulting in the error: "Response discarded - newer message being processed for agent".

By removing the duplicate emit, messages are now only emitted once through the proper channel (serverInstance.createMessage()), eliminating the race condition and ensuring messages are processed correctly.

Documentation changes needed?

My changes do not require a change to the project documentation.

Testing

Where should a reviewer start?

Review the change in packages/server/src/api/messaging/channels.ts around line 245, where the duplicate internalMessageBus.emit() call was removed. Verify that serverInstance.createMessage() in packages/server/src/index.ts still properly emits messages to the internal message bus.

Detailed testing steps

  1. Verify message emission still works:

    • Send a message through the /central-channels/:channelId/messages endpoint
    • Verify that the message is properly emitted to the internal message bus (check logs for [AgentServer] Published message {id} to internal message bus)
    • Verify that agents can still process messages from the GUI
  2. Verify race condition is fixed:

    • Send multiple messages in quick succession through the GUI
    • Verify that messages are processed correctly without the "Response discarded - newer message being processed for agent" error
    • Check that agent responses are generated properly
  3. Verify no duplicate processing:

    • Monitor the internal message bus logs
    • Confirm that each message is only emitted once (not twice)
    • Verify that agents receive each message exactly once

None: Automated tests are acceptable.

@graphite-app
Copy link

graphite-app bot commented Nov 10, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • merge-queue - adds this PR to the back of the merge queue
  • merge-queue-hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 10, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@odilitime
Copy link
Collaborator

shouldn't we prefer the event since it's extensible?

@tungpun tungpun changed the title chore: rm the message emit in src api to avoid race condition fix: rm the message emit in src api to avoid race condition Nov 11, 2025
tungpun added a commit to KrystalDeFi/eliza that referenced this pull request Nov 13, 2025


fix: rm the message emit in src api to avoid race condition elizaOS#6137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants