Repository navigation
fix(hooks): hooks_installed removed a hook it did not install - #424
Merged
NandhaKishorM merged 1 commit intoSep 29, 2026
Merged
Conversation
Owner
|
Thank you for finding this, a block really should not remove a hook the application installed itself. Restoring a snapshot causes two new problems, though: with two overlapping blocks the first exit removes the second block's hook and later reinstalls the first block's hook for good, and a hook added with add_hook inside the block is lost. Could you instead remove exactly one occurrence of each hook the block added (the most recent match by identity) under the registry lock? Please also add tests for interleaved blocks and for add_hook inside a block. |
The context manager restored the list by removing every entry matching a hook it had added, by identity. A hook object that an application had already installed therefore matched too, and was removed along with the block's own copy: ``` installed one hook before the block: 1 inside the block : 2 hook(s) after the block : 0 hook(s) <- the caller's hook is gone dispatches on the next call : 0 ``` `docs/hooks/api.md` and `docs/hooks/patterns.md` both say the block *"restores the previous list on exit, even if the block raises"*. An application that installs one audit or metrics hook at startup and later wraps a request in `hooks_installed` for a temporary one loses the audit hook for the rest of the process, silently. ## The snapshot fix was wrong in two other ways The first version of this fix saved `self.hooks` on entry and assigned it back on exit. The review caught two cases that breaks, and both are real: - **Two overlapping blocks.** The inner block installs its hook. The inner block exits and reinstates the snapshot taken *before* it, which is also before the outer block's hook was added — so the outer hook disappears while its `with` is still open. Worse, at the outer exit the outer snapshot is reinstated, so a hook that a *later* caller installed inside the window is removed for good. - **A hook added with `add_hook` inside the block** is not in the snapshot, so it is discarded on exit even though the block did not put it there. ## What it does now Removes **one occurrence per hook the block added**, the most recent match by identity, under the registry lock. `_extend_hooks` appends, so a hook that was already installed sits earlier in the list than the block's copy, which makes "most recent match" the block's own. Everything the block did not add is left alone. Deliberate choices tested: - **One occurrence per hook, not one per list entry.** A hook named twice in one `hooks_installed(...)` call is added twice and removed twice, so the block still leaves nothing behind — while a hook the caller installed once keeps its own copy. - **The lock is held for the read-modify-write.** The whole removal happens inside `_hooks_mutex_for_registry`, so a concurrent `add_hook` cannot be interleaved between the copy and the assignment. - **`_remove_hooks` is left alone.** Its predicate semantics are right for a caller who wants "remove every copy"; the bug was using it to implement a restore. - **The tests cover the arrangements the old ones could not.** The existing check installed a *temporary* hook that was registered nowhere else, which passes under any of the three implementations. The new ones install the hook first, nest two blocks, and add a hook inside a block — the only arrangements that distinguish them. ``` # with the snapshot version restored, the new tests fail: IndexError: list index out of range (f.hooks[0] after the block -- the hook added inside was discarded) $ python tests/test_hooks.py 176 passed, 0 failed (141 before) $ python tests/test_hooks_api.py all hook API tests passed $ python -m pytest tests/test_serve.py tests/test_system_one_lang.py -q 31 passed $ python -m ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120 All checks passed! $ python -m compileall -q laya/ tests/ ``` Verified against all four arrangements directly, with a weight-free fake agent: a hook installed before the block survives and still fires once; a hook added inside survives; an inner block's exit leaves the outer hook installed; and a hook named twice in one block leaves nothing behind. Known limitation: this is the Python side. `laya-ts/src/hooks.ts` carries the same remove-by-identity logic from PR NandhaKishorM#308, so the original defect is present in the port until that is addressed — the same one-occurrence-per-hook change applies there, in a separate PR so this diff stays one logical change.
PerryLink
force-pushed
the
fix/hooks-installed-restores
branch
from
September 25, 2026 15:40
250ba1b to
48f9760
Compare
Owner
|
Passed checks and merged. Thank you @PerryLink! It ships in 0.3.22. |
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.
fix(hooks):
hooks_installedremoved a hook it did not installThe context manager restored the list by removing every entry matching a hook it had added, by
identity. A hook object that an application had already installed therefore matched too, and was
removed along with the block's own copy:
Both pages that document it say the opposite —
docs/hooks/api.md:144anddocs/hooks/patterns.md:224, "restores the previous list on exit, even if the block raises".An application that installs one audit or metrics hook at startup and later wraps a request in
hooks_installedfor a temporary one loses the audit hook for the rest of the process, silently:the block is the only thing that ran, and it looks like it cleaned up after itself.
Installing a hook that is already installed is legal —
add_hookappends, and the hook list is alist — so the block has to take its own copy back off and leave the caller's in place.
Deliberate choices tested:
_extend_hookswould capture the block's own hooks, and taking it without the lock would race a concurrentadd_hook; both are avoided by reading it first, under_hooks_mutex_for_registry, which is the lock the rest of the registry uses._remove_hooksis left alone. It is public-ish and its predicate semantics are right for a caller who wants "remove every copy"; the bug was using it to implement a restore.test_hooks.pyalready checked that a temporary hook fires only inside the block, which passes either way because the temporary hook is not registered anywhere else. The new cases install the hook first, which is the only arrangement that distinguishes the two implementations.Known limitation: this is the Python side.
laya-ts/src/hooks.ts:270carries the sameremove-by-identity logic in the port, in PR #308, so the same case is present in TypeScript until
that is addressed — worth doing in the same shape, but in a separate PR so this diff stays one
change.