isolated-vm icon indicating copy to clipboard operation
isolated-vm copied to clipboard

Asynchronously instantiating WebAssembly code "stops" task

Open y21 opened this issue 2 years ago • 0 comments

const ivm = require('isolated-vm');

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

function load() {
    return ctx.eval(`
    const u8 = new Uint8Array([0,97,115,109,1,0,0,0,1,7,1,96,2,127,127,1,127,3,2,1,0,5,3,1,0,17,7,16,2,6,109,101,109,111,114,121,2,0,3,97,100,100,0,0,10,9,1,7,0,32,0,32,1,106,11,11,10,1,0,65,128,128,192,0,11,1,4,0,131,1,9,112,114,111,100,117,99,101,114,115,2,8,108,97,110,103,117,97,103,101,1,4,82,117,115,116,0,12,112,114,111,99,101,115,115,101,100,45,98,121,3,5,114,117,115,116,99,37,49,46,54,48,46,48,45,110,105,103,104,116,108,121,32,40,50,50,101,52,57,49,97,99,55,32,50,48,50,50,45,48,49,45,49,51,41,6,119,97,108,114,117,115,6,48,46,49,57,46,48,12,119,97,115,109,45,98,105,110,100,103,101,110,18,48,46,50,46,56,48,32,40,52,99,97,97,57,56,49,54,53,41]);
    WebAssembly.instantiate(u8).then(x => x.instance.exports.add(5, 6));
    `, {
        promise: true,
    });
}

setImmediate(async () => {
    console.log('hi');
    await load();
    console.log('bye'); // never executes
});

The last log line that prints "bye" never executes and the callback function exits.

This also happens with invalid wasm modules (or any buffer for that matter). The wasm module in the example is the smallest example wasm I could come up with and just exposes a add(i32, i32) -> i32 function. This doesn't happen if it is instantiated synchronously (i.e. with new WebAssembly.Instance(new WebAssembly.Module(...))) and compiles as expected, and "bye" gets printed.

Node.js: v16.15.1 v8: 9.4.146.24-node.21 ivm: 4.4.1 Running under WSL2, if that matters.

y21 avatar Jun 05 '22 21:06 y21