Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ comptime {
}
```

Module initialization is transactional:

- If `init` fails, the environment refcount is restored and `cleanup` is not called.
- If `init` succeeds but module registration fails, the environment refcount is restored and
`cleanup` is called.
- After successful registration, `cleanup` runs when the environment exits.

This enables safe shared-state initialization for worker thread scenarios.

---
Expand Down
24 changes: 18 additions & 6 deletions src/js/export_module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,27 @@ pub fn exportModule(comptime Module: type, comptime options: anytype) void {
};

var prev_refcount: u32 = 0;
var init_succeeded = false;
if (has_lifecycle) {
prev_refcount = State.env_refcount.fetchAdd(1, .monotonic);
errdefer if (!cleanup_hook_registered) {
_ = State.env_refcount.fetchSub(1, .acq_rel);
};

if (has_init) {
try options.init(prev_refcount);
}
errdefer if (!cleanup_hook_registered) {
if (has_lifecycle) {
const prev_refcount_rollback = State.env_refcount.fetchSub(1, .acq_rel);
std.debug.assert(prev_refcount_rollback > 0);
const new_refcount = prev_refcount_rollback - 1;

if (has_cleanup) {
if (init_succeeded) {
options.cleanup(new_refcount);
}
}
}
};

if (has_init) {
try options.init(prev_refcount);
init_succeeded = true;
}

_ = try registerDecls(Module, env, module, 0);
Expand Down
Loading