Skip to content

Commit b93a723

Browse files
addaleaxcodebytere
authored andcommitted
test: regression tests for async_hooks + Promise + Worker interaction
Add regression tests for the case in which an async_hook is enabled inside a Worker thread and `process.exit()` is called during the async part of an async function. This commit includes multiple tests that seem like they should all crash in a similar way, but interestingly don’t. In particular, it’s surprising that the presence of a statement after `process.exit()` in a function has an effect on the kind of crash that’s being exhibited (V8 DCHECK vs. assertion in our own code) and the circumstances under which it crashes (e.g. the -1 and -2 tests can be “fixed” by reverting 13c5a16, although they should have the same behavior as the -3 and -4 tests). PR-URL: #33347 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent dd4789b commit b93a723

4 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker } = require('worker_threads');
4+
5+
const w = new Worker(`
6+
const { createHook } = require('async_hooks');
7+
8+
setImmediate(async () => {
9+
createHook({ init() {} }).enable();
10+
await 0;
11+
process.exit();
12+
});
13+
`, { eval: true });
14+
15+
w.on('exit', common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker } = require('worker_threads');
4+
5+
// Like test-async-hooks-worker-promise.js but with the `await` and `createHook`
6+
// lines switched, because that resulted in different assertion failures
7+
// (one a Node.js assertion and one a V8 DCHECK) and it seems prudent to
8+
// cover both of those failures.
9+
10+
const w = new Worker(`
11+
const { createHook } = require('async_hooks');
12+
13+
setImmediate(async () => {
14+
await 0;
15+
createHook({ init() {} }).enable();
16+
process.exit();
17+
});
18+
`, { eval: true });
19+
20+
w.postMessage({});
21+
w.on('exit', common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker } = require('worker_threads');
4+
5+
// Like test-async-hooks-worker-promise.js but with an additional statement
6+
// after the `process.exit()` call, that shouldn’t really make a difference
7+
// but apparently does.
8+
9+
const w = new Worker(`
10+
const { createHook } = require('async_hooks');
11+
12+
setImmediate(async () => {
13+
createHook({ init() {} }).enable();
14+
await 0;
15+
process.exit();
16+
process._rawDebug('THIS SHOULD NEVER BE REACHED');
17+
});
18+
`, { eval: true });
19+
20+
w.on('exit', common.mustCall());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker } = require('worker_threads');
5+
6+
// Like test-async-hooks-worker-promise.js but doing a trivial counter increase
7+
// after process.exit(). This should not make a difference, but apparently it
8+
// does. This is *also* different from test-async-hooks-worker-promise-3.js,
9+
// in that the statement is an ArrayBuffer access rather than a full method,
10+
// which *also* makes a difference even though it shouldn’t.
11+
12+
const workerData = new Int32Array(new SharedArrayBuffer(4));
13+
const w = new Worker(`
14+
const { createHook } = require('async_hooks');
15+
16+
setImmediate(async () => {
17+
createHook({ init() {} }).enable();
18+
await 0;
19+
process.exit();
20+
workerData[0]++;
21+
});
22+
`, { eval: true, workerData });
23+
24+
w.on('exit', common.mustCall(() => assert.strictEqual(workerData[0], 0)));

0 commit comments

Comments
 (0)