abort_controller: fix AbortSignal.any() memory leak #60185
Closed
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.
Description
This PR fixes a critical memory leak in
AbortSignal.any()
that causes unbounded memory growth in production environments, growing from 36 MB to over 1.3 GB and leading to OOM crashes.Fixes: #54614
Root Cause
When
AbortSignal.any()
is called in tight loops without attaching abort listeners, signals are prematurely added to thegcPersistentSignals
SafeSet to prevent garbage collection. However, without listeners, these signals are NEVER removed, causing the set to grow unbounded.The code was adding signals to
gcPersistentSignals
in two places:This prevented V8 from garbage collecting signals even when they had no listeners and were no longer referenced.
The Fix
Removed premature addition of signals to
gcPersistentSignals
. Signals are now only added when abort listeners are actually registered, which is already correctly handled by thekNewListener
method (lines 312-326).Before:
After:
The
kNewListener
method already handles GC prevention correctly when listeners are attached:Evidence
Memory Leak Confirmed (Node.js v22.18.0)
Running reproduction with
--max-old-space-size=512
:Growth Rate: ~1.5 MB per 1,000 iterations
Result: OOM crash at 300k iterations with 512 MB heap
Test Case
Added
test/parallel/test-abortcontroller-any-memory-leak.js
which tests:All tests assert memory growth stays under reasonable thresholds (50-100 MB for 50k-100k iterations).
Without fix: Memory grows hundreds to thousands of MBs
With fix: Stable memory within GC variance
Performance Impact
Before:
gcPersistentSignals
immediatelyAfter:
Backward Compatibility
No breaking changes. This fix only changes WHEN signals are added to
gcPersistentSignals
:AbortSignal.any()
, even without listenerskNewListener
)Behavior with abort listeners remains identical. Signals are still prevented from GC when needed.
Checklist
make -j4 test
(Build and run all tests)