Repository navigation
fix: join owned Python tasks before execution cancellation returns - #126
Draft
Brian Krabach (bkrabach) wants to merge 1 commit into
Draft
Brian Krabach (bkrabach) wants to merge 1 commit into
Brian Krabach (bkrabach) wants to merge 1 commit into
Conversation
Own each native execution scope and join its Python stages and hook runners before acknowledging cancellation, including repeated and pre-start cancellation. Keep teardown hooks and unrelated executions independent. Document the contract and retain native cancellation regressions. Generated with Amplifier Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
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.
Summary
Make cancellation of Rust-backed Python
RustSession.execute()acknowledge completion of the execution's owned Python work, rather than only dropping the native receiver. The public await joins its debug stage, orchestrator stage, and submitted Python hook runners, including their asynchronousfinallyblocks, before re-raisingasyncio.CancelledError.This is a draft for behavioral and implementation review, not a merge-ready release PR. The branch remains at version
2.0.2; its locally built test wheel is not a published fixed release. The atomic version bump and release gates below are intentionally incomplete, not waived.Source:
11ca41713add9f16af050685cd282b851840632a, based on9614052f7c9e493977dcba3f5c98ebd4d8591ec5.Why and how
The unmodified native bridge reproduced a public cancellation returning while its separately scheduled Python orchestrator still awaited a cooperatively cancellable tool. A caller could then save, replace, or clean up the session while the old execution remained active.
The change keeps the ownership mechanism at the native/Python boundary:
session.rsreturns a dormant public coroutine and captures a per-execution loop/context and Rust scope guard. Python stage coroutines are created lazily only when their loop accepts the stage._async_compat.pyretains and observes exactly those stage tasks and submitted hook owners. Rust scope release and Python completion jointly acknowledge settlement. Repeated cancellation cannot abandon a join already draining.bridges.rsenrolls hooks from the emitting task's captured context before submission. Exact hook ownership remains the cancellation authority, so hooks awaited by cancellation finalizers still run. Stale descendant context cannot reopen an already settled execution.CONTRACTS.mdspecifies the boundary. No cancel-all loop sweep, new lifecycle event, global execution owner, kernel deadline, or dependency/version change is introduced.The trade-off is deliberate: cancellation completion becomes stronger and can take longer. A noncooperative finalizer can hold the join indefinitely.
asyncio.wait_for()around a cancellation-draining operation is not a hard abandonment boundary. Hosts retain responsibility for external supervision; this does not guarantee termination of arbitrary detached descendants, threads, sockets, or WASM work.Verification evidence
Previously completed isolated checks used the actual maturin-built ABI3 native extension, not a mocked engine, on Linux aarch64 with normal-GIL CPython 3.14.7:
To reproduce the focused source regression after preparing a native build, run
python -m pytest bindings/python/tests/test_session_execution_cancellation.py -q; usetests/andbindings/python/tests/for the broader Python/binding checks.Cross-host impact and adoption limits
Static traces at Unified
9289170b36dee02686df926486a807946bc4cca8, Agent08853e3f3814af4501acc0231d26bb1d6e516a84, and Resolvea09242596c9af3f04edc0c68c77ccb044bfa65d0support the ownership fix, but do not qualify adopting it:2.0.1pin and requires durable conversation settlement before terminal reporting. This fix matches that intent, but does not update the pin or establish its finalization/terminal ordering at runtime.1.6.1and worker Core2.0.1live in distinct immutable dependency/image closures. Only an interpreter actually adopting the changed native bridge gets this behavior. Daemon upgrades do not upgrade existing workers.No host companion PR, automatic adoption, deployment, or CLI dependency floor is proposed. Synthetic offline/native-local history checks do not cover shared-root UUID sessions, real provider accounts, deployed hosts, or whole Click startup.
Required before merge/release
Under
context/release-mandate.md("Pre-Merge Gate") anddocs/CORE_DEVELOPMENT_PRINCIPLES.mdsection 10:scripts/bump_version.pytopyproject.toml,crates/amplifier-core/Cargo.toml, andbindings/python/Cargo.toml; this branch has no bump.scripts/e2e-smoke-test.shrelease-readiness evidence, including real tool dispatch, delegation, and recipe execution. The offline checks above do not replace it.docs/NATIVE_ARTIFACTS.md. The local interpreter matrix is not that release matrix.The Core owner, not a delegated author/reviewer, owns the eventual merge/release decision. No merge, tag, package publication, or release artifact publication is requested here.
Compatibility and review focus
No public method signature or dependency floor changes. The observable cancellation timing/ownership contract is strengthened, so review noncooperative-finalizer behavior and loop-shutdown limits explicitly. Recommendation: review the ownership mechanism now; keep this draft until the release-readiness requirements are actually satisfied.