Skip to content

fix(napi): memory leak in PromiseRaw cleanup callback#2995

Merged
Brooooooklyn merged 4 commits intomainfrom
claude/fix-issue-2926-011CUpQ9fDvQcsz9TAcesAnJ
Nov 6, 2025
Merged

fix(napi): memory leak in PromiseRaw cleanup callback#2995
Brooooooklyn merged 4 commits intomainfrom
claude/fix-issue-2926-011CUpQ9fDvQcsz9TAcesAnJ

Conversation

@Brooooooklyn
Copy link
Member

@Brooooooklyn Brooooooklyn commented Nov 5, 2025

This commit fixes a memory leak that occurred when converting a Promise from an Unknown object, specifically manifesting in Deno environments.

The issue was in the promise_callback_finalizer function, which had a type mismatch when cleaning up callback allocations:

  1. The callback was stored as Box<(Cb, *mut bool)> (a tuple containing the callback and an executed flag pointer)
  2. The finalizer incorrectly cast it as Box<Cb> when trying to free it
  3. This caused improper deallocation and memory leaks

The fix ensures:

  • The executed flag allocation is always properly cleaned up
  • The callback tuple is cast to the correct type (Cb, *mut bool) before being dropped
  • All allocations are properly freed in both execution paths (callback executed or not executed)

Fixes #2926


Note

Fix memory leak by always freeing the executed-flag and correctly dropping (Cb, *mut bool) when the Promise callback was never executed.

  • Memory management in crates/napi/src/bindgen_runtime/js_values/promise_raw.rs:
    • Update promise_callback_finalizer to always free the executed flag (*mut bool).
    • When the callback was not executed, correctly drop finalize_hint as Box<(Cb, *mut bool)> instead of Box<Cb> to free all allocations.

Written by Cursor Bugbot for commit e1cfa4c. This will update automatically on new commits. Configure here.

This commit fixes a memory leak that occurred when converting a Promise
from an Unknown object, specifically manifesting in Deno environments.

The issue was in the `promise_callback_finalizer` function, which had
a type mismatch when cleaning up callback allocations:

1. The callback was stored as `Box<(Cb, *mut bool)>` (a tuple containing
   the callback and an executed flag pointer)
2. The finalizer incorrectly cast it as `Box<Cb>` when trying to free it
3. This caused improper deallocation and memory leaks

The fix ensures:
- The executed flag allocation is always properly cleaned up
- The callback tuple is cast to the correct type `(Cb, *mut bool)`
  before being dropped
- All allocations are properly freed in both execution paths (callback
  executed or not executed)

Fixes #2926
@Brooooooklyn Brooooooklyn requested a review from Copilot November 5, 2025 09:04
@graphite-app
Copy link

graphite-app bot commented Nov 5, 2025

How to use the Graphite Merge Queue

Add the label ready-to-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a memory management bug in the promise callback finalizer where the cleanup logic had incorrect type casts. The finalizer now properly cleans up both the execution flag allocation and the callback tuple.

  • Fixed the finalizer to always clean up the executed flag allocation instead of conditionally
  • Corrected the type cast for the callback cleanup from Cb to (Cb, *mut bool) to match the actual data structure
  • Added clarifying comments explaining the cleanup logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Brooooooklyn
Copy link
Member Author

cursor review

@Brooooooklyn
Copy link
Member Author

@codex review

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no bugs!


@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Brooooooklyn Brooooooklyn changed the title Fix NAPI-RS issue 2926 fix(napi): memory leak in PromiseRaw cleanup callback Nov 6, 2025
@Brooooooklyn Brooooooklyn merged commit 52c0da5 into main Nov 6, 2025
69 of 71 checks passed
@Brooooooklyn Brooooooklyn deleted the claude/fix-issue-2926-011CUpQ9fDvQcsz9TAcesAnJ branch November 6, 2025 09:07
@github-actions github-actions bot mentioned this pull request Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak when converting a Promise from an Unknown

3 participants