Skip to content

fix(napi): don't panic when the instance cache drops at process exit - #43

Merged
triniwiz merged 1 commit into
mainfrom
fix/napi-exit-tls
Nov 6, 2026
Merged

triniwiz merged 1 commit into
mainfrom
fix/napi-exit-tls

Conversation

@triniwiz

@triniwiz triniwiz commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

On Node, process.exit() with WinRT wrappers still in the napi instance cache panics during shutdown:

thread '<unnamed>' panicked at .../std/src/thread/local.rs:479:25:
cannot access a Thread Local Storage value during or after destruction: AccessError

NAPI_INSTANCE_CACHE's thread-local destructor drops its entries after REF_GRAVEYARD has already been destroyed, and InstanceCacheEntry::drop pushes into the graveyard with .with(). Both that push and evict_instance now use try_with and skip a destroyed thread-local. Nothing is lost: at that point the process is exiting and the deferred napi_ref can't be deleted anyway.

The repro exits cleanly on a build from before #40 and panics on current main, so it became reachable with #40–#42.

Repro (windows-napi on Node):

const { Windows } = require('windows-napi/nswinrt.js');
Windows.Security.Cryptography.CryptographicBuffer.DecodeFromHexString('0a0b');
process.exit(0);

Testing

  • Repro: panics on main, exits cleanly with this change (3/3 runs).
  • windows-napi npm test: 442/442.

With wrappers still cached, process.exit() ran the instance cache's
thread-local destructor after REF_GRAVEYARD's, and the entry's Drop
panicked on the destroyed thread-local ("cannot access a Thread Local
Storage value during or after destruction"). The entry drop and
evict_instance now skip a thread-local that is already gone; the
deferred napi_ref has nothing left to clean up at that point.

Repro: CryptographicBuffer.DecodeFromHexString('0a0b'); process.exit(0)
@coderabbitai

coderabbitai Bot commented Nov 6, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ef35f8dc-bd15-4db3-8c9b-98a3891475f2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@triniwiz
triniwiz merged commit 0dbc5e7 into main Nov 6, 2026
3 checks passed
@triniwiz
triniwiz deleted the fix/napi-exit-tls branch November 6, 2026 10:25
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.

1 participant