Skip to content

Commit 1a49a5c

Browse files
committed
process: triggerAsyncId can be undefined
Fixes: nodejs#14386 Fixes: nodejs#14381
1 parent aa496f4 commit 1a49a5c

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

Diff for: lib/internal/process/next_tick.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function setupNextTick() {
281281
if (process._exiting)
282282
return;
283283

284-
if (triggerAsyncId === null) {
284+
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId <= 0) {
285285
triggerAsyncId = async_hooks.initTriggerId();
286286
}
287287

Diff for: test/async-hooks/test-internal-nexttick-default-trigger.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ internal.nextTick(null, common.mustCall(function() {
2626
assert.strictEqual(async_hooks.triggerAsyncId(), rootAsyncId);
2727
}));
2828

29+
// internal default
30+
internal.nextTick(undefined, common.mustCall(function() {
31+
assert.strictEqual(async_hooks.triggerAsyncId(), rootAsyncId);
32+
}));
33+
2934
// internal
3035
internal.nextTick(rootAsyncId + 1, common.mustCall(function() {
3136
assert.strictEqual(async_hooks.triggerAsyncId(), rootAsyncId + 1);
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { OutgoingMessage } = require('http');
5+
const { Writable } = require('stream');
6+
const assert = require('assert');
7+
8+
// check that OutgoingMessage can be used without a proper Socket
9+
// Fixes: https://github.com/nodejs/node/issues/14386
10+
// Fixes: https://github.com/nodejs/node/issues/14381
11+
12+
class Response extends OutgoingMessage {
13+
constructor() {
14+
super({ method: 'GET', httpVersionMajor: 1, httpVersionMinor: 1 });
15+
}
16+
17+
_implicitHeader() {}
18+
}
19+
20+
const res = new Response();
21+
const ws = new Writable({
22+
write: common.mustCall((chunk, encoding, callback) => {
23+
assert(chunk.toString().match(/hello world/));
24+
setImmediate(callback);
25+
})
26+
});
27+
28+
res.socket = ws;
29+
ws._httpMessage = res;
30+
res.connection = ws;
31+
32+
res.end('hello world');
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { ServerResponse } = require('http');
5+
const { Writable } = require('stream');
6+
const assert = require('assert');
7+
8+
// check that ServerResponse can be used without a proper Socket
9+
// Fixes: https://github.com/nodejs/node/issues/14386
10+
// Fixes: https://github.com/nodejs/node/issues/14381
11+
12+
const res = new ServerResponse({
13+
method: 'GET',
14+
httpVersionMajor: 1,
15+
httpVersionMinor: 1
16+
});
17+
18+
const ws = new Writable({
19+
write: common.mustCall((chunk, encoding, callback) => {
20+
assert(chunk.toString().match(/hello world/));
21+
setImmediate(callback);
22+
})
23+
});
24+
25+
res.assignSocket(ws);
26+
27+
res.end('hello world');

0 commit comments

Comments
 (0)