Skip to content

Fix/issue 20117 runner aenter race#21124

Open
jellyfish0316 wants to merge 3 commits intoPrefectHQ:mainfrom
jellyfish0316:fix/issue-20117-runner-aenter-race
Open

Fix/issue 20117 runner aenter race#21124
jellyfish0316 wants to merge 3 commits intoPrefectHQ:mainfrom
jellyfish0316:fix/issue-20117-runner-aenter-race

Conversation

@jellyfish0316
Copy link
Contributor

related to #20117

this PR prevents concurrent Runner auto-starts from racing on shared async lifecycle state.

Summary

When multiple flow runs try to auto-start the same Runner concurrently, they could race in Runner.__aenter__ and attempt to enter or tear down shared async contexts more than once. In practice this could surface as:

  • RuntimeError("Each CancelScope may only be used for a single 'with' block")
  • RuntimeError("Attempted to exit cancel scope in a different task than it was entered in")
  • Cannot open a client instance more than once

This change serializes runner lifecycle transitions with a private lock, tracks active context users, and ensures final teardown is performed by the task that entered the shared async context stack.

It also adds a focused regression test covering concurrent implicit startup through execute_bundle.

Validation:

  • uv run pytest tests/runner/test_runner.py -k "test_concurrent_execute_bundle_initializes_runner_once" -vv
  • local high-concurrency repro: 100 iterations of concurrent bundle execution completed without reproducing the runner lifecycle errors above

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 14, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing jellyfish0316:fix/issue-20117-runner-aenter-race (2466170) with main (98309ff)

Open in CodSpeed

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant