-
-
Notifications
You must be signed in to change notification settings - Fork 32k
lib: clean up references to GCed abort signals #55354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 12 commits into
nodejs:main
from
geeksilva97:abortsignal-mem-leak
Oct 14, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
00dc205
lib: remove settled dependant signals when they are GCed
geeksilva97 220915b
test: test settled signals dropping
geeksilva97 85ac22a
fixup: typo
geeksilva97 9a7c5ce
fix: avoid holding strong reference on finalizers
geeksilva97 4b3ec8e
test: improve tests
geeksilva97 87ce721
lib: optmize finalizers logic
geeksilva97 3fb3765
test: ensure short-lived signals are GCed
geeksilva97 ba52b6b
refactor: rename variables for improving readability
geeksilva97 5f0939f
test: improve signal dropping test
geeksilva97 d3cc655
test: test settled signals when AbortSignal.any receives a composite …
geeksilva97 c1a1c2c
refactor: remove unneeded describe
geeksilva97 c749402
chore: address pr's comments
geeksilva97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
122 changes: 122 additions & 0 deletions
122
test/parallel/test-abortsignal-drop-settled-signals.mjs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Flags: --expose_gc | ||
// | ||
import '../common/index.mjs'; | ||
import { describe, it } from 'node:test'; | ||
|
||
function makeSubsequentCalls(limit, done, holdReferences = false) { | ||
let dependantSymbol; | ||
let signalRef; | ||
const ac = new AbortController(); | ||
const retainedSignals = []; | ||
const handler = () => { }; | ||
|
||
function run(iteration) { | ||
if (iteration > limit) { | ||
// This setImmediate is necessary to ensure that in the last iteration the remaining signal is GCed (if not | ||
// retained) | ||
setImmediate(() => { | ||
global.gc(); | ||
done(ac.signal, dependantSymbol); | ||
}); | ||
return; | ||
} | ||
|
||
if (holdReferences) { | ||
retainedSignals.push(AbortSignal.any([ac.signal])); | ||
} else { | ||
// Using a WeakRef to avoid retaining information that will interfere with the test | ||
signalRef = new WeakRef(AbortSignal.any([ac.signal])); | ||
signalRef.deref().addEventListener('abort', handler); | ||
} | ||
|
||
dependantSymbol ??= Object.getOwnPropertySymbols(ac.signal).find( | ||
(s) => s.toString() === 'Symbol(kDependantSignals)' | ||
); | ||
|
||
setImmediate(() => { | ||
// Removing the event listener at some moment in the future | ||
// Which will then allow the signal to be GCed | ||
signalRef?.deref()?.removeEventListener('abort', handler); | ||
run(iteration + 1); | ||
}); | ||
} | ||
|
||
run(1); | ||
}; | ||
|
||
function runShortLivedSourceSignal(limit, done) { | ||
const signalRefs = new Set(); | ||
|
||
function run(iteration) { | ||
if (iteration > limit) { | ||
global.gc(); | ||
done(signalRefs); | ||
return; | ||
} | ||
|
||
const ac = new AbortController(); | ||
signalRefs.add(new WeakRef(ac.signal)); | ||
AbortSignal.any([ac.signal]); | ||
|
||
setImmediate(() => run(iteration + 1)); | ||
} | ||
|
||
run(1); | ||
}; | ||
|
||
const limit = 10_000; | ||
|
||
describe('when there is a long-lived signal', () => { | ||
it('drops settled dependant signals', (t, done) => { | ||
makeSubsequentCalls(limit, (signal, depandantSignalsKey) => { | ||
setImmediate(() => { | ||
t.assert.strictEqual(signal[depandantSignalsKey].size, 0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('keeps all active dependant signals', (t, done) => { | ||
makeSubsequentCalls(limit, (signal, depandantSignalsKey) => { | ||
t.assert.strictEqual(signal[depandantSignalsKey].size, limit); | ||
|
||
done(); | ||
}, true); | ||
}); | ||
}); | ||
|
||
it('does not prevent source signal from being GCed if it is short-lived', (t, done) => { | ||
runShortLivedSourceSignal(limit, (signalRefs) => { | ||
setImmediate(() => { | ||
const unGCedSignals = [...signalRefs].filter((ref) => ref.deref()); | ||
|
||
t.assert.strictEqual(unGCedSignals.length, 0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('drops settled dependant signals when signal is composite', (t, done) => { | ||
const controllers = Array.from({ length: 2 }, () => new AbortController()); | ||
const composedSignal1 = AbortSignal.any([controllers[0].signal]); | ||
const composedSignalRef = new WeakRef(AbortSignal.any([composedSignal1, controllers[1].signal])); | ||
|
||
const kDependantSignals = Object.getOwnPropertySymbols(controllers[0].signal).find( | ||
(s) => s.toString() === 'Symbol(kDependantSignals)' | ||
); | ||
geeksilva97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
setImmediate(() => { | ||
global.gc(); | ||
|
||
t.assert.strictEqual(composedSignalRef.deref(), undefined); | ||
t.assert.strictEqual(controllers[0].signal[kDependantSignals].size, 2); | ||
t.assert.strictEqual(controllers[1].signal[kDependantSignals].size, 1); | ||
|
||
setImmediate(() => { | ||
t.assert.strictEqual(controllers[0].signal[kDependantSignals].size, 0); | ||
t.assert.strictEqual(controllers[1].signal[kDependantSignals].size, 0); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.