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

Minimal representation of isolate code hanging the entire process #451

Open
mifopen opened this issue Jan 23, 2024 · 0 comments
Open

Minimal representation of isolate code hanging the entire process #451

mifopen opened this issue Jan 23, 2024 · 0 comments

Comments

@mifopen
Copy link

mifopen commented Jan 23, 2024

For me, the following code hangs the outer node process in 60% of cases. In other cases, it finishes with a top-level error about the unhandled promise. By slightly modifying the code, I can also come up with a version that crashes the outer process with the same top-level error without an opportunity to catch it.

import ivm from 'isolated-vm';

const isolate = new ivm.Isolate();
const context = isolate.createContextSync();

context.evalClosureSync(
    `
    globalThis.fn = async () => {
        await new Promise(res => {
            $1.applySync(undefined, [new $0.Reference(res), 0]);
        });
        new Promise((res, rej) => { rej(new Error("from promise")); });
    };
`,
    [
        /* $0 */
        ivm,

        /* $1 setTimeout */
        new ivm.Reference((cb, ms) => {
            setTimeout(() => {
                cb.apply(undefined, []);
            }, ms);
        }),
    ],
);

const code = `fn()`;

console.log('before first');
try {
    await context.eval(code, { promise: true });
} catch (e) {
    console.log('error first');
}
console.log('after first');

console.log('before second');
try {
    await context.eval(code, { promise: true });
} catch (e) {
    console.log('error second');
}

console.log('after second');

console.log('before third');
try {
    await context.eval(code, { promise: true });
} catch (e) {
    console.log('error third');
}
console.log('after third');

console.log('before fourth');
try {
    await context.eval(code, { promise: true });
} catch (e) {
    console.log('error fourth');
}
console.log('after fourth');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant