Repository navigation
fix(native): free leaked keys and lists, use per-call invoker - #119
Conversation
TimoGlastra
commented
Nov 17, 2026
- 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>
🦋 Changeset detectedLatest commit: ce5f448 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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
left a comment
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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>