lib: fix AbortSignal.any() observed-composite leak#64481
Open
bitpshr wants to merge 1 commit into
Open
Conversation
An observed composite signal (one with an `abort` listener) is added to gcPersistentSignals so it stays alive long enough to fire. When one of its sources aborted, the composite was marked aborted but never removed from that set, so it was retained forever and its WeakRef was never pruned from a long-lived source's kDependantSignals. That set therefore grew without bound. Drop each transitively-aborted dependent from gcPersistentSignals, the same way the directly-aborted signal is already removed, so it can be collected and its dependant entries pruned. Fixes: nodejs#64476 Signed-off-by: Paul Bouchon <mail@bitpshr.net>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes a memory leak in
AbortSignal.any()where an observed composite that follows a long-lived source keeps accumulating in that source'skDependantSignals, even across a forcedglobal.gc().The retainer is
gcPersistentSignals. When you add anabortlistener, the observed composite is added there so it stays alive long enough to fire. When one of its sources aborts,abortSignal()marks the composite aborted and runs its abort steps, but it never removes the composite fromgcPersistentSignals. That set is only pruned when the listener is explicitly removed or a source is GC'd, so with a long-lived source the aborted composite is retained forever, never collected, and itsWeakRefis never pruned from the source's dependant set.An aborted composite can never fire again, so this drops each transitively-aborted dependent from
gcPersistentSignalsin the abort path, mirroring the removal that already happens for the directly-aborted signal a few lines down. Once it's collectable, the existingFinalizationRegistryprunes its entry from every source it followed.The added test observes a long-lived source across many aborted composites and asserts the dependant set drains to zero. It fails before this change (the set stays at the iteration count) and passes after.
Fixes: #64476