You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(context): keep a module's listeners at the last shutdown, restored to what the code registered - #606
…d to what the code registered
The last instance's shutdown called removeAllListeners() on every emitter
created inside an api call. Those include emitters that outlive slothlet:
undici keeps a fetch socket in its keep-alive pool and later destroys it
with an error, which then had no listener, became an uncaught exception,
and killed the process.
Shutdown now removes no listener. Every listener wrapper is released, so
one firing afterwards calls its listener directly with no instance
context, and on each tracked emitter a wrapper registered with on() or
prependListener() is swapped in place for the registered function, without
emitting removeListener or newListener. A once wrapper removes itself when
it fires and reports its listener through .listener.
Fixes#605
…tdown; skip a read-only listener table
A once wrapper left attached at shutdown still closed over its captured
AsyncResource and pinned caller, so a once event that never fired kept
them alive for the emitter's lifetime. It is now replaced by a plain
one-shot wrapper with no slothlet state, which reports the registered
function through .listener as Node's own does.
A listener table the emitter keeps read-only made the swap throw and abort
the rest of shutdown; that emitter is now left with its released wrappers
and the others are still released.
This check cannot detect a stale Slothlet once wrapper because those wrappers have both _slothletOriginal and .listener. The new plain once replacement retains .listener but drops _slothletOriginal, so the marker alone is the correct discriminator; otherwise the shutdown assertion has a false-negative path.
Detect stale Slothlet once wrappers in shutdown assertion
This predicate excludes Slothlet's stale once wrappers—the exact wrappers this assertion should detect—because those wrappers also define .listener. The stateless shutdown replacement has no _slothletOriginal, so checking that marker alone distinguishes it from both persistent and once Slothlet wrappers; as written, a failure to replace third-party once listeners would pass unnoticed.
A once wrapper read back with rawListeners() and called twice ran its listener twice. Node's own once
wrapper is one-shot: a fired flag makes a second call a no-op. Slothlet's once and prependOnceListener
wrappers, and the plain one-shot wrapper that replaces them at the last shutdown (#605), now carry the
same flag.
…fter shutdown
The shutdown assertions skipped any listener with .listener, so a slothlet once wrapper left in place
would have passed. _slothletOriginal alone tells a slothlet wrapper, on or once, from the plain one-shot
wrapper that replaces it.
Previously-missed findings from review 5481731419, both fixed in e3211295:
Use _slothletOriginal to detect stale once wrappers (eventemitter-multi-instance-shutdown.test.vitest.mjs:40) and Detect stale Slothlet once wrappers in shutdown assertion (third-party-cleanup.test.vitest.mjs:47). Both predicates excluded any listener with .listener, which also excluded a slothlet once wrapper left in place. They now count fn._slothletOriginal !== undefined. Every slothlet wrapper, on or once, carries that marker, and the plain one-shot replacement does not. Both suites pass in all configs.
Clear retained wrapper captures when releasing frozen event listeners
src/lib/helpers/eventemitter-context.mjs:744
This catch path leaves the wrappers' slothlet state retained. runtime_wrapEventListener closes over the const resource and bound values (lines 133–150), and a once wrapper also closes over that wrapped function; advancing releaseEpoch only bypasses those references when invoked. If _events is frozen, the still-attached wrappers therefore keep the AsyncResource/ALS store or pinned caller alive for the emitter's lifetime. Keep the captures in clearable state and clear every tracked wrapper independently of whether replacing the listener-table entry succeeds.
eventemitter-context.mjs:744: when an emitter's listener table cannot be written (a frozen _events), the swap is skipped, so the slothlet wrappers stay attached and their closures kept the AsyncResource and pinned caller alive. Fixed in a9e49d32. The wrapper's captures are now clearable, and every slothlet wrapper (on and once) gets a release hook. At the last shutdown every tracked wrapper is released first, whether or not its table can be edited. A released wrapper calls its listener directly and needs neither capture. Test in shutdown-keeps-listeners: "a wrapper left on a listener table that cannot be written holds no captured context after shutdown". It failed in all 8 configs before.
Types regenerated in 2933bc64; the declarations had not been regenerated for the earlier commits on this branch.
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
area: coreTouches core library / runtime source codearea: testsTouches test files, fixtures, or test infrastructure! fix → nextv4 flow: fix contributor PR targeting the next integration branchtype: documentationRelates to docs, README updates, guides, or inline code comments
2 participants
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's Changed
💥 Breaking Changes
No breaking changes
✨ Features
No new features
🐛 Bug Fixes
📦 Dependencies
No dependency updates
🔧 Other Changes
👥 Contributors