Skip to content

fix: make TrackingContext thread-safe#1321

Open
alyabouzaid wants to merge 2 commits into
amazon-braket:mainfrom
alyabouzaid:fix-tracking-context-thread-safety
Open

fix: make TrackingContext thread-safe#1321
alyabouzaid wants to merge 2 commits into
amazon-braket:mainfrom
alyabouzaid:fix-tracking-context-thread-safety

Conversation

@alyabouzaid

Copy link
Copy Markdown

Issue #, if available: Fixes #1319

Description of changes:

TrackingContext shares one process-global set of trackers; broadcast_event() (called on every get_quantum_task()) iterates it while Tracker context managers on other threads add/remove entries, which intermittently raises RuntimeError: Set changed size during iteration. This change guards the set with a threading.Lock and has broadcast_event iterate over a snapshot, so the lock is never held across tracker callbacks.

Testing done:

AI disclosure: this PR was prepared with the assistance of Claude Code (Anthropic's Claude); I review and test all changes.

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

TrackingContext stores active trackers in a plain set that is shared
process-wide and mutated by Tracker context managers while
broadcast_event iterates it. Concurrent task submission or status
polling with an active Tracker intermittently raises
'RuntimeError: Set changed size during iteration'.

Guard the set with a lock and have broadcast_event iterate over a
snapshot, keeping the critical section small and avoiding holding the
lock across tracker callbacks.

Fixes amazon-braket#1319
@alyabouzaid
alyabouzaid requested a review from a team as a code owner July 13, 2026 05:40
@github-actions github-actions Bot added the aws label Jul 13, 2026
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (df95926) to head (bf5ec8a).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1321   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          171       171           
  Lines        11550     11556    +6     
  Branches      1511      1511           
=========================================
+ Hits         11550     11556    +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TrackingContext is not thread-safe: broadcast_event iterates _trackers without a lock → RuntimeError: Set changed size during iteration

2 participants