Skip to content

Commit a9e49d3

Browse files
committed
fix(context): drop every listener wrapper's captured context at the last shutdown, even where it stays attached
1 parent e321129 commit a9e49d3

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

‎src/lib/helpers/eventemitter-context.mjs‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @Email: <[email protected]>
88
* -----
99
* @Last modified by: Nate Corcoran <CLDMV> ([email protected])
10-
* @Last modified time: 2026-10-03T22:14:52-07:00 (1791090892)
10+
* @Last modified time: 2026-10-12T16:02:57-07:00 (1791846177)
1111
* -----
1212
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
1313
*
@@ -130,12 +130,12 @@ function runtime_wrapEventListener(listener) {
130130
// carries identity instead. A Node run cannot take it — the import is a live binding, not a value
131131
// this suite can swap — so it is exercised only where the platform lacks the module.
132132
/* v8 ignore next */
133-
const resource = AsyncResource ? new AsyncResource("slothlet-event-listener") : null;
133+
let resource = AsyncResource ? new AsyncResource("slothlet-event-listener") : null;
134134

135135
// Bind the listener to whoever registered it, for the runtimes the AsyncResource capture does
136136
// not serve. Done here, at registration, because that is when the registering module is the
137137
// executing one; by the time the event fires it is long gone.
138-
const bound = pinToCurrentCaller(listener);
138+
let bound = pinToCurrentCaller(listener);
139139

140140
const epoch = releaseEpoch;
141141
// Create wrapped listener that executes within captured context
@@ -154,6 +154,13 @@ function runtime_wrapEventListener(listener) {
154154
// Attach metadata for cleanup and identification
155155
runtime_wrappedListener._slothletOriginal = listener;
156156
runtime_wrappedListener._slothletResource = resource;
157+
// Drops the captured context at the last shutdown (#605 review), whether or not the wrapper can be
158+
// taken off its emitter: a released wrapper calls its listener directly and needs neither capture.
159+
runtime_wrappedListener._slothletRelease = () => {
160+
resource = null;
161+
bound = null;
162+
runtime_wrappedListener._slothletResource = null;
163+
};
157164

158165
return runtime_wrappedListener;
159166
}
@@ -460,6 +467,10 @@ function runtime_patchOnce() {
460467
// Metadata for slothlet tracking + Node's introspection contract.
461468
runtime_onceWrapper._slothletOriginal = listener;
462469
runtime_onceWrapper._slothletResource = wrapped._slothletResource;
470+
runtime_onceWrapper._slothletRelease = () => {
471+
wrapped._slothletRelease();
472+
runtime_onceWrapper._slothletResource = null;
473+
};
463474
runtime_onceWrapper.listener = listener;
464475

465476
runtime_trackListener(this, event, listener, runtime_onceWrapper);
@@ -530,6 +541,10 @@ function runtime_patchPrependOnceListener() {
530541

531542
runtime_onceWrapper._slothletOriginal = listener;
532543
runtime_onceWrapper._slothletResource = wrapped._slothletResource;
544+
runtime_onceWrapper._slothletRelease = () => {
545+
wrapped._slothletRelease();
546+
runtime_onceWrapper._slothletResource = null;
547+
};
533548
runtime_onceWrapper.listener = listener;
534549

535550
runtime_trackListener(this, event, listener, runtime_onceWrapper);
@@ -706,7 +721,9 @@ function runtime_plainOnce(emitter, event, listener) {
706721
* `prependOnceListener` wrapper for a plain one-shot wrapper with no slothlet state, which reports its
707722
* listener through `.listener` as Node's own does. The emitter then holds what it would without
708723
* slothlet. The swap edits the listener table directly, so no `removeListener` / `newListener` event
709-
* fires; a table the emitter keeps read-only is left as it is, its wrappers released.
724+
* fires; a table the emitter keeps read-only is left as it is, its wrappers released. Every wrapper
725+
* drops its captured context (its AsyncResource and pinned caller) first, so one left on such a table
726+
* keeps no instance alive.
710727
*
711728
* @public
712729
*/
@@ -717,6 +734,15 @@ export function cleanupEventEmitterResources() {
717734
releaseEpoch++;
718735
for (const emitter of trackedEmitters) {
719736
const emitterTracking = wrappedListeners.get(emitter);
737+
// Every wrapper drops its captured context first, so one the listener table keeps (a table that
738+
// cannot be written) holds no instance state either (#605 review).
739+
if (emitterTracking) {
740+
for (const byOriginal of emitterTracking.values()) {
741+
for (const wrappers of byOriginal.values()) {
742+
for (const wrapper of wrappers) wrapper._slothletRelease();
743+
}
744+
}
745+
}
720746
const events = emitter._events;
721747
if (emitterTracking && events) {
722748
try {
@@ -740,8 +766,9 @@ export function cleanupEventEmitterResources() {
740766
}
741767
}
742768
} catch (____error) {
743-
// A listener table the emitter keeps read-only cannot be edited; its wrappers stay, released,
744-
// so they run their listeners as plain code. The other emitters are still released.
769+
// A listener table the emitter keeps read-only cannot be edited; its wrappers stay, released and
770+
// holding no captured context, so they run their listeners as plain code. The other emitters are
771+
// still released.
745772
}
746773
}
747774
// Drop this emitter's tracking entry, so a tracked emitter still referenced after shutdown does not

‎tests/vitests/suites/listener-cleanup/shutdown-keeps-listeners.test.vitest.mjs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @Email: <[email protected]>
88
* -----
99
* @Last modified by: Nate Corcoran <CLDMV> ([email protected])
10-
* @Last modified time: 2026-10-10T19:32:01-07:00 (1791685921)
10+
* @Last modified time: 2026-10-12T16:03:41-07:00 (1791846221)
1111
* -----
1212
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
1313
*
@@ -152,6 +152,18 @@ describe.each(getMatrixConfigs({}))("the last shutdown keeps a module's listener
152152
expect(frozen.conn.listenerCount("error")).toBe(1);
153153
});
154154

155+
it("a wrapper left on a listener table that cannot be written holds no captured context after shutdown", async () => {
156+
api = await slothlet({ ...config, base: root });
157+
const frozen = await api.conn.open();
158+
Object.freeze(frozen.conn._events);
159+
await api.shutdown();
160+
api = null;
161+
for (const event of ["error", "close", "ready"]) {
162+
const [wrapper] = frozen.conn.rawListeners(event);
163+
expect(wrapper._slothletResource ?? null, event).toBeNull();
164+
}
165+
});
166+
155167
it("a socket opened inside an api call keeps its error listener, so a later destroy is handled", async () => {
156168
server = net.createServer((socket) => sockets.add(socket));
157169
await new Promise((done) => server.listen(0, "127.0.0.1", done));

0 commit comments

Comments
 (0)