Bug 265175

Summary: Pipe error with csh/tcsh
Product: Base System Reporter: rkanfwwatgxavmdmen
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: New ---    
Severity: Affects Some People CC: kevans, martin, rkanfwwatgxavmdmen
Priority: ---    
Version: 13.1-RELEASE   
Hardware: amd64   
OS: Any   
Attachments:
Description Flags
Screenshot of shell output none

Description rkanfwwatgxavmdmen 2022-07-12 17:00:42 UTC
Created attachment 235222 [details]
Screenshot of shell output

vmstat -m | head -n 1 && vmstat -m | grep kstat_data

when ran as root on csh/tcsh, does not _always_ show the output of the second command, but prints only the headers. The main word here is "always", as sometimes it works fine, but sometimes shows only the headers of vmstat. When commands are separated with a semicolon, everything works fine. Reproducible on 2 separate machines via ssh and on physical console. Bug shows itself only when the command is ran by root, everything works fine with normal user.
Comment 1 Martin Waschbüsch 2022-07-15 15:26:30 UTC
(In reply to Niko Nastonen from comment #0)

I could reproduce what you describe, but are you sure it is pipes that are the problem?

If I try with other commands such as:
df -h | head -n 1 && df -h | grep tmp

I do not see this behavior at all, while on the other hand
sockstat | head -n 1 && sockstat | grep root

always shows this behavior.

As you said, using ; makes it work every time in all cases.

To me it seems like the && operator is somehow causing this (though I do not have any idea why).
Comment 2 rkanfwwatgxavmdmen 2022-07-15 18:22:43 UTC
Also found out that

vmstat -m | head -n 1 || vmstat -m | grep kstat_data

sometimes shows output from both commands and also only as root. As an unprivileged user the output is only headers of vmstat, as it should be. So I presume there is something wrong with the output of the pipe.
Mr Kyle Evans had an idea that there is a race condition inside shell / pipe handling. That is if I understood correctly what he meant.
Comment 3 rkanfwwatgxavmdmen 2022-07-15 19:25:24 UTC
(In reply to Niko Nastonen from comment #2)

Filed a bug in tcsh bugtracker:

https://bugs.astron.com/view.php?id=368

Not a FreeBSD issue.
Comment 4 Kyle Evans freebsd_committer freebsd_triage 2022-07-15 19:27:58 UTC
(In reply to Niko Nastonen from comment #2)

My opinion is that it is not a bug at all, simply how it works. Head will finish very quickly and close stdin sometimes while vmstat is still trying to write to stdout, so you will naturally see an error where that happens.
Comment 5 Kyle Evans freebsd_committer freebsd_triage 2022-07-15 19:31:08 UTC
Actually, I could probably see an argument that head(1) should drain the rest of stdin even with -n constraint.