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

terminate called after throwing an instance of 'ivm::FatalRuntimeError'

Open BartTactick opened this issue 2 years ago • 3 comments

Hey there,

I really enjoy this package but i ran into the following error: terminate called after throwing an instance of 'ivm::FatalRuntimeError'

Basicly my setup is like this: The limit for RAM is 20 MB Then i download a file using Axios (stream). After that i want to turn it into a Base64 string with: `const contents = await readFile(path, { encoding: "base64", });

return contents;`

Basicly when i use that with a 100MB file, it will cause the error. How can i catch this error correctly?

Test script:

import { readFile } from "fs/promises";
import { Isolate } from "isolated-vm";

const isolate = new Isolate({
    memoryLimit: 20,
});
const context = isolate.createContextSync();

async function fileToBase64() {
    const contents = await readFile("./100mb.bin", {
        encoding: "base64",
    });

    return contents;
}

(async () => {
    await context.evalClosure(
        `
            fileToBase64 =  async (...args) => {
                return $0.apply(undefined, args, { arguments: { copy: true }, result: { promise: true, copy: true } });
            }
            `,
        [async (path: string) => fileToBase64()],
        {
            arguments: { reference: true },
        }
    );

    const result = await isolate
        .compileScript('fileToBase64();', {
            lineOffset: -1,
        })
        .then((script) => {
            return script.run(context, {
                promise: true,
                reference: true,
            });
        })
        .then(async (e) => await e.copy())
        .catch((e) => ({ error: e }));

    console.log({ result });
})();

BartTactick avatar Feb 22 '22 13:02 BartTactick

When i set the memory limit to 150 MB, it works but i still think it should throw a normal error instead of the fatal runtime error. So basicly whats going wrong is that the return of the function fileToBase64 is bigger than the allowed memory. Therefore crashing the runtime.

BartTactick avatar Feb 22 '22 13:02 BartTactick

In my personal experience, and this varies by nodejs version, limits under 128mb tend to be unreliable. v8 is super thirsty for memory.

laverdet avatar Feb 22 '22 16:02 laverdet

In my personal experience, and this varies by nodejs version, limits under 128mb tend to be unreliable. v8 is super thirsty for memory.

128 MB worked with a 100MB file. But if i throw a 200MB file against it with 128 MB RAM, its the same issue. So i think something should happen to prevent this FatalRuntimeError?

BartTactick avatar Feb 23 '22 09:02 BartTactick