-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
child_process: only stop readable side of stream passed to process #27373
Conversation
This comment has been minimized.
This comment has been minimized.
stream._stdio.emit('close'); | ||
} | ||
stream.handle.readStop(); | ||
stream._stdio.pause(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, we could also use stream._stdio.push(null)
and disable reading permanently, instead of until it’s explicitly started again. Any thoughts on that? Generally, the use case of “let another process read from this pipe, once that process is done we’ll continue reading ourselves” is not absurd or anything – shell scripts do that a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never saw it myself but I can understand why just pausing the stream might be useful for piping logs or similar if the process crashes. If there's no technical drawback it sounds strictly better in term of enabled use cases, compared to closing it for good.
A few questions to better understand how it works:
- What does
readStop
do, particularly on writable streams? Is it just a noop? - Maybe related, what does
pause
exactly do? Keep the fd open but stop polling it for data for readable streams, and buffer what's written into it for writable streams? - Since you call both
pause
andreadStop
, how is it meant to be resumed? Is it justresume()
, or is there another function that should be called to counteractreadStop
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never saw it myself but I can understand why just pausing the stream might be useful for piping logs or similar if the process crashes. If there's no technical drawback it sounds strictly better in term of enabled use cases, compared to closing it for good.
@arcanis Okay, thanks! I don’t think there are any technical drawbacks to this.
A few questions to better understand how it works:
- What does
readStop
do, particularly on writable streams? Is it just a noop?
It stops libuv from listening for readable events on the fd until readStart()
is called again. In partiuclar, no read()
system call will occur during that time.
- Maybe related, what does
pause
exactly do? Keep the fd open but stop polling it for data for readable streams, and buffer what's written into it for writable streams?
pause()
makes the stream stop emitting 'data'
events. According to the documentation, it does not stop emitting readable
events, but I guess in that case it’s up to the user to take care of not calling .read()
anyway.
- Since you call both
pause
andreadStop
, how is it meant to be resumed? Is it justresume()
, or is there another function that should be called to counteractreadStop
?
Thanks for pointing this out – yes, one thing more is necessary here: Adding stream.handle.reading = false;
, so that ._read()
will call readStart()
again:
Line 522 in eac8f50
} else if (!this._handle.reading) { |
_read()
actually gets called once we resume the stream. (None of these things are signs of a great streams implementation… we should probably get rid of handle.reading
altogether, but that’s not really a concern for this PR.)
(Both calling .resume()
explicitly and using .on('data')
to start reading again should work.)
I’ve added those things, and also added a test file for this scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a lot of flags, eheh! 😄
Btw, why isn't readStop
enough? If the libuv doesn't listen for readable events anymore, I guess that the data
events won't be triggered, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arcanis The issue that I’m seeing is, that at the time that this code is executed, there might be an existing 'data'
listener or similar that reads data from the stream. When the readable stream buffer drains, the streams implementation calls _read()
, and for sockets that leads to a handle.readStart()
call, undoing the effects of the handle.readStop()
call here.
I mean, sure, it’s somewhat questionable to just ignore existing listeners that read data from the stream, but at this point I feel like that’s a reasonable solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that makes sense, thanks for the explanation!
Closing the underlying resource completely has the unwanted side effect that the stream can no longer be used at all, including passing it to other child processes. What we want to avoid is accidentally reading from the stream; accordingly, it should be sufficient to stop its readable side manually, and otherwise leave the underlying resource intact. Fixes: nodejs#27097 Refs: nodejs#21209
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Recall that if uv isn't epolling a fd to read it, then it won't detect when it closes, either. I didn't look closely, but from the commentary, this PR might have the side effect of causing the 'close' event not to be emitted. I'm not saying that's a problem, but if it is worrying, maybe something to look into. |
@sam-github - is your comment an objection or a request for change? Does anyone have objections on this one landing? It has necessary approvals, and if it lands today by noon UTC, it goes into |
the only failure |
It's a suggestion that you look carefully at close events, which may no longer get emitted if you stop reading. Feel free to disregard, I haven't had time to look at this. |
landed as cc7b3fb |
Closing the underlying resource completely has the unwanted side effect that the stream can no longer be used at all, including passing it to other child processes. What we want to avoid is accidentally reading from the stream; accordingly, it should be sufficient to stop its readable side manually, and otherwise leave the underlying resource intact. Fixes: #27097 Refs: #21209 PR-URL: #27373 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
Closing the underlying resource completely has the unwanted side effect that the stream can no longer be used at all, including passing it to other child processes. What we want to avoid is accidentally reading from the stream; accordingly, it should be sufficient to stop its readable side manually, and otherwise leave the underlying resource intact. Fixes: #27097 Refs: #21209 PR-URL: #27373 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
Closing the underlying resource completely has the unwanted side effect
that the stream can no longer be used at all, including passing it
to other child processes.
What we want to avoid is accidentally reading from the stream;
accordingly, it should be sufficient to stop its readable side
manually, and otherwise leave the underlying resource intact.
Fixes: #27097
Refs: #21209
I’d love to get reviews from @gireeshpunathil, @arcanis, @elibarzilay and maybe @mcollina (esp. regarding the
pause()
vspush(null)
choice?).Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes