Skip to content

Commit 69a2a6b

Browse files
mcollinadanielleadams
authored andcommitted
bootstrap: call _undestroy() inside _destroy for stdout and stderr
This change makes `process.stdout` and `process.stderr` to be automatically undestroyed when ended/destrouyed, therefore making it always possible to write/console.log to stdout. Fixes: #39447 PR-URL: #39685 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 07cadc4 commit 69a2a6b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/internal/bootstrap/switches/is_main_thread.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function createWritableStdioStream(fd) {
100100

101101
function dummyDestroy(err, cb) {
102102
cb(err);
103+
this._undestroy();
103104

104105
// We need to emit 'close' anyway so that the closing
105106
// of the stream is observable. We just make sure we

test/parallel/test-stdio-undestroy.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const spawn = require('child_process').spawn;
5+
6+
if (process.argv[2] === 'child') {
7+
process.stdout.destroy();
8+
process.stderr.destroy();
9+
console.log('stdout');
10+
process.stdout.write('rocks\n');
11+
console.error('stderr');
12+
setTimeout(function() {
13+
process.stderr.write('rocks too\n');
14+
}, 10);
15+
return;
16+
}
17+
18+
const proc = spawn(process.execPath, [__filename, 'child'], { stdio: 'pipe' });
19+
20+
let stdout = '';
21+
proc.stdout.setEncoding('utf8');
22+
proc.stdout.on('data', common.mustCallAtLeast(function(chunk) {
23+
stdout += chunk;
24+
}, 1));
25+
26+
let stderr = '';
27+
proc.stderr.setEncoding('utf8');
28+
proc.stderr.on('data', common.mustCallAtLeast(function(chunk) {
29+
stderr += chunk;
30+
}, 1));
31+
32+
proc.on('exit', common.mustCall(function(exitCode) {
33+
assert.strictEqual(exitCode, 0);
34+
assert.strictEqual(stdout, 'stdout\nrocks\n');
35+
assert.strictEqual(stderr, 'stderr\nrocks too\n');
36+
}));

0 commit comments

Comments
 (0)