Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {

bool timed_out = false;
bool received_signal = false;
const uint32_t async_stack_length =
realm->env()->async_hooks()->fields()[AsyncHooks::kStackLength];
MaybeLocal<Value> result;
{
auto wd = timeout != -1
Expand Down Expand Up @@ -853,6 +855,13 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
if (timed_out || received_signal) {
if (!realm->env()->is_main_thread() && realm->env()->is_stopping()) return;
isolate->CancelTerminateExecution();
// A promise job terminated by the watchdog never runs its async hook
// `after` callback, so drop the contexts it left on the stack.
AsyncHooks* async_hooks = realm->env()->async_hooks();
while (async_hooks->fields()[AsyncHooks::kStackLength] >
async_stack_length) {
async_hooks->pop_async_context(realm->env()->execution_async_id());
}
// It is possible that execution was terminated by another timeout in
// which this timeout is nested, so check whether one of the watchdogs
// from this invocation is responsible for termination.
Expand Down
9 changes: 9 additions & 0 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,8 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
MaybeLocal<Value> result;
bool timed_out = false;
bool received_signal = false;
const uint32_t async_stack_length =
env->async_hooks()->fields()[AsyncHooks::kStackLength];
{
auto wd = timeout != -1 ? std::make_optional<Watchdog>(
env->isolate(), timeout, &timed_out)
Expand All @@ -1304,6 +1306,13 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
if (!env->is_main_thread() && env->is_stopping())
return false;
env->isolate()->CancelTerminateExecution();
// A promise job terminated by the watchdog never runs its async hook
// `after` callback, so drop the contexts it left on the stack.
AsyncHooks* async_hooks = env->async_hooks();
while (async_hooks->fields()[AsyncHooks::kStackLength] >
async_stack_length) {
async_hooks->pop_async_context(env->execution_async_id());
}
// It is possible that execution was terminated by another timeout in
// which this timeout is nested, so check whether one of the watchdogs
// from this invocation is responsible for termination.
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-async-hooks-vm-module-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Flags: --experimental-vm-modules
'use strict';

const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const vm = require('vm');

async_hooks.createHook({ init() {} }).enable();

(async () => {
const context = vm.createContext({}, { microtaskMode: 'afterEvaluate' });
const module = new vm.SourceTextModule(
'Promise.resolve().then(() => { while (true); });',
{ context }
);
await module.link(common.mustNotCall());

await assert.rejects(module.evaluate({ timeout: 5 }), {
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
});
setImmediate(common.mustCall());
})().then(common.mustCall());
24 changes: 24 additions & 0 deletions test/parallel/test-async-hooks-vm-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const vm = require('vm');

async_hooks.createHook({ init() {} }).enable();

const executionAsyncId = async_hooks.executionAsyncId();
const context = vm.createContext({}, { microtaskMode: 'afterEvaluate' });

assert.throws(() => {
vm.runInContext(
'Promise.resolve().then(() => { while (true); });',
context,
{ timeout: 5 }
);
}, {
code: 'ERR_SCRIPT_EXECUTION_TIMEOUT',
});

assert.strictEqual(async_hooks.executionAsyncId(), executionAsyncId);
setImmediate(common.mustCall());
Loading