-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Piping process.stdin
to child.stdin
leaves behind an open handle
#32291
Comments
You could replace |
@mscdex That’s true, but it still seems to me like when we’re not reading from the only stream in use, that that should stop the event loop? |
I think ultimately the issue here is that once we’ve told a stream to start reading, we have no way of telling it to stop until it does read some data. We have a hack specifically for @ronag @nodejs/streams Any ideas? Would it be possible to figure out when a stream is no longer actively being consumed and e.g. emit a event in that case that stops an in-progress |
We do not expose The "correct" implementation is: const { spawn } = require('child_process')
const { pipeline } = require('stream')
const child = spawn('./foo')
// pipeline calls destroy for you
pipeline(process.stdin, child.stdin, (err) => {
console.log('done', err)
}) |
@mcollina I’m sorry but I think that answer misses the point here.
Yes. I am asking for a new mechanism to be introduced for this, essentially.
That may be helpful for the issue that OP is running into, but it assumes that you always want to destroy a stream when you are unpiping it, and that’s definitely not true. What if stdin were to be piped into another destination after the child process ended? This problem also arises even without the use of pipes: 'use strict';
function handler(chunk) {
console.log('got', chunk);
process.stdin.removeListener('data', handler);
};
process.stdin.on('data', handler); After the first chunk, there is nothing in the process that should keep the event loop alive, but the process does not stop trying to read from |
I understand now. The property of |
@mcollina Yes, |
I think that's possible. It means adding some more intelligence to: Lines 876 to 924 in 40b559a
|
Original OP here: the short version is that I performed one |
I would like to add that destroying the stream is not always an option in cases where the function does not know whether the stream will be re-used (in which case it should not be destroyed) or not (in which case it should not leave a handle open, keeping the parent process running). |
Do you maybe have a concrete example of this? Shouldn't it be up to the "root" consumer/app then to ensure that resources are released/destroyed? If resources are not properly cleaned up then it should keep running, no? |
Yes you're right. What I meant was more along the following lines. In an example like: const { spawn } = require('child_process')
const child = spawn('./foo')
process.stdin.pipe(child.stdin)
child.on('exit', () => {
console.log('done')
process.stdin.destroy()
}) This seems to be a problem that spawning a child process that uses the parent |
Hm, this is not my area of expertise but I would expect any open handles/references to be closed on |
I'm writing essentially a script runner and I want to run multiple processes in a row. In this case, I'm basically converting shell scripts to Node.js:
becomes #!/usr/bin/env node
async function main() {
await execute('interactive-command-1 arg1 arg2')
await execute('interactive-command-2 arg1 arg2')
} so I'm trying to write an main().then(() => {
process.stdin.destroy();
}); which I think is the implication here. It's part of the contract of |
Hi all, any updates on this? |
13.11.0
(tried on8.3.0
too, same result)19.10
What steps will reproduce the bug?
foo.c
Compile it:
index.js
:Launch
index.js
:$ node index.js input done
After entering some
input
, the child process exits anddone
is printed. However the main process does not exit (although it should). It looks likeprocess.stdin
has a left handle. After entering newline again onstdin
, the process now exits.How often does it reproduce?
Always
What is the expected behavior?
See above.
What do you see instead?
See above.
The text was updated successfully, but these errors were encountered: