Worker threads get killed if they listen for data on stdin
Description
When a worker thread tries to listen for data on stdin, it gets killed.
Not even finally blocks get called.
Environment
- Version: v12.0.0
- Platform: Darwin Joshs-MacBook-Air.local 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64
- Subsystem: Probably
worker_thread
Example
A worker is started, it prints "processing" 10 times:
const { Worker, isMainThread } = require('worker_threads') if(isMainThread) { new Worker(__filename).on('exit', (exitStatus) => console.log({ exitStatus })) } else { // process.stdin.on('data', console.log) // <-- this line is commented out for(i=0; i < 10; ++i) console.log(`processing(${i})`) } // => processing(0) // => processing(1) // => processing(2) // => processing(3) // => processing(4) // => processing(5) // => processing(6) // => processing(7) // => processing(8) // => processing(9) // => { exitStatus: 0 }
Now we uncomment the line that listens for data, it gets killed after 2 or 3 iterations (on my machine, guessing quicker on faster machines):
const { Worker, isMainThread } = require('worker_threads') if(isMainThread) { new Worker(__filename).on('exit', (exitStatus) => console.log({ exitStatus })) } else { process.stdin.on('data', console.log) // <-- now this line runs for(i=0; i < 10; ++i) console.log(`processing(${i})`) } // => processing(0) // => processing(1) // => processing(2) // => { exitStatus: 0 }
Expected Behaviour
- I expected the second example to behave the same as the first example. Which is to say that I expect my worker would be able to process my stdin.
- However, if this behaviour is intentional, then I'd expect
- The behaviour to be documented here
- To respect
finallyblocks (omitted from my example to keep it short/focused) - To exit with a nonzero status (docs say "If the worker was terminated, the exitCode parameter will be 1")