Skip to content

fix: synchronize ClientTaskManager to prevent race condition in concu…#742

Open
kabir wants to merge 2 commits intoa2aproject:mainfrom
kabir:intermittent-failure
Open

fix: synchronize ClientTaskManager to prevent race condition in concu…#742
kabir wants to merge 2 commits intoa2aproject:mainfrom
kabir:intermittent-failure

Conversation

@kabir
Copy link
Collaborator

@kabir kabir commented Mar 13, 2026

…rrent SSE event processing

ClientTaskManager was not thread-safe, causing intermittent test failures when
SSE events were delivered concurrently by the HTTP client. The race condition
occurred when multiple events (startWork, addArtifact, complete) were emitted
rapidly in streaming mode.

Without synchronization, concurrent threads could interleave read-modify-write
operations on currentTask, resulting in lost updates. Specifically, the artifact
update could be overwritten by a status update, leaving the final COMPLETED task
with no artifacts. The fast time to failure of the test 0.017s, indicates that the
COMPLETED status update was received, and that the latch did not time out.

This manifested as intermittent failures in agent-to-agent tests where the test
received a COMPLETED task but extractTextFromTask() returned an empty string
because the artifact list was empty.

Fix: Added synchronized keyword to all ClientTaskManager methods (getCurrentTask,
saveTaskEvent variants, updateWithMessage) to ensure atomic processing of events
regardless of HTTP client threading behavior.

Impact: All client transports (JSON-RPC, gRPC, REST) in streaming mode
Validated: 100 consecutive test iterations passed on CI

Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

…rrent SSE event processing

  ClientTaskManager was not thread-safe, causing intermittent test failures when
  SSE events were delivered concurrently by the HTTP client. The race condition
  occurred when multiple events (startWork, addArtifact, complete) were emitted
  rapidly in streaming mode.

  Without synchronization, concurrent threads could interleave read-modify-write
  operations on currentTask, resulting in lost updates. Specifically, the artifact
  update could be overwritten by a status update, leaving the final COMPLETED task
  with no artifacts.

  This manifested as intermittent failures in agent-to-agent tests where the test
  received a COMPLETED task but extractTextFromTask() returned an empty string
  because the artifact list was empty.

  Fix: Added synchronized keyword to all ClientTaskManager methods (getCurrentTask,
  saveTaskEvent variants, updateWithMessage) to ensure atomic processing of events
  regardless of HTTP client threading behavior.

  Impact: All client transports (JSON-RPC, gRPC, REST) in streaming mode
  Validated: 200 consecutive test iterations passed on CI

  Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical concurrency issue within the ClientTaskManager by implementing thread-safe access to its state. The change ensures that concurrent streaming events, particularly from HTTP clients, are processed atomically, thereby eliminating race conditions that led to lost task updates and intermittent test failures.

Highlights

  • Concurrency Fix: Implemented synchronization for key methods within ClientTaskManager to prevent race conditions during concurrent SSE event processing.
  • Thread Safety: Ensured thread-safe operations for getCurrentTask, saveTaskEvent variants, and updateWithMessage methods by adding the synchronized keyword.
  • Bug Resolution: Addressed intermittent test failures where task artifacts were lost due to interleaved read-modify-write operations on the currentTask.
Changelog
  • client/base/src/main/java/io/a2a/client/ClientTaskManager.java
    • The getCurrentTask() method was made synchronized.
    • The saveTaskEvent(Task) method was made synchronized.
    • The saveTaskEvent(TaskStatusUpdateEvent) method was made synchronized.
    • The saveTaskEvent(TaskArtifactUpdateEvent) method was made synchronized.
    • The updateWithMessage(Message, Task) method was made synchronized.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/test-race-condition.yml
Activity
  • No specific activity (comments, reviews, progress updates) has been provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request modifies ClientTaskManager.java by adding the synchronized keyword to several public methods, including getCurrentTask, saveTaskEvent (multiple overloads), and updateWithMessage. This change ensures thread safety for task management operations, preventing potential race conditions when accessing or modifying shared task-related state from multiple threads.

@@ -0,0 +1,69 @@
name: Test Race Condition Fix (Temporary)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@kabir do you plan to keep it in the CI or remove it before merging?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought I had removed it. Done now

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.

2 participants