fix: make TrackingContext thread-safe#1321
Open
alyabouzaid wants to merge 2 commits into
Open
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available: Fixes #1319
Description of changes:
TrackingContextshares one process-globalsetof trackers;broadcast_event()(called on everyget_quantum_task()) iterates it whileTrackercontext managers on other threads add/remove entries, which intermittently raisesRuntimeError: Set changed size during iteration. This change guards the set with athreading.Lockand hasbroadcast_eventiterate over a snapshot, so the lock is never held across tracker callbacks.Testing done:
mainwith theRuntimeErrorabove and passes with this change.Trackerenter/exit churn): no error after the fix.pytest test/unit_tests/braket/tracking test/unit_tests/braket/aws/test_aws_session.py: 110 passed, 4 xfailed.ruff format --checkandruff check srcclean.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
xin 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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.