fix: rm the message emit in src api to avoid race condition #6137
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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/messagesendpoint 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/messagesendpoint inpackages/server/src/api/messaging/channels.ts.The message is already emitted as a root message when
serverInstance.createMessage(newRootMessageData)is called (seepackages/server/src/index.tslines 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/messagesendpoint, it was being emitted to the internal message bus twice:serverInstance.createMessage()(which is the correct place)This double emission caused a race condition where the message processing race check in
DefaultMessageService.processMessage()(seepackages/core/src/services/default-message-service.tslines 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.tsaround line 245, where the duplicateinternalMessageBus.emit()call was removed. Verify thatserverInstance.createMessage()inpackages/server/src/index.tsstill properly emits messages to the internal message bus.Detailed testing steps
Verify message emission still works:
/central-channels/:channelId/messagesendpoint[AgentServer] Published message {id} to internal message bus)Verify race condition is fixed:
Verify no duplicate processing:
None: Automated tests are acceptable.