0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/known_issues/test-vm-timeout-escape-promise.js
James M Snell 5e5a9455f8
doc, test: document and test vm timeout escapes
Using `process.nextTick()`, `Promise`, or `queueMicrotask()`, it
is possible to escape the `timeout` set when running code with
`vm.runInContext()`, `vm.runInThisContext()`, and
`vm.runInNewContext()`.

This documents the issue and adds three known_issues tests.

Refs: https://github.com/nodejs/node/issues/3020
PR-URL: https://github.com/nodejs/node/pull/23743
Refs: https://github.com/nodejs/node/issues/3020
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2018-10-24 13:51:59 -07:00

40 lines
850 B
JavaScript

'use strict';
// https://github.com/nodejs/node/issues/3020
// Promises, nextTick, and queueMicrotask allow code to escape the timeout
// set for runInContext, runInNewContext, and runInThisContext
require('../common');
const assert = require('assert');
const vm = require('vm');
const NS_PER_MS = 1000000n;
const hrtime = process.hrtime.bigint;
function loop() {
const start = hrtime();
while (1) {
const current = hrtime();
const span = (current - start) / NS_PER_MS;
if (span >= 100n) {
throw new Error(
`escaped timeout at ${span} milliseconds!`);
}
}
}
assert.throws(() => {
vm.runInNewContext(
'Promise.resolve().then(loop); loop();',
{
hrtime,
loop
},
{ timeout: 5 }
);
}, {
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
message: 'Script execution timed out after 5ms'
});