nodejs / nodejs/node

AbortSignal.any memory leak when circular dependencies

Open
#57,584 14 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

abortcontroller
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

23.10.0

Platform
6.8.0-55-generic #57-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 12 23:42:21 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Subsystem

No response

What steps will reproduce the bug?
class Test {
  private abortController = new AbortController()

  public test(cb: (abortSignal: AbortSignal) => void, abortSignal: AbortSignal) {
    const signal = AbortSignal.any([abortSignal, this.abortController.signal])

    cb(signal)
  }
}

const registry = new FinalizationRegistry((heldValue) => {
  console.log(`${heldValue} has been collected`)
})

;(() => {
  const test = new Test()
  registry.register(test, 'test')

  const abortController = new AbortController()

  test.test((abortSignal) => {
    abortSignal.addEventListener('abort', () => {
      console.log(test)
    })
  }, abortController.signal)
})()

global.gc?.()

setTimeout(() => {
  console.log('the end')
}, 2000)

run with node --expose-gc test.ts

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior? Why is that the expected behavior?

the output should be

test has been collected
the end

because the test object is not referenced anymore

What do you see instead?

output:

the end

the test object is not garbage collected

Additional information

The issues seems to be caused by the cyclic dependency: test object => abortController field => abortSignal => AbortSignal.any result => cb function => test object

AbortSignal.any seems to prevent node from garbage collecting them all at once

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied reproduction with node --expose-gc and inspect the AbortSignal.any, AbortController, and FinalizationRegistry interaction described in the issue. Done means the test object is collected and the output includes both “test has been collected” and “the end”.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.