Conversation
…dError asyncio.CancelledError is a BaseException subclass (Python 3.9+), so `except Exception` in kernel loop sites lets it escape, aborting remaining cleanup functions, hook handlers, and contribution collection. Different patterns per site semantics: - collect_contributions: catch CancelledError → break (partial results) - cleanup loops: catch BaseException → continue, re-raise fatal after - session.cleanup: try/finally ensures loader.cleanup always runs - session.execute: catch BaseException for status tracking (re-raises) - hooks emit/emit_and_collect: catch CancelledError → log, continue - cancellation.trigger_callbacks: catch CancelledError → continue Also fixes identical vulnerability in hooks.py (emit, emit_and_collect) and cancellation.py (trigger_callbacks) beyond the original PR scope. Includes 10 new tests for CancelledError resilience across all sites. Inspired-by: ramparte (PR #11) Co-Authored-By: ramparte <ramparte@users.noreply.github.com> 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.
Problem
asyncio.CancelledErroris aBaseExceptionsubclass (since Python 3.9), soexcept Exceptionin kernel loop-based exception handling sites lets it escape, aborting remaining cleanup functions, hook handlers, and contribution collection. This causes resource leaks (open connections, file handles) and skipped lifecycle observers.Inspired by #11 from @ramparte, who correctly identified this class of bug. This PR expands the fix to all affected sites with context-appropriate exception handling patterns, and adds comprehensive tests.
Approach
Different patterns per site semantics — one size does not fit all:
collect_contributionsexcept CancelledError→breakcoordinator.cleanup)except BaseException→ continue, re-raise fatal afterKeyboardInterrupt/SystemExitaftersession.cleanuptry/finallyloader.cleanup()(sys.path restoration) always runssession.executeexcept BaseException→ status tracking → re-raisehooks.emit/emit_and_collectexcept CancelledError→ log, continuesession:end)cancellation.trigger_callbacksexcept CancelledError→ continue, re-raise fatal afterFiles Changed
amplifier_core/coordinator.py—collect_contributions()+cleanup()amplifier_core/session.py—_safe_exception_str()+execute()+cleanup()amplifier_core/hooks.py—emit()+emit_and_collect()amplifier_core/cancellation.py—trigger_callbacks()tests/test_cancellation_resilience.py— 10 new tests covering all sitesTesting
test_event_taxonomy.pyunrelated to this change)python_check(ruff + pyright) clean on all changed filesCloses
Supersedes #11