Skip to content
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): avoid slotToVal memory leak for watched promises #10758

Merged
merged 2 commits into from
Dec 21, 2024
Merged
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
16 changes: 13 additions & 3 deletions packages/swingset-liveslots/src/watchedPromises.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,23 @@ export function makeWatchedPromiseManager({
} else {
watchedPromiseTable.init(vpid, harden([[watcher, ...args]]));

promiseRegistrations.init(vpid, p);

// pseudoThen registers a settlement callback that will remove
// this promise from promiseRegistrations and
// watchedPromiseTable. To avoid triggering
// https://github.com/Agoric/agoric-sdk/issues/10757 and
// preventing slotToVal cleanup, the `pseudoThen()` should
// precede `maybeExportPromise()`. This isn't foolproof, but
// does mitigate in advance of a proper fix. See #10756 for
// details of this particular mitigation, and #10757 for the
// deeper bug.
pseudoThen(p, vpid);

// Ensure that this vat's promises are rejected at termination.
if (maybeExportPromise(vpid)) {
syscall.subscribe(vpid);
}

promiseRegistrations.init(vpid, p);
pseudoThen(p, vpid);
}
});
}
Expand Down
42 changes: 42 additions & 0 deletions packages/swingset-liveslots/test/watch-promise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import test from 'ava';

import { Far } from '@endo/marshal';
import { makePromiseKit } from '@endo/promise-kit';
import { setupTestLiveslots } from './liveslots-helpers.js';

const build = vatPowers => {
const { VatData } = vatPowers;
const { makeKindHandle, defineDurableKind, watchPromise } = VatData;

const kh = makeKindHandle('handler');
const init = () => ({});
const behavior = {
onFulfilled: _value => 0,
onRejected: _reason => 0,
};
const makeHandler = defineDurableKind(kh, init, behavior);

return Far('root', {
async run() {
const pr = makePromiseKit();
const handler = makeHandler();
watchPromise(pr.promise, handler);
pr.resolve('ignored');
},
});
};

test('watched local promises should not leak slotToVal entries', async t => {
const { dispatchMessage, testHooks } = await setupTestLiveslots(
t,
build,
'vatA',
);
const { slotToVal } = testHooks;
const initial = slotToVal.size;

await dispatchMessage('run');
t.is(slotToVal.size, initial);
await dispatchMessage('run');
t.is(slotToVal.size, initial);
});
Loading