Skip to content

Fix race condition in task cancellation handling#421

Open
Avnithakur731-a wants to merge 3 commits into
utksh1:mainfrom
Avnithakur731-a:fix/cancel-task-race-condition
Open

Fix race condition in task cancellation handling#421
Avnithakur731-a wants to merge 3 commits into
utksh1:mainfrom
Avnithakur731-a:fix/cancel-task-race-condition

Conversation

@Avnithakur731-a
Copy link
Copy Markdown

@Avnithakur731-a Avnithakur731-a commented May 30, 2026

Description

This PR fixes a race condition in task cancellation handling.

Changes Made

  • Removed duplicate task status updates from cancel_task().
  • Ensured task cancellation is handled only through the asyncio.CancelledError path in execute_task().
  • Preserved audit logging for cancellation events.
  • Prevented multiple code paths from updating task status simultaneously.

Problem Fixed

Previously, both cancel_task() and execute_task() could update the task status to CANCELLED, leading to a race condition and inconsistent task state handling.

Result

  • Consistent task state updates.
  • Single source of truth for cancellation handling.
  • Improved executor reliability.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

How Has This Been Tested?

  • Reviewed task cancellation flow.
  • Verified that task status updates occur only in the CancelledError handler.
  • Confirmed removal of duplicate cancellation status updates from cancel_task().

Checklist

  • My code follows the code style of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.

@Avnithakur731-a
Copy link
Copy Markdown
Author

Hi @utksh1,

The requested changes have been completed and the CI issues have been resolved. All checks are passing now.

Could you please take a look at the PR and let me know if any further changes are needed? If everything looks good, I would appreciate a merge.

Thanks for your time!

@utksh1 utksh1 added level:intermediate 35 pts difficulty label for moderate contributor PRs type:bug Bug fix work category bonus label type:testing Testing work category bonus label area:backend Backend API, database, or service work labels May 30, 2026
Copy link
Copy Markdown
Owner

@utksh1 utksh1 left a comment

Choose a reason for hiding this comment

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

Thanks for the update. This still needs changes before merge: the PR removes the only database status transition in , so a cancelled task can remain in its previous status after the container/process cleanup. Please keep cancellation authoritative by updating to , setting , broadcasting the status change, and invalidating cached views exactly once. The indentation-only changes lower in the file can also be reverted to keep the PR focused.

@utksh1
Copy link
Copy Markdown
Owner

utksh1 commented May 30, 2026

Clarifying the requested change: keep cancel_task authoritative by updating tasks.status to cancelled, setting completed_at, broadcasting the cancelled status, and invalidating cached views exactly once. The current diff removes that state transition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend Backend API, database, or service work level:intermediate 35 pts difficulty label for moderate contributor PRs type:bug Bug fix work category bonus label type:testing Testing work category bonus label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants