Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ public EventKind onMessageSend(MessageSendParams params) throws JSONRPCError {
ResultAggregator.EventTypeAndInterrupt etai = null;
try {
EventConsumer consumer = new EventConsumer(queue);
etai = resultAggregator.consumeAndBreakOnInterrupt(consumer);
producerRunnable.addDoneCallback(consumer.createAgentRunnableDoneCallback());
etai = resultAggregator.consumeAndBreakOnInterrupt(consumer);
Comment on lines 165 to +166
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This reordering is a crucial fix for a potential race condition. To aid future maintenance and prevent this subtle bug from being reintroduced, I recommend adding a comment explaining why this specific order of operations is necessary.

            // It's crucial to add the done callback before starting to consume events.
            // This prevents a race condition where the producer thread might fail before
            // the consumer has registered the callback to handle the error, which could
            // otherwise lead to a deadlock.
            producerRunnable.addDoneCallback(consumer.createAgentRunnableDoneCallback());
            etai = resultAggregator.consumeAndBreakOnInterrupt(consumer);


if (etai == null) {
log.debug("No result, throwing InternalError");
throw new InternalError("No result");
Expand Down