7788 * -----
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
0 commit comments