Skip to content

Commit

Permalink
fix(ses): avoid cache corruption when execute() throws
Browse files Browse the repository at this point in the history
* fix(ses): avoid cache corruption when execute() throws

* test(compartment-mapper): cover catching require errors

* fix(ses): ensure execute() runs once and error thrown from it is persisted by existing memoization

* fix(ses): protect error caching from falsy throws
  • Loading branch information
naugtur committed Apr 5, 2022
1 parent ce0d672 commit 1d9c17b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 9 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions packages/compartment-mapper/test/test-error-handling.js
Expand Up @@ -67,3 +67,31 @@ scaffold(
shouldFailBeforeArchiveOperations: true,
},
);

scaffold(
'fixtures-error-handling / catch',
test,
fixtureUrl('fixtures-error-handling/node_modules/catch/main.js'),
assertFixture,
fixtureAssertionCount,
{
onError: (t, { error, _title }) => {
t.regex(error.message, /obviouslymissing/);
},
shouldFailBeforeArchiveOperations: false,
},
);

scaffold(
'fixtures-error-handling / throw falsy',
test,
fixtureUrl('fixtures-error-handling/node_modules/what-the-falsy/main.js'),
assertFixture,
fixtureAssertionCount,
{
onError: (t, { error, _title }) => {
t.assert(!error);
},
shouldFailBeforeArchiveOperations: false,
},
);
29 changes: 20 additions & 9 deletions packages/ses/src/module-instance.js
Expand Up @@ -17,6 +17,7 @@ import {
keys,
mapGet,
weakmapGet,
reflectHas,
} from './commons.js';
import { compartmentEvaluate } from './compartment-evaluate.js';

Expand Down Expand Up @@ -79,20 +80,30 @@ export const makeThirdPartyModuleInstance = (
};
}

let activated = false;
const localState = {
activated: false,
};
return freeze({
notifiers,
exportsProxy,
execute() {
if (!activated) {
if (reflectHas(localState, 'errorFromExecute')) {
throw localState.errorFromExecute;
}
if (!localState.activated) {
activate();
activated = true;
// eslint-disable-next-line @endo/no-polymorphic-call
staticModuleRecord.execute(
proxiedExports,
compartment,
resolvedImports,
);
localState.activated = true;
try {
// eslint-disable-next-line @endo/no-polymorphic-call
staticModuleRecord.execute(
proxiedExports,
compartment,
resolvedImports,
);
} catch (err) {
localState.errorFromExecute = err;
throw err;
}
}
},
});
Expand Down

0 comments on commit 1d9c17b

Please sign in to comment.