fix: preserve object identity across the host boundary (#4)#84
Merged
Conversation
A host function's return value is disposed by the VM once consumed. The VMMap retains marshalled object handles for identity while sync is on, but the returned handle WAS the map's own handle, so the VM disposed it and the map entry went stale: marshalling the same value twice produced two distinct VM objects (foo !== id(foo), get() !== get()) and leaked the orphaned sibling. Hand the VM a dup of the handle when the map retains it (prepareReturn), keeping the map's copy alive. Identity now holds across round-trips and repeated returns; with syncEnabled:false nothing is retained, so behaviour is unchanged (no dup). Host-side mutations of an already-marshalled object are no longer picked up by re-marshalling (the value is identity-cached); arena.sync remains the way to propagate host writes. Updated the edge 'getter' test accordingly and un-commented its ctx.dispose() now that the leak is gone.
…outs Identity assertions don't need the debug-sync runtime's leak detection (that is covered by remarshalleak.test.ts), so run them on the default runtime — under coverage the debug-sync variant pushed each in-VM call past the 30s timeout in CI. The remaining debug-sync leak tests get a 90s timeout, matching the existing slow 'many newFunction' edge test.
Contributor
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes part of #4.
Problem
Reference identity was not preserved across the host boundary in the default (sync-on) mode:
Root cause
A host function's return value is disposed by the VM once consumed. The
VMMapretains marshalled object handles for identity while sync is on — but the handle returned to the VM was the map's own handle, so the VM disposed it and the entry went stale. Marshalling the same value again then missed the map, created a fresh VM object (breaking===) and orphaned the sibling handle (the leak fixed in #83 was the same root mechanism surfacing as a dispose-time abort).Fix
Hand the VM a
dup()of the handle when theVMMapretains it (prepareReturn), keeping the map's copy alive.findthen returns a live handle on the next marshal, so identity holds.syncEnabled: withsyncEnabled: falsenothing is retained, so no dup — behaviour unchanged.Behavioural note (decided with maintainer)
Identity retention means a host object marshalled once is identity-cached: a later host-side mutation is not picked up by re-marshalling.
arena.sync(obj)remains the supported way to propagate host writes to the VM. Theedge.test.ts > gettertest documented the old re-marshal-each-time behaviour (which was also what leaked); it's updated to the new semantics, plus a newgetter (synced)test shows live tracking viaarena.sync. Itsctx.dispose()is un-commented now that the leak is gone.Tests
New
src/identity.test.tspins identity (round-trip, repeated returns, retained refs, and the sync-off negative case). Full suite: 168 passing, typecheck + lint clean.