Fix #309: Use stream_select instead of non-blocking STDIN #312
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The idiom used in the readStdin function from src/Utils/CLI.php is not
entirely error-prone: marking stdin as non-blocking and then reading
from it can lead to racy scenarios where we are not able to catch
what's being fed to the program. This can be seen very frequently on
s390x, where a loop like this:
$ while echo "invalid query" | /usr/bin/php7.4 /path/to//highlight-query; do sleep 1; done;
will lead to failures once on every few runs (as can be seen in the
description of issue #309).
This commit implements the more robust method which uses stream_select
to wait for stdin to become available, but resorts to a sensible
timeout value (0.2 seconds) which will prevent the blocking from
happening.
I tested this patch extensively on s390x and amd64, and noticed that
the non-determinism is gone.
Signed-off-by: Sergio Durigan Junior sergio.durigan@canonical.com