Skip to content

Commit

Permalink
fix: handle transient 0 refCounts correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Apr 21, 2021
1 parent 215dfb9 commit 9975d75
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/SwingSet/src/vats/comms/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ export function makeState(syscall, identifierBase = 0) {
if (lref && parseLocalSlot(lref).type === 'promise') {
const refCount = Number(store.get(`${lref}.refCount`)) + 1;
// cdebug(`++ ${lref} ${tag} ${refCount}`);
if (refCount === 1 && deadLocalPromises.has(lref)) {
// Oops, turns out the zero refCount was a transient
deadLocalPromises.delete(lref);
}
store.set(`${lref}.refCount`, `${refCount}`);
}
}
Expand All @@ -195,13 +199,29 @@ export function makeState(syscall, identifierBase = 0) {
// cdebug(`-- ${lref} ${tag} ${refCount}`);
store.set(`${lref}.refCount`, `${refCount}`);
if (refCount === 0) {
// If we are still in the middle of a resolve operation, and this lref
// is an ancillary promise that was briefly added to the decider's
// clist, we'll reach here when we retire that short-lived
// identifier. However the lref is still in play: the resolution
// function hasn't finished running, and we'll add the lref to the
// subscriber's clist momentarily, where it will live until we get an
// ack and can retire it from that clist too. We add the lref to the
// maybe-dead set, but we do not trigger GC until the entire comms
// dispatch is complete and any ancillary promises are safely referenced
// by their subscribers clists.
deadLocalPromises.add(lref);
return true;
}
}
return false;
}

/**
* Delete any local promises that have zero references.
*
* Note that this should only be called *after* all work for a crank is done,
* because transient zero refCounts are possible during the middle of a crank.
*/
function purgeDeadLocalPromises() {
if (enableLocalPromiseGC) {
for (const lpid of deadLocalPromises.values()) {
Expand Down

0 comments on commit 9975d75

Please sign in to comment.