fix(sdk/python): run destructor cleanup synchronously when no loop is running (#620 follow-up) - #902
Merged
Merged
Conversation
…g loop fire_and_forget()'s running-loop branch did a bare loop.create_task(coro). asyncio only keeps a weak reference to a task, so the task could be garbage-collected mid-flight and silently never complete, and because nobody ever retrieved the result, a failing task printed the noisy "Task exception was never retrieved" traceback on collection — exactly what the thread branch of #899 fixed for the no-loop case. Hold the task in a module-level set and attach a done callback that drops the reference and logs any failure at debug level, matching the thread branch's message. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… running #899 replaced the bare asyncio.run() in Agent.__del__ with fire_and_forget(). That fixed the running-loop case (previously it raised RuntimeError and the exception was swallowed), but regressed the common destructor case: with no running loop, fire_and_forget() hands the coroutine to a daemon thread, and at interpreter exit that thread is killed before it does any work. AsyncExecutionManager.stop(), the background-task gather and the notification dispatcher shutdown were all silently dropped. Dispatch on loop presence instead: asyncio.run() when there is no running loop so the cleanup actually completes before __del__ returns, and fire_and_forget() only when a loop is already running. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Performance
✓ No regressions detected |
Contributor
📊 Coverage gateThresholds from
✅ Gate passedNo surface regressed past the allowed threshold and the aggregate stayed above the floor. |
Contributor
📐 Patch coverage gateThreshold: 80% on lines this PR touches vs
✅ Patch gate passedEvery surface whose lines were touched by this PR has patch coverage at or above the threshold. |
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.
Follow-up to #899, which merged before this review landed. Two things from that review: one verified regression and one gap.
1. Regression:
Agent.__del__cleanup stopped running#899 replaced the bare
asyncio.run(self._cleanup_async_resources())inAgent.__del__withfire_and_forget(...).fire_and_forget()has two branches. The running-loop branch was the point of the change and is genuinely a fix — pre-#899 the destructor calledasyncio.run()on the loop thread, which raisedRuntimeErrorand got swallowed by the surroundingexcept Exception: pass, so cleanup never happened. But the no-running-loop branch spawns a daemon thread that callsasyncio.run(coro). That's the common destructor case (GC / interpreter exit), and a daemon thread is killed at interpreter exit before it does any work.Side-by-side probe (same agent,
_cleanup_async_resourcesinstrumented, process exits right after the destructor runs):asyncio.run)RuntimeError, swallowed — cleanup skippedfire_and_forget)So
AsyncExecutionManager.stop(), the background-taskgather, and the notification-dispatcher shutdown were all being silently dropped in the case that fires most often.Fix: dispatch on loop presence inside the existing
try/except— synchronousasyncio.run()when no loop is running (so destructor-at-exit cleanup actually completes),fire_and_forget()only when a loop is already running (whereasyncio.run()would raise).2. Gap:
fire_and_forget's running-loop branch was un-retained and unobservedloop.create_task(coro)with no reference kept. asyncio only holds a weak reference to a task, so it could be collected mid-flight and silently never complete; and because nobody retrieved the result, a failing task printedTask exception was never retrievedon collection — the exact noise the thread branch of #899 fixed for the no-loop case.Fix: retain the task in a module-level
_BACKGROUND_TASKSset and attach a done callback that discards the reference, ignores cancellation, and logs the exception at debug with the same"fire_and_forget background task failed"message the thread branch uses.Validation Contract
_cleanup_async_resourcesto completion synchronously, before__del__returns.tests/test_agent_core.py::test_del_runs_cleanup_to_completion_when_no_loop_is_runningtests/test_agent_core.py::test_del_schedules_cleanup_on_the_running_loop_without_blockingfire_and_forgetinside a running loop that raises does not produceTask exception was never retrievednoise; the failure is logged at debug level.tests/test_run_async.py::test_fire_and_forget_running_loop_failure_is_logged_not_noisytests/test_run_async.py::test_fire_and_forget_running_loop_task_is_retained_until_doneC1, C3 and C4 fail against the pre-fix source on this branch (verified by reverting just the two source files and re-running). C2 passes both before and after — it is a regression guard for the behavior #899 correctly fixed, which this PR must not undo.
Gates
ruff check .(CI-pinned ruff 0.15.22) — All checks passed./scripts/run_pytest.sh(full suite, what CI runs) — 1885 passed, 4 skipped, 36 deselectedRefs #620. Follow-up to #899.
🤖 Generated with Claude Code