Repository navigation
fix: run preClose hook exactly once per declaring instance - #6940
Merged
Merged
Conversation
A single preClose hook added to an instance was executed once for every
instance in the encapsulation subtree (the declaring instance plus each
descendant plugin) instead of exactly once. In addHook, onReady/onListen/
onRoute are special-cased to store the hook only on the declaring instance,
but preClose was omitted, so it fell into the propagating _addHook path
(which copies the hook into every child). Since hookRunnerApplication('preClose')
already recurses the child tree at close time, each declared hook re-fired
once per descendant.
Add preClose to the same special-case branch. buildHooks already resets
children's preClose to [], so preClose was never meant to be inherited; this
makes each declared hook fire exactly once.
Signed-off-by: contactjawad <[email protected]>
metcoder95
approved these changes
Aug 13, 2026
Eomm
approved these changes
Aug 13, 2026
climba03003
pushed a commit
that referenced
this pull request
Aug 14, 2026
…6954) (cherry picked from commit f5ef344) Signed-off-by: contactjawad <[email protected]> Co-authored-by: Jawad Ali <[email protected]>
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.
What
A single
preClosehook added to a Fastify instance is executed once for every instance in the encapsulation subtree (the declaring instance plus each descendant plugin) instead of exactly once.Reproduction
With two levels of nested plugins the same hook runs 3 times. An equivalent
onReadyhook in the identical setup correctly runs once.Root cause
In
addHook, the hooksonReady,onListen, andonRouteare special-cased to be stored only on the declaring instance viathis[kHooks].add.preClosewas omitted from that list, so it falls into theelsebranch and goes through_addHook, which copies the hook into every child viathis[kChildren].forEach. At close time,hookRunnerApplication('preClose')already walks the entire child tree, so each propagated copy fires again.That
preCloseis not meant to be inherited is already established elsewhere in the codebase:buildHooksinlib/hooks.jsresetsonReady,onListen, andpreCloseto empty arrays for children.preClosewas simply left off the special-case list inaddHook.Fix
Add
preCloseto the same special-case branch asonReady/onListen/onRouteso it is stored only on the declaring instance and not propagated into children. SincehookRunnerApplicationalready recurses the child tree andbuildHooksalready excludespreClosefrom inheritance, each declared hook now fires exactly once.Tests
Added two regression tests in
test/close.test.js(a single child plugin and nested child plugins) asserting the hook runs exactly once. Both fail onmain(count 2 and 3) and pass with this change. The fulltest/close.test.jssuite (26 tests) stays green.Checklist
npm run testand the linter