Hi!

When grep is built with musl, e.g. Alpine/postmarketOS,
it seems `grep -q` exits too early and the piped process gets EPIPE.

Input:
```
$ cat /tmp/hoge
SESSION UID USER SEAT LEADER CLASS         TTY   IDLE SINCE
     c1   0 root -    71     user-early    pts/0 no   -
     c2   0 root -    89     manager-early -     no   -

2 sessions listed.
```
Test script:
```
#!/bin/bash

set -o pipefail

trial=1000
count=0
for ((i=0;i<trial;i++)); do
    cat /tmp/hoge | grep root | grep root | grep root | grep -q user-early
    if [[ $? != 0 ]]; then
        count=$(( count += 1 ))
    fi
done

echo $count/$trial
```
Result:
```
$ /tmp/test.sh
93/1000
```
Note, the deep nest in the test script is not necessary to reproduce
the issue, but increases the probability of the error. If the command
in the loop of the test script is replaced with
```
grep root /tmp/hoge | grep -q user-early
```
then the error rate is about 1/1000 .

I guess `grep -q` mistakenly handles that draining the input is done
after a matching line is found even if the piped process still writes
something.

See also https://github.com/systemd/systemd/pull/39988 .

```
$ grep --version
grep (GNU grep) 3.12
Copyright (C) 2025 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others; see
<https://git.savannah.gnu.org/cgit/grep.git/tree/AUTHORS>.

grep -P uses PCRE2 10.47 2025-10-21
```

Yu



Reply via email to