Skip to content

fix(native): free leaked keys and lists, use per-call invoker - #119

Merged
TimoGlastra merged 2 commits into
mainfrom
TimoGlastra/fix-native-memory-leaks
Nov 17, 2026
Merged

TimoGlastra merged 2 commits into
mainfrom
TimoGlastra/fix-native-memory-leaks

Conversation

@TimoGlastra

Copy link
Copy Markdown
Contributor
  • Node.js: free keys and entry lists when their handle is garbage collected, as a safety net for handles that are never freed.
  • Node.js: free string lists even when reading them throws, and share the reading code between both callers.
  • React Native: take the invoker from the call state instead of a global, so callbacks run on the runtime that made the call.
  • shared: free the keys already read when KeyEntryList.toArray throws.

- Node.js: free keys and entry lists when their handle is garbage
  collected, as a safety net for handles that are never freed.
- Node.js: free string lists even when reading them throws, and share
  the reading code between both callers.
- React Native: take the invoker from the call state instead of a
  global, so callbacks run on the runtime that made the call.
- shared: free the keys already read when KeyEntryList.toArray throws.

Signed-off-by: TimoGlastra <timo@animo.id>
@TimoGlastra
TimoGlastra requested a review from a team as a code owner November 17, 2026 15:08
@TimoGlastra
TimoGlastra requested a review from genaris November 17, 2026 15:08
@changeset-bot

changeset-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ce5f448

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@openwallet-foundation/askar-nodejs Patch
@openwallet-foundation/askar-react-native Patch
@openwallet-foundation/askar-shared Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@genaris genaris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some comments regarding the leak tests after running in my MacBook, but otherwise LGTM.

})

// A key that is never freed is about 160 bytes, so this grew by about 16 kB per iteration without the safety net
test('frees keys that are garbage collected without being freed', async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I ran this test against main's NodeJSAskar.ts (no FinalizationRegistry) and it still passes on macOS: rss -88801 bytes per iteration. The Math.min over rounds filters noise that goes up, but RSS also drops by several MB in some rounds, which hides the ~1.6 MB a round of leaked keys adds.

Since what we want to assert is "the finalizer calls askar_key_free for every key that is collected", a deterministic check would be stronger and much faster than the ~7 s this takes now, e.g. spy on askar_key_free on the native library object, generate N keys without freeing them, run memoryAfterGarbageCollection(), and expect(spy).toHaveBeenCalledTimes(N). That fails reliably without the safety net and doesn't depend on allocator behaviour.

for (let i = 0; i < ROUNDS; i++) {
const after = await round()
for (const kind of ['rss', 'external', 'arrayBuffers'] as const) {
growth[kind] = Math.min(growth[kind], (after[kind] - before[kind]) / iterations)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Taking the minimum across rounds means a single round where the allocator returns memory to the OS makes the test pass regardless of the other rounds (see the numbers above: both tests report negative growth here). Comparing the first and last measurement over all rounds, or taking the median, keeps the noise filtering without that blind spot.

Signed-off-by: TimoGlastra <timo@animo.id>
@TimoGlastra
TimoGlastra enabled auto-merge (squash) November 17, 2026 18:35
@TimoGlastra
TimoGlastra merged commit a774212 into main Nov 17, 2026
12 checks passed
@TimoGlastra
TimoGlastra deleted the TimoGlastra/fix-native-memory-leaks branch November 17, 2026 19:12
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.

2 participants