Skip to content

Commit

Permalink
feat(xsnap): isReady() eliminates need for .evaluate('null')
Browse files Browse the repository at this point in the history
this lets us ensure that snapshot loading is finished without
affecting the XS engine heap state.
  • Loading branch information
dckc committed Jun 29, 2021
1 parent 30001bd commit 355e626
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions packages/xsnap/src/xsnap.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ ExitCode main(int argc, char* argv[])
if (snapshot.stream) {
machine = fxReadSnapshot(&snapshot, "xsnap", NULL);
fclose(snapshot.stream);
// collect after restore from snapshots for consistency
fx_gc(machine);
}
else
snapshot.error = errno;
Expand Down Expand Up @@ -372,6 +370,9 @@ ExitCode main(int argc, char* argv[])
char command = *nsbuf;
// fprintf(stderr, "command: len %d %c arg: %s\n", nslen, command, nsbuf + 1);
switch(command) {
case 'R': // isReady
fxWriteNetString(toParent, ".", "", 0);
break;
case '?':
case 'e':
xsBeginCrank(machine, gxCrankMeteringLimit);
Expand Down
13 changes: 13 additions & 0 deletions packages/xsnap/src/xsnap.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ export function xsnap(options) {
return Promise.race([vatCancelled, result]);
}

/**
* @returns {Promise<void>}
*/
async function isReady() {
const result = baton.then(async () => {
await messagesToXsnap.next(encoder.encode(`R`));
await runToIdle();
});
baton = result.catch(() => {});
return Promise.race([vatCancelled, result]);
}

/**
* @param {Uint8Array} message
* @returns {Promise<RunResult<Uint8Array>>}
Expand Down Expand Up @@ -305,6 +317,7 @@ export function xsnap(options) {
return freeze({
issueCommand,
issueStringCommand,
isReady,
close,
terminate,
evaluate,
Expand Down
2 changes: 1 addition & 1 deletion packages/xsnap/test/test-snapstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ test('create SES worker, save, restore, resume', async t => {

const worker = await store.load(h, async snapshot => {
const xs = xsnap({ name: 'ses-resume', snapshot, os: osType(), spawn });
await xs.evaluate('0');
await xs.isReady();
return xs;
});
t.teardown(() => worker.close());
Expand Down

0 comments on commit 355e626

Please sign in to comment.