Skip to content

fix(context): keep a module's listeners at the last shutdown, restored to what the code registered - #606

Merged
Shinrai merged 6 commits into
nextfrom
fix/shutdown-foreign-listeners
Oct 13, 2026
Merged

Shinrai merged 6 commits into
nextfrom
fix/shutdown-foreign-listeners

Conversation

@cldmv-bot

@cldmv-bot cldmv-bot Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

🚀 What's Changed

💥 Breaking Changes

No breaking changes

✨ Features

No new features

🐛 Bug Fixes

📦 Dependencies

No dependency updates

🔧 Other Changes

👥 Contributors

…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
@cldmv-bot cldmv-bot Bot added the ! fix → next v4 flow: fix contributor PR targeting the next integration branch label Oct 11, 2026
@Shinrai

Shinrai commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Resolves #605 — closed when this ships to the default branch.

@Shinrai
Shinrai requested a balanced review from Copilot October 11, 2026 02:38
@cldmv-bot cldmv-bot Bot added area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure type: documentation Relates to docs, README updates, guides, or inline code comments labels Oct 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Once wrappers retain captured context resources, and immutable listener tables can cause shutdown cleanup to abort.

3 open findings
What changed in this PR

Updates EventEmitter shutdown cleanup to preserve module listeners while removing Slothlet context wrapping.

Changes:

  • Restores persistent listeners and releases stale wrapper epochs.
  • Adds shutdown and multi-instance regression coverage.
  • Documents post-shutdown listener behavior.
File Description
src/​lib/​helpers/​eventemitter-context.mjs Implements listener restoration and epoch release.
docs/​CONTEXT-PROPAGATION.md Documents shutdown behavior.
tests/​vitests/​suites/​listener-cleanup/​third-party-cleanup.test.vitest.mjs Updates native and third-party expectations.
tests/​vitests/​suites/​listener-cleanup/​shutdown-keeps-listeners.test.vitest.mjs Adds late-event regression tests.
tests/​vitests/​suites/​context/​eventemitter-multi-instance-shutdown.test.vitest.mjs Updates multi-instance shutdown coverage.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread docs/CONTEXT-PROPAGATION.md Resolved
…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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Read-only listener tables can retain captured context, and the replacement once wrapper does not fully preserve one-shot semantics.

1 open finding
3 resolved since last review
Previously missed (2)

In code that hasn't changed since last review

Medium severity Use _slothletOriginal to detect stale once wrappers

tests/​vitests/​suites/​context/​eventemitter-multi-instance-shutdown.test.vitest.mjs:40

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.

Medium severity Detect stale Slothlet once wrappers in shutdown assertion

tests/​vitests/​suites/​listener-cleanup/​third-party-cleanup.test.vitest.mjs:47

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.

🧠 Review effort: Balanced

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.
@Shinrai
Shinrai force-pushed the fix/shutdown-foreign-listeners branch from 6157fbb to e321129 Compare October 11, 2026 16:41
@Shinrai

Shinrai commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Read-only listener tables leave attached wrappers retaining AsyncResource or pinned-caller state after shutdown.

0 open findings

1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity 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.

🧠 Review effort: Balanced

@Shinrai

Shinrai commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Previously-missed item from review 5484096909 (against 3d1db09):

  • 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It modifies process-wide EventEmitter behavior and directly edits Node.js listener internals, warranting final human review.

0 open findings

🧠 Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Touches core library / runtime source code area: tests Touches test files, fixtures, or test infrastructure ! fix → next v4 flow: fix contributor PR targeting the next integration branch type: documentation Relates to docs, README updates, guides, or inline code comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants