Skip to content

Make __instance_signal and __instance_terminated fully indepedent #6270

Merged
guybedford merged 3 commits into
mainfrom
logan/instance-lifetime-tracking
Apr 11, 2026
Merged

Make __instance_signal and __instance_terminated fully indepedent #6270
guybedford merged 3 commits into
mainfrom
logan/instance-lifetime-tracking

Conversation

@logan-gatlin

@logan-gatlin logan-gatlin commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

Currently, __instance_signal is ignored if __instance_terminated is not present. This is because registering a signal creates a strong reference to the instance's memory, and we need some indication of when it is safe to free it. Keeping a weak reference to the memory is not an option, since weak references are not safe to use in a signal handler.
The new solution is to keep a weak reference to the instance alongside our strong reference to its memory. Then in the GC prologue, in addition to dropping references to memories that have __instance_terminated set, we will also drop memories when our instance weakref is now dead. WASM memories have independent lifetimes from the instance they were spawned from. This is because the instance that created a memory may die while another instance that still lives has imported its memory. Therefore, our strong reference to the memory will not prevent the instance from being GC'd.

@ask-bonk

ask-bonk Bot commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

The old code had kj::mv(signalOffset) which was unnecessary for a kj::Maybe<uint32_t> (trivially copyable). The new code just copies both, which is fine.

I've done a thorough review of all the changes. The code is well-structured, safe, and properly tested. I don't see any actionable issues — no logic bugs, no safety issues, no missing compat flags (since it's gated by an existing autogate), and no backward compatibility concerns.

LGTM

github run

@codspeed-hq

codspeed-hq Bot commented Mar 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 70 untouched benchmarks
⏩ 129 skipped benchmarks1


Comparing logan/instance-lifetime-tracking (501f6ec) with main (696113e)

Open in CodSpeed

Footnotes

  1. 129 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@logan-gatlin logan-gatlin marked this pull request as ready for review March 9, 2026 18:47
@logan-gatlin logan-gatlin requested review from a team as code owners March 9, 2026 18:47
Comment thread src/workerd/io/tracked-wasm-instance.h Outdated
@guybedford guybedford requested review from a team as code owners April 10, 2026 00:50
@guybedford guybedford force-pushed the logan/instance-lifetime-tracking branch 4 times, most recently from 54846e2 to 634b63a Compare April 10, 2026 02:48

@guybedford guybedford 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.

To update on the potential memory concerns, I created a negative test in Edgeworker today which disabled the GC preamble and verified WebAssembly.Instance finalization in a GC loop. I was able to verify that this test resulted in an OOM - so the backing store references we hold are strong. Turning the GC preamble back on, they were then cleared again.

That is, the memory model here is sound in both directions - the kj::Array strong references are sound, and the memory collection model is sound.

This is great work, and ready to ship, thank you @logan-gatlin!

logan-gatlin and others added 3 commits April 10, 2026 14:11
Remove the terminatedByteOffset fast-path from shouldRetain() since GC-based
cleanup via the weak instanceRef is sufficient for all entry types. Remove
three redundant per-permutation shouldRetain/filter tests that now tested
identical logic; the mixed list test covers shouldRetain across all permutations.
@guybedford guybedford force-pushed the logan/instance-lifetime-tracking branch from ceb8ee6 to 501f6ec Compare April 10, 2026 21:12

@npaun npaun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM; Guy's changes are a straightforward removal of an unneeded fast path.

@guybedford guybedford merged commit d9cfdd0 into main Apr 11, 2026
24 of 27 checks passed
@guybedford guybedford deleted the logan/instance-lifetime-tracking branch April 11, 2026 03:17
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.

3 participants