-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(liveslots): retain WeakRefs to voAware collections #7552
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b063ef
chore(liveslots): give VRM access to WeakRef
warner f357672
chore(liveslots): add weakRefFor() to mock GC tools
warner 3ea1a2c
chore(liveslots): expose registerDroppedCollection instead of raw FR
warner 3935723
fix(liveslots): retain WeakRefs to voAware collections
warner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
61 changes: 61 additions & 0 deletions
61
packages/swingset-liveslots/test/test-dropped-collection-weakrefs.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import test from 'ava'; | ||
import '@endo/init/debug.js'; | ||
import { Far } from '@endo/marshal'; | ||
import { makeLiveSlots } from '../src/liveslots.js'; | ||
import { kser } from './kmarshal.js'; | ||
import { buildSyscall } from './liveslots-helpers.js'; | ||
import { makeStartVat } from './util.js'; | ||
import { makeMockGC } from './mock-gc.js'; | ||
|
||
test('droppedCollectionWeakRefs', async t => { | ||
const { syscall } = buildSyscall(); | ||
const gcTools = makeMockGC(); | ||
let myVOAwareWeakMap; | ||
let myVOAwareWeakSet; | ||
|
||
// In XS, WeakRefs are treated as strong references except for | ||
// forced GC, which reduces our sensitivity to GC timing (which is | ||
// more likely to change under small upgrades of the engine). We'd | ||
// like this improvement for objects tracked by | ||
// FinalizationRegistries too. Liveslots has two FRs, | ||
// `vreffedObjectRegistry` (whose entries all have WeakRefs in | ||
// slotToVal), and `droppedCollectionRegistry` (in the VRM). | ||
// | ||
// `droppedCollectionRegistry` tracks the VO-aware replacements for | ||
// WeakMap/Set that we impose on userspace, and these do not have | ||
// vref identities, so they will never appear in slotToVal or | ||
// valToSlot, so we need to create new WeakRefs to trigger the | ||
// retain-under-organic-GC behavior. Those WeakRefs are held in the | ||
// FR context/"heldValue" until it fires. | ||
|
||
function buildRootObject(vatPowers) { | ||
const { WeakMap, WeakSet } = vatPowers; | ||
// creating a WeakMap/Set should put it in droppedCollectionWeakRefs | ||
myVOAwareWeakMap = new WeakMap(); | ||
myVOAwareWeakSet = new WeakSet(); | ||
return Far('root', {}); | ||
} | ||
|
||
const makeNS = () => ({ buildRootObject }); | ||
const ls = makeLiveSlots(syscall, 'vatA', {}, {}, gcTools, undefined, makeNS); | ||
const { dispatch, testHooks } = ls; | ||
await dispatch(makeStartVat(kser())); | ||
|
||
const wmWeakRef = gcTools.weakRefFor(myVOAwareWeakMap); | ||
const wsWeakRef = gcTools.weakRefFor(myVOAwareWeakSet); | ||
|
||
// we snoop inside our mock FinalizationRegistry to get the context | ||
const fr = testHooks.getDroppedCollectionRegistry(); | ||
t.is(fr.registry.get(myVOAwareWeakMap).wr, wmWeakRef); | ||
t.is(fr.registry.get(myVOAwareWeakSet).wr, wsWeakRef); | ||
|
||
gcTools.kill(myVOAwareWeakMap); | ||
gcTools.flushAllFRs(); | ||
t.falsy(fr.registry.has(myVOAwareWeakMap)); | ||
t.truthy(fr.registry.has(myVOAwareWeakSet)); // not dead yet | ||
|
||
gcTools.kill(myVOAwareWeakSet); | ||
gcTools.flushAllFRs(); | ||
t.falsy(fr.registry.has(myVOAwareWeakMap)); | ||
t.falsy(fr.registry.has(myVOAwareWeakSet)); // dead now | ||
}); |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So little tangent on what optimizations engines are allowed or not. Technically, since
wr
is never observed by the program, the JS engine is allowed to not keep it in theheldValue
, which means that the WeakRef would actually not exist in the first place.If we wanted to be as close to the spec rules as possible, we would need to actually try to deref the weakref and assert it's empty (or even better, log the target if not empty, causing an actual observation). Obviously no engine goes that far in its optimizations, certainly not XS, and the destructuring dropping the wr is in fact completely sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, the engine is allowed to inspect the callback's code to determine that? hardcore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The engine is allowed to do anything as long as it's not observable to the program. If a property of an object is never used, it doesn't have to store it ;) But in practice engines are not omniscient enough.