fix(hooks): keep a strong reference to fire-and-forget hook triggers - #2565
Open
LHMQ878 wants to merge 1 commit into
Open
fix(hooks): keep a strong reference to fire-and-forget hook triggers#2565LHMQ878 wants to merge 1 commit into
LHMQ878 wants to merge 1 commit into
Conversation
`asyncio` holds tasks in a `WeakSet`, so `asyncio.create_task(engine.trigger(...))` followed by discarding the local variable lets the GC collect a still-pending task. The hook subprocess is then never awaited and the hook silently does not run — non-deterministically, depending on when a collection happens. `HookEngine.fire_and_forget_trigger()` was added for exactly this pattern and holds the task in `_pending_fire_and_forget` until it completes, but only `SubagentStop` was migrated to it. Five call sites still rolled their own `create_task` + `add_done_callback`: - `soul/toolset.py` — `PostToolUse`, `PostToolUseFailure` - `soul/kimisoul.py` — `StopFailure`, `Notification`, `PostCompact` Reported for `PostToolUse` / `PostToolUseFailure` in MoonshotAI#2564; the other three are the same defect and are fixed here too. The helper also logs a failing task via `logger.warning` instead of calling `task.exception()` for its side effect, so hook failures stop being swallowed silently. `SessionEnd` in `cli/__init__.py` is left alone — it is awaited under `asyncio.wait_for`, so it is not fire-and-forget. Tests: `fire_and_forget_trigger` had no coverage at all, which is how the migration got missed. Added tests that the task survives a `gc.collect()` with no caller reference and still runs its command to completion, that the reference is released once done, and an AST guard asserting no `create_task` wraps a hook `trigger` call in either module. Reverting the fix fails the two guard tests. Fixes MoonshotAI#2564 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Jul 28, 2026
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.
Fixes #2564.
Problem
asyncioholds running tasks in aWeakSet. Writinglets the GC collect the still-pending task, so the hook subprocess is never
awaited and the hook silently does not run. It fires non-deterministically,
depending on when a collection happens.
HookEngine.fire_and_forget_trigger()already exists for exactly this pattern —its docstring describes this bug — and keeps the task in
_pending_fire_and_forgetuntil it completes. OnlySubagentStophad beenmigrated to it.
What changed
Five call sites still rolled their own
create_task:soul/toolset.pyPostToolUse,PostToolUseFailuresoul/kimisoul.pyStopFailure,Notification,PostCompact#2564 reports the two in
toolset.py. The three inkimisoul.pyare the samedefect, so they are fixed here too.
Two side benefits of using the helper: the task is discarded from the pending set
on completion (no leak), and a failing task is reported through
logger.warning("Fire-and-forget hook task failed")instead of having itsexception fetched purely for the side effect and dropped.
SessionEndincli/__init__.py:697is deliberately left as-is — it runs underawait asyncio.wait_for(..., timeout=5), so it is not fire-and-forget.Tests
fire_and_forget_triggerhad no test coverage at all, which is plausibly howthe migration got missed. New
tests/hooks/test_fire_and_forget.py:gc.collect()when the caller keeps no reference, and itscommand still runs to completion (asserted via a file the hook touches)
toolset.pyandkimisoul.pyasserting that nocreate_taskcall wraps a hooktrigger(...)call, so a future fire-and-forgetsite cannot silently reintroduce this
Control experiment — reverting only
src/and rerunning:With the fix applied:
5 passed.ruff checkandruff format --checkare clean on all three files.Note on the pre-existing failures
pytest tests/hooks/ tests/core/test_dynamic_injection_hooks.pyreports5 failed, 38 passedon this branch. Those 5 (test_integration.py::test_pre_tool_use_block_flow,::test_stop_hook_feedback,::test_user_prompt_submit_block,test_runner.py::test_exit_2_blocks,::test_json_deny_decision) failidentically on unmodified
main— they rely on POSIX shell semantics and I ranthis on Windows. Baseline is
5 failed, 33 passed; the only difference is my 5new tests passing.