Skip to content

Comments

src: avoid JS callback for deprecated Promise resolve-after-resolved events#61960

Open
jorgitin02 wants to merge 1 commit intonodejs:mainfrom
jorgitin02:fix/51452-promise-race-memory-leak
Open

src: avoid JS callback for deprecated Promise resolve-after-resolved events#61960
jorgitin02 wants to merge 1 commit intonodejs:mainfrom
jorgitin02:fix/51452-promise-race-memory-leak

Conversation

@jorgitin02
Copy link

Summary

  • return early in src/node_task_queue.cc for kPromiseResolveAfterResolved and kPromiseRejectAfterResolved
  • avoid invoking the JS promise reject callback for event types that JS-side logic already ignores
  • add test/parallel/test-promise-race-memory-leak.js to cover immediate-resolution Promise.race/Promise.any behavior and nearby safety checks

Context

Issue: #51452
The original report was against older Node versions (v21.5.0). On current main, reproduction is less direct, but this change keeps C++ behavior aligned with existing JS semantics (deprecated multipleResolves path is ignored) and removes unnecessary callback/context handling for these events.

Testing

  • python3 tools/test.py test/parallel/test-promise-race-memory-leak.js
  • python3 tools/test.py --repeat=5 test/parallel/test-promise-race-memory-leak.js
  • python3 tools/test.py test/parallel/test-promise*
  • python3 tools/test.py test/parallel/test-promise-reject-callback-exception.js
  • make lint-cpp
  • make lint-js (skipped in this environment due local binary resolution: /Users/jorgitin/Documents/projects/node/node is a directory)

Do not call JS promise reject callback for kPromiseResolveAfterResolved
and kPromiseRejectAfterResolved events. These events are triggered when
a promise is resolved/rejected after already being resolved, and notifying
JS land causes memory leaks in tight loops with immediately-resolving
promises (e.g., Promise.race, Promise.any).

While the JS-side handler in lib/internal/process/promises.js already
ignores these events (multipleResolves was deprecated and removed), the
C++ callback was still being invoked, causing unnecessary async context
manipulation and keeping promise object references alive.

Fixes: nodejs#51452
Signed-off-by: jorge guerrero <grrr.jrg@gmail.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants