-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
All streams are duplex (r/w) by default and can thus keep the loop running #37
Comments
Possible solution A: Clear, explicit documentation. This is probably the least work. Some documentation could certainly help here – though TBH I'm not considering this a solution. |
Possible solution B: Examine stream resource meta data and automatically pause write-only streams. This would involve some sorcery with stream_get_meta_data() and fopen() flags. This would probably solve the 80% case where we can automatically stop reading from write-only streams. This shouldn't be too much work and would probably resolve this for most cases. I'm not sure how reliable this is going to be, so we would likely need some tests to back this up. However, the user could potentially still issue some fancy things like:
As such, while this may be considered a good default for now, I'm tempted to consider this a temporary workaround only. |
Possible solution C: Add The user would then have to use code like: $in = new ReadableStreamResource(STDIN);
$out = new WritableStreamResource(STDOUT);
assert($in->isReadable() === true);
assert($out->isWritable() === true);
$in->pipe($out); And
This would align with our interfaces: Due to the available interface, this would also prevent illogical method calls like these:
This change would (obviously) involve some more work and a BC break (which I'm okay with, personally). |
Why isn't calling $loop = React\EventLoop\Factory::create();
$in = new \React\Stream\Stream(STDIN, $loop);
// program exit
$heartBeatTimer = $loop->addPeriodicTimer(0.01, function() {
pcntl_signal_dispatch();
});
pcntl_signal(SIGTERM, function() use ($in, $heartBeatTimer){
$heartBeatTimer->cancel();
// Remove the stdin stream from the run loop as well
$in->close();
});
// use the stdin stream
$in->on('data', function(string $newLine){
// handle the input line
});
$loop->run(); |
@semmel Not sure I get what you're suggesting here, care to elaborate? :) The loop does (correctly) terminate if it has nothing to do. It's just that an (unmindful) call to |
@clue Yeah I probably misunderstood completely this issue 😳 My understanding is that it is just natural to this library to keep the loop running while there are streams open and connected to the loop. What I probably got wrong is
thinking that in order to drop out of the loop I had to call So nevermind, I just didn't get what the bug is about while I was just looking for an example to manage a |
You could also implement both solutions, so the user can either choose stream type explicitly ( Having all streams duplex by default is confusing.
It would be more convenient if when there is no more data to write to STDOUT (for example, a stream has empty buffer and no data were added on |
Actually there can be EOF on STDIN when it is redirected to a fileand file has been read completely or when a user presses Ctrl + D in console. |
The
Stream
class assumes all stream resources are duplex (r/w) streams, while some may only be readable (e.g.STDIN
) or only be writable (e.g.STDOUT
).I'm not considering this to be a bug because this happens to be documented behavior, albeit probably unexpected for many newcomers.
The following gist highlights this problem:
The average user would probably assume this to write out some data (if at all) and then exit.
However, this example does not terminate ever. The
Stream
class assumes theSTDOUT
stream is a duplex stream resource and thus keeps waiting for incoming data infinitely.In the above example, this problem can be worked around by pausing the stream anywhere like this:
I'm going to argue that this is counter-intuitive and confusing (what exactly is being paused here?).
Note that a similar problem happens with stream resources that are read-only (e.g.
STDIN
) though it's less apparent.This ticket aims to serve as a reminder for this hardly documented behavior, for the reference and in order to discuss possible alternatives.
The text was updated successfully, but these errors were encountered: