@@ -9,21 +9,29 @@ if (common.isASan) {
99import { describe , it } from 'node:test' ;
1010
1111const makeSubsequentCalls = ( limit , done , holdReferences = false ) => {
12+ let dependantSymbol ;
13+ let signalRef ;
1214 const ac = new AbortController ( ) ;
1315 const retainedSignals = [ ] ;
14- let dependantSymbol ;
16+ const handler = ( ) => { } ;
1517
1618 function run ( iteration ) {
1719 if ( iteration > limit ) {
18- global . gc ( ) ;
19- done ( ac . signal , dependantSymbol ) ;
20+ // This setImmediate is necessary to ensure that in the last iteration the remaining signal is GCed (if not
21+ // retained)
22+ setImmediate ( ( ) => {
23+ global . gc ( ) ;
24+ done ( ac . signal , dependantSymbol ) ;
25+ } ) ;
2026 return ;
2127 }
2228
2329 if ( holdReferences ) {
2430 retainedSignals . push ( AbortSignal . any ( [ ac . signal ] ) ) ;
2531 } else {
26- AbortSignal . any ( [ ac . signal ] ) ;
32+ // Using a WeakRef to avoid retaining information that will interfere with the test
33+ signalRef = new WeakRef ( AbortSignal . any ( [ ac . signal ] ) ) ;
34+ signalRef . deref ( ) . addEventListener ( 'abort' , handler ) ;
2735 }
2836
2937 if ( ! dependantSymbol ) {
@@ -33,7 +41,12 @@ const makeSubsequentCalls = (limit, done, holdReferences = false) => {
3341 dependantSymbol = kDependantSignals ;
3442 }
3543
36- setImmediate ( ( ) => run ( iteration + 1 ) ) ;
44+ setImmediate ( ( ) => {
45+ // Removing the event listener at some moment in the future
46+ // Which will then allow the signal to be GCed
47+ signalRef ?. deref ( ) ?. removeEventListener ( 'abort' , handler ) ;
48+ run ( iteration + 1 ) ;
49+ } ) ;
3750 }
3851
3952 run ( 1 ) ;
@@ -92,3 +105,29 @@ describe('when there is a short-lived signal', () => {
92105 } ) ;
93106 } ) ;
94107} ) ;
108+
109+ // describe('when provided signal is composed', () => {
110+ // it('drops settled dependant signals', (t, done) => {
111+ // const controllers = Array.from({ length: 2 }, () => new AbortController());
112+ // const composedSignal1 = AbortSignal.any([controllers[0].signal]);
113+ // const composedSignalRef = new WeakRef(AbortSignal.any([composedSignal1, controllers[1].signal]));
114+
115+ // global.gc();
116+
117+ // setImmediate(() => {
118+ // // Object.getOwnPropertySymbols(composedSignal1).forEach((s) => {
119+ // // console.log(s, composedSignal1[s]);
120+ // // });
121+
122+ // // console.log('signal 2 ====')
123+
124+ // const composedSignal2 = composedSignalRef.deref();
125+
126+ // Object.getOwnPropertySymbols(composedSignal2).forEach((s) => {
127+ // console.log(s, composedSignal2[s]);
128+ // });
129+ // done();
130+ // });
131+
132+ // });
133+ // });
0 commit comments