Skip to content

Commit c7f350d

Browse files
committed
process,worker: ensure code followed by exit() effectless
Cope with the delay(to the next function call) of v8::Isolate::TerminateExecution()
1 parent a01dbf1 commit c7f350d

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed

lib/internal/process/per_thread.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ function hrtimeBigInt() {
104104
return hrBigintValues[0];
105105
}
106106

107+
function nop() {}
108+
107109
// The execution of this function itself should not cause any side effects.
108110
function wrapProcessMethods(binding) {
109111
const {
@@ -195,6 +197,16 @@ function wrapProcessMethods(binding) {
195197
// in the user land. Either document it, or deprecate it in favor of a
196198
// better public alternative.
197199
process.reallyExit(process.exitCode || kNoFailure);
200+
201+
// If this is a worker, v8::Isolate::TerminateExecution() is called above.
202+
// That function spoofs the stack pointer to cause the stack guard
203+
// check to throw the termination exception. Because v8 performs
204+
// stack guard check upon every function call, we give it a chance.
205+
//
206+
// Without this, user code followed by `process.exit()` would take effect.
207+
// test/parallel/test-worker-voluntarily-exit-followed-by-addition.js
208+
// test/parallel/test-worker-voluntarily-exit-followed-by-throw.js
209+
nop();
198210
}
199211

200212
function kill(pid, sig) {

test/cctest/test_environment.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,9 @@ TEST_F(EnvironmentTest, ExitHandlerTest) {
553553
callback_calls++;
554554
node::Stop(*env);
555555
});
556-
node::LoadEnvironment(*env, "process.exit(42)").ToLocalChecked();
556+
// When terminating, v8 throws makes the current embedder call bail out
557+
// with MaybeLocal<>()
558+
EXPECT_TRUE(node::LoadEnvironment(*env, "process.exit(42)").IsEmpty());
557559
EXPECT_EQ(callback_calls, 1);
558560
}
559561

test/node-api/test_worker_terminate/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ if (isMainThread) {
1919
const { Test } = require(`./build/${common.buildType}/test_worker_terminate`);
2020

2121
const { counter } = workerData;
22-
// Test() tries to call a function twice and asserts that the second call does
23-
// not work because of a pending exception.
22+
// Test() tries to call a function and asserts it fails because of a
23+
// pending termination exception.
2424
Test(() => {
2525
Atomics.add(counter, 0, 1);
2626
process.exit();

test/node-api/test_worker_terminate/test_worker_terminate.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ napi_value Test(napi_env env, napi_callback_info info) {
1717
NODE_API_ASSERT(env, t == napi_function,
1818
"Wrong first argument, function expected.");
1919

20-
status = napi_call_function(env, recv, argv[0], 0, NULL, NULL);
21-
assert(status == napi_ok);
2220
status = napi_call_function(env, recv, argv[0], 0, NULL, NULL);
2321
assert(status == napi_pending_exception);
2422

test/parallel/test-async-hooks-worker-asyncfn-terminate-4.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { Worker } = require('worker_threads');
1212
const workerData = new Int32Array(new SharedArrayBuffer(4));
1313
const w = new Worker(`
1414
const { createHook } = require('async_hooks');
15+
const { workerData } = require('worker_threads');
1516
1617
setImmediate(async () => {
1718
createHook({ init() {} }).enable();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread } = require('worker_threads');
5+
6+
if (isMainThread) {
7+
const workerData = new Int32Array(new SharedArrayBuffer(4));
8+
const w = new Worker(__filename, {
9+
workerData,
10+
});
11+
w.on('exit', common.mustCall(() => {
12+
assert.strictEqual(workerData[0], 0);
13+
}));
14+
} else {
15+
const { workerData } = require('worker_threads');
16+
process.exit();
17+
workerData[0] = 1;
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread } = require('worker_threads');
5+
6+
if (isMainThread) {
7+
const workerData = new Int32Array(new SharedArrayBuffer(4));
8+
const w = new Worker(__filename, {
9+
workerData,
10+
});
11+
w.on('exit', common.mustCall(() => {
12+
assert.strictEqual(workerData[0], 0);
13+
}));
14+
} else {
15+
const { workerData } = require('worker_threads');
16+
try {
17+
process.exit();
18+
throw new Error('xxx');
19+
// eslint-disable-next-line no-unused-vars
20+
} catch (err) {
21+
workerData[0] = 1;
22+
}
23+
}

0 commit comments

Comments
 (0)