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
"cmd | grep --quiet" introduces race condition #1109
Comments
Shellcheck doesn't run your code so if you turn pipefail off before you get to the Now for your simple example, without any flow control, it could work. But for consistency you would probably have to forbid pipefail being left on and so throw a "You seem to have left pipefail active on line N, this causes unexpected behaviour" if you get to a condition or other flow control (eg: return). |
This is a very interesting issue that ShellCheck should be warning about. Yes, it's a mathematically undecidable problem, but that's easily solved through the magic of false positives :P |
Another fail scenarios: |
Similar issue is #665 |
I really like to see this check as I faced this issue, too:
If cmd returns 1 million lines of text and the first line contains the match, this will be really slow compared to --quiet. Instead this could be a solution:
Another possible solution is not to use pipefail at all or only selectively as mentioned here: https://mywiki.wooledge.org/BashPitfalls#pipefail
At the moment I tend to remove pipefail. Before I used it, I never had any problems and now I have to think about race conditions in pipes... |
I would suggest to close this as duplicate of #665. |
FYI, I tend to always use |
Covering all the exceptions caused by pipefail is much more than checking only the grep commands. For me there are only two options left:
|
Here's a snippet or screenshot that shows the problem:
This script should be endless loop, but after some iterations it'll exit with an error (SIGPIPE delivered).
grep --quiet
closes input pipe when it matches pattern, while command on the left hand side will exit with an error trying to write to a closed pipe:Here's what shellcheck currently says:
Nothing
Here's what I wanted or expected to see:
Using "cmd | grep --quiet" with "set -o pipefail" introduces race condition, which may cause the script to fail. use "cmd | grep --count" if possible.
The text was updated successfully, but these errors were encountered: