Skip to content

Commit 363bf74

Browse files
mcollinamarco-ippolito
authored andcommitted
worker: flush stdout and stderr on exit
Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #56428 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent c535a3c commit 363bf74

4 files changed

Lines changed: 67 additions & 4 deletions

File tree

‎lib/internal/bootstrap/switches/is_not_main_thread.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,24 @@ process.removeListener('removeListener', stopListeningIfSignal);
3131

3232
const{
3333
createWorkerStdio,
34+
kStdioWantsMoreDataCallback,
3435
}=require('internal/worker/io');
3536

3637
letworkerStdio;
3738
functionlazyWorkerStdio(){
38-
if(!workerStdio)workerStdio=createWorkerStdio();
39+
if(workerStdio===undefined){
40+
workerStdio=createWorkerStdio();
41+
process.on('exit',flushSync);
42+
}
43+
3944
returnworkerStdio;
4045
}
4146

47+
functionflushSync(){
48+
workerStdio.stdout[kStdioWantsMoreDataCallback]();
49+
workerStdio.stderr[kStdioWantsMoreDataCallback]();
50+
}
51+
4252
functiongetStdout(){returnlazyWorkerStdio().stdout;}
4353

4454
functiongetStderr(){returnlazyWorkerStdio().stderr;}

‎lib/internal/worker/io.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,13 @@ class WritableWorkerStdio extends Writable {
373373
chunks: ArrayPrototypeMap(chunks,
374374
({ chunk, encoding })=>({ chunk, encoding })),
375375
});
376-
ArrayPrototypePush(this[kWritableCallbacks],cb);
377-
if(this[kPort][kWaitingStreams]++===0)
378-
this[kPort].ref();
376+
if(process._exiting){
377+
cb();
378+
}else{
379+
ArrayPrototypePush(this[kWritableCallbacks],cb);
380+
if(this[kPort][kWaitingStreams]++===0)
381+
this[kPort].ref();
382+
}
379383
}
380384

381385
_final(cb){
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
const{ Worker, isMainThread }=require('worker_threads');
5+
6+
if(isMainThread){
7+
constw=newWorker(__filename,{stdout: true});
8+
constexpected='hello world';
9+
10+
letdata='';
11+
w.stdout.setEncoding('utf8');
12+
w.stdout.on('data',(chunk)=>{
13+
data+=chunk;
14+
});
15+
16+
w.on('exit',common.mustCall(()=>{
17+
assert.strictEqual(data,expected);
18+
}));
19+
}else{
20+
process.stdout.write('hello');
21+
process.stdout.write(' ');
22+
process.stdout.write('world');
23+
process.exit(0);
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
const{ Worker, isMainThread }=require('worker_threads');
5+
6+
if(isMainThread){
7+
constw=newWorker(__filename,{stdout: true});
8+
constexpected='hello world';
9+
10+
letdata='';
11+
w.stdout.setEncoding('utf8');
12+
w.stdout.on('data',(chunk)=>{
13+
data+=chunk;
14+
});
15+
16+
w.on('exit',common.mustCall(()=>{
17+
assert.strictEqual(data,expected);
18+
}));
19+
}else{
20+
process.on('exit',()=>{
21+
process.stdout.write(' ');
22+
process.stdout.write('world');
23+
});
24+
process.stdout.write('hello');
25+
}

0 commit comments

Comments
 (0)