GNU bug report logs - #79946
grep -q exits too early and causes EPIPE in the piped process

Previous Next

Package: grep;

Reported by: Yu Watanabe <watanabe.yu+bug.grep <at> gmail.com>

Date: Thu, 4 Dec 2025 05:20:02 UTC

Severity: normal

To reply to this bug, email your comments to 79946 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-grep <at> gnu.org:
bug#79946; Package grep. (Thu, 04 Dec 2025 05:20:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Yu Watanabe <watanabe.yu+bug.grep <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-grep <at> gnu.org. (Thu, 04 Dec 2025 05:20:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Yu Watanabe <watanabe.yu+bug.grep <at> gmail.com>
To: bug-grep <at> gnu.org
Subject: grep -q exits too early and causes EPIPE in the piped process
Date: Thu, 4 Dec 2025 11:22:25 +0900
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




Information forwarded to bug-grep <at> gnu.org:
bug#79946; Package grep. (Thu, 04 Dec 2025 05:30:02 GMT) Full text and rfc822 format available.

Message #8 received at 79946 <at> debbugs.gnu.org (full text, mbox):

From: jackson <at> fastmail.fm
To: "Yu Watanabe" <watanabe.yu+bug.grep <at> gmail.com>, 79946 <at> debbugs.gnu.org
Subject: Re: bug#79946: grep -q exits too early and causes EPIPE in the piped
 process
Date: Wed, 03 Dec 2025 23:28:49 -0600
[Message part 1 (text/plain, inline)]

On Wed, Dec 3, 2025, at 8:22 PM, Yu Watanabe wrote:
> /tmp/hoge

What is in the file "/tmp/hoge" ?  If it is the output of a "loginctl" command on some system, that makes consideration of this report impractical for anyone on a system that doesn't emit output with the same matching strings.

Paul Jackson
jackson <at> fastmail.fm
[Message part 2 (text/html, inline)]

Information forwarded to bug-grep <at> gnu.org:
bug#79946; Package grep. (Thu, 04 Dec 2025 05:57:02 GMT) Full text and rfc822 format available.

Message #11 received at 79946 <at> debbugs.gnu.org (full text, mbox):

From: Sam James <sam <at> gentoo.org>
To: jackson <at> fastmail.fm
Cc: 79946 <at> debbugs.gnu.org, Yu Watanabe <watanabe.yu+bug.grep <at> gmail.com>
Subject: Re: bug#79946: grep -q exits too early and causes EPIPE in the
 piped process
Date: Thu, 04 Dec 2025 05:55:49 +0000
jackson <at> fastmail.fm writes:

> On Wed, Dec 3, 2025, at 8:22 PM, Yu Watanabe wrote:
>> /tmp/hoge
>
> What is in the file "/tmp/hoge" ?  If it is the output of a "loginctl"
> command on some system, that makes consideration of this report
> impractical for anyone on a system that doesn't emit output with the
> same matching strings.

He cats it in his report?

>
> Paul Jackson
> jackson <at> fastmail.fm




Information forwarded to bug-grep <at> gnu.org:
bug#79946; Package grep. (Thu, 04 Dec 2025 06:31:01 GMT) Full text and rfc822 format available.

Message #14 received at 79946 <at> debbugs.gnu.org (full text, mbox):

From: jackson <at> fastmail.fm
To: "Sam James" <sam <at> gentoo.org>
Cc: 79946 <at> debbugs.gnu.org, Yu Watanabe <watanabe.yu+bug.grep <at> gmail.com>
Subject: Re: bug#79946: grep -q exits too early and causes EPIPE in the piped
 process
Date: Thu, 04 Dec 2025 00:29:40 -0600
>> He cats it in his report?

Yes, he does.  My bad.  Sorry for the noise.

-- 
Paul Jackson
jackson <at> fastmail.fm




This bug report was last modified today.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.