Repository navigation
Description
Version
v26.12.1, v24.22.3, v22.23.3
Platform
Microsoft Windows NT 10.0.19045.0 x64 (v26.12.1)
Linux x64, official Docker images, 2 CPUs (v24.22.3, v22.23.3)
Subsystem
vm
What steps will reproduce the bug?
const vm = require('node:vm');
process.on('unhandledRejection', () => {});
const bodies = {
'Promise.resolve(1)': 'Promise.resolve(1);',
'Promise.reject(1)': 'Promise.reject(1);',
'async function that throws': '(async () => { throw 1; })();',
};
(async () => {
console.log(process.version, process.platform, process.arch);
for (const [name, body] of Object.entries(bodies)) {
let lost = 0;
let slowest = 0;
for (let i = 0; i < 25; i++) {
const context = vm.createContext({ finished: false });
// The loop ends by itself after 500 ms, so a lost stop does not hang this process.
const code = `const end = Date.now() + 500; while (Date.now() < end) { ${body} } finished = true;`;
const start = Date.now();
try {
vm.runInContext(code, context, { timeout: 50 });
} catch (err) {
if (err.code !== 'ERR_SCRIPT_EXECUTION_TIMEOUT') throw err;
}
slowest = Math.max(slowest, Date.now() - start);
if (context.finished) lost++;
await new Promise((resolve) => setTimeout(resolve, 10));
}
console.log(`${name}: ran past its timeout to its end in ${lost} of 25 runs, slowest ${slowest} ms`);
}
})();How often does it reproduce? Is there a required condition?
The script has to be rejecting a promise that has no handler at the moment the timeout fires. In these loops that happened in 9 to 24 of 25 runs. The Promise.resolve(1) loop always stopped in time.
What is the expected behavior? Why is that the expected behavior?
The script stops after about 50 ms and vm.runInContext() throws ERR_SCRIPT_EXECUTION_TIMEOUT, as it does for the Promise.resolve(1) loop. The documentation says timeout is the number of milliseconds to execute code before terminating execution.
What do you see instead?
The stop is lost and the script runs on. ERR_SCRIPT_EXECUTION_TIMEOUT is still thrown, but only once the script has ended by itself.
v26.12.1 win32 x64
Promise.resolve(1): ran past its timeout to its end in 0 of 25 runs, slowest 105 ms
Promise.reject(1): ran past its timeout to its end in 15 of 25 runs, slowest 3038 ms
async function that throws: ran past its timeout to its end in 23 of 25 runs, slowest 4804 ms
v24.22.3 linux x64
Promise.resolve(1): ran past its timeout to its end in 0 of 25 runs, slowest 56 ms
Promise.reject(1): ran past its timeout to its end in 9 of 25 runs, slowest 2109 ms
async function that throws: ran past its timeout to its end in 24 of 25 runs, slowest 1794 ms
v22.23.3 linux x64
Promise.resolve(1): ran past its timeout to its end in 0 of 25 runs, slowest 95 ms
Promise.reject(1): ran past its timeout to its end in 9 of 25 runs, slowest 593 ms
async function that throws: ran past its timeout to its end in 21 of 25 runs, slowest 547 ms
A script that does not end by itself, for (;;) { Promise.reject(1); } with timeout: 60, ran until the heap was full in 6 of 10 runs on v26.12.1 (--max-old-space-size=1024). In the other 4 it stopped within 92 ms.
Additional information
I think this is V8 bug 578383284, fixed on V8's main branch by v8/v8@17a1e55 ("[promises][modules] Handle termination in promise rejection", V8 15.9). I got this from reading the source. I have not run a build with the fix.
When a promise without a handler is rejected, Runtime_PromiseRejectEventFromStack calls Node's PromiseRejectCallback (src/node_task_queue.cc), which calls into JavaScript inside a TryCatchScope. If the timeout fires there, the TryCatch catches the termination. Its destructor throws it again, because the call depth is not zero, but the runtime function then returns undefined without looking for an exception, and the script carries on. The watchdog fires once, so nothing stops the script after that. V8's fix adds that check after ReportPromiseReject.
deps/v8/src/runtime/runtime-promise.cc has no such check in v26.12.1, v26.13.1, the v27.x branch or main (36ca6a7, V8 15.8). The canary-base branch has it.
Could the fix be backported to the supported release lines? The whole commit is large, but Runtime_PromiseRejectEventFromStack and Runtime_PromiseRevokeReject look like the part that matters here. Having PromiseRejectCallback request the termination again when try_catch.HasTerminated() may be a smaller fix. I have tried neither.
I searched the issues and pull requests and found no report of this. #66439 is about a vm timeout too, but about the async hook stack.
I'm reporting it here and not as a vulnerability because node:vm is documented as not being a security mechanism.
Activity