Skip to content

refactor(js): unify env lifecycle refcounting#53

Open
nazarhussain wants to merge 1 commit into
mainfrom
nh/lifecycle-cleanup
Open

refactor(js): unify env lifecycle refcounting#53
nazarhussain wants to merge 1 commit into
mainfrom
nh/lifecycle-cleanup

Conversation

@nazarhussain

@nazarhussain nazarhussain commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Motivation

export_module.zig (bare atomic) and js/io.zig (mutex-guarded refcount) duplicate the same retain/release lifecycle. That split is the root cause of both open fixes: the cross-env init race (#31) and the missed cleanup on registration failure (#52).

Description

  • Add lifecycle.SharedResource(T, hooks): a mutex-guarded refcount that runs fallible init under the lock before incrementing, and cleanup after decrementing.
  • Port js/io.zig and the module lifecycle in export_module.zig onto it. No public API changes.

Both fixes hold by construction:

Supersedes #31 and #52.

Testing

New tests in lifecycle.zig cover hook ordering, init-failure rollback, and instance visibility. zig build test: 54/54 pass.

🤖 Generated with Claude Code

Module lifecycle used a bare atomic while js.io() had a mutex-guarded
refcount; the split allowed an init/cleanup race across concurrent
N-API envs and skipped cleanup when export registration failed after a
successful init. SharedResource serializes hooks under a lock and only
increments after init succeeds, so both hold by construction.

Refs #31, #52

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@nazarhussain

Copy link
Copy Markdown
Contributor Author

@GrapeBaBa The changes in #31 and #52 were using the pattern and logic we already have in io.zig so I extracted it out to a common file lifecycle.zig and used across both io.zig and export_module.zig.

See if that seems good to you.

@nazarhussain
nazarhussain requested a review from GrapeBaBa July 17, 2026 07:02
@nazarhussain nazarhussain self-assigned this Jul 17, 2026
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