Skip to content

feat(server): Add lock to TaskUpdater to prevent race conditions #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2025

Conversation

pankaj-bind
Copy link
Contributor

@pankaj-bind pankaj-bind commented Jul 5, 2025

Description

This PR adds a lock to the TaskUpdater to prevent race conditions when updating tasks that are in a terminal state (e.g., completed, failed). This ensures updates are handled atomically, making task state management more robust.

File Changes

  • src/a2a/server/tasks/task_updater.py
  • tests/server/tasks/test_task_updater.py

Benefits

  • Robustness: Prevents race conditions during concurrent task updates.
  • Stability: Ensures reliable task execution in production.
  • Test Coverage: Adds new tests for concurrent scenarios.

Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Follow the CONTRIBUTING Guide.
  • Make your Pull Request title in the https://www.conventionalcommits.org/ specification.
    • Important Prefixes for release-please:
      • fix: which represents bug fixes, and correlates to a SemVer patch.
      • feat: represents a new feature, and correlates to a SemVer minor.
      • feat!:, or fix!:, refactor!:, etc., which represent a breaking change (indicated by the !) and will result in a SemVer major.
  • Ensure the tests and linter pass (Run nox -s format from the repository root to format)
  • Appropriate docs were updated (if necessary)

Fixes #278 🦕

@pankaj-bind pankaj-bind requested a review from a team as a code owner July 5, 2025 18:20
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.

Summary of Changes

Hello @pankaj-bind, 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 addresses a potential race condition within the TaskUpdater component. My changes introduce an asyncio.Lock and state management to ensure that task status updates, especially when transitioning to a terminal state (like completed or failed), are handled atomically. This significantly enhances the robustness and stability of task state management by preventing multiple concurrent updates from corrupting the final state.

Highlights

  • Concurrency Control: I've integrated an asyncio.Lock into the TaskUpdater to serialize access to the update_status method, preventing race conditions during concurrent modifications.
  • Terminal State Enforcement: The TaskUpdater now tracks whether a task has reached a terminal state (completed, canceled, failed, rejected). Once a task is in such a state, any subsequent attempts to update its status will raise a RuntimeError, ensuring state integrity.
  • Enhanced Test Coverage: I've added new unit tests specifically designed to validate the race condition prevention mechanism and the terminal state enforcement, including a test simulating concurrent updates.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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

This pull request introduces a lock to the TaskUpdater to prevent race conditions when updating a task's status, particularly to a terminal state. The implementation correctly uses asyncio.Lock to ensure atomic updates and adds a flag to prevent further updates once a terminal state is reached. The accompanying tests are well-written and cover both the new error-handling logic and the concurrent update scenario.

My review includes a high-severity issue regarding the consistency of the final flag for terminal states.
Overall, this is a solid improvement to the robustness of task state management.

Copy link
Contributor

@lukehinds lukehinds left a comment

Choose a reason for hiding this comment

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

Looks good to me.

pankaj-bind and others added 3 commits July 6, 2025 18:05
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@pstephengoogle pstephengoogle merged commit 1022093 into a2aproject:main Jul 7, 2025
5 checks passed
swapydapy pushed a commit that referenced this pull request Jul 8, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.2.11](v0.2.10...v0.2.11)
(2025-07-07)


### ⚠ BREAKING CHANGES

* Removes `push_notifier` interface from the SDK and introduces
`push_notification_config_store` and `push_notification_sender` for
supporting push notifications.

### Features

* Add constants for Well-Known URIs
([#271](#271))
([1c8e12e](1c8e12e))
* Adds support for List and Delete push notification configurations.
([f1b576e](f1b576e))
* Adds support for more than one `push_notification_config` per task.
([f1b576e](f1b576e))
* **server:** Add lock to TaskUpdater to prevent race conditions
([#279](#279))
([1022093](1022093))
* Support for database backend Task Store
([#259](#259))
([7c46e70](7c46e70))


### Code Refactoring

* Removes `push_notifier` interface from the SDK and introduces
`push_notification_config_store` and `push_notification_sender` for
supporting push notifications.
([f1b576e](f1b576e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
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.

[Feat]: Race Condition in TaskUpdater for Terminal States
4 participants