Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ package builtins

import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"sort"
"syscall"
"time"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -199,6 +201,14 @@ func (c *CallContext) Errf(format string, a ...any) {
fmt.Fprintf(c.Stderr, format, a...)
}

// IsBrokenPipe reports whether err is a broken-pipe (EPIPE) error,
// which occurs when writing to a pipe whose read end has been closed.
// In bash this triggers SIGPIPE which silently terminates the writer;
// builtins should use this to suppress error messages on pipe closure.
func IsBrokenPipe(err error) bool {
return err != nil && errors.Is(err, syscall.EPIPE)
}

// FileID is a comparable file identity for cycle detection.
// On Unix: device + inode. On Windows: volume serial + file index.
// Used as map key for visited-set tracking.
Expand Down
3 changes: 3 additions & 0 deletions builtins/cat/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func registerFlags(fs *builtins.FlagSet) builtins.HandlerFunc {
err = catRaw(ctx, callCtx, file)
}
if err != nil {
if builtins.IsBrokenPipe(err) {
break
}
name := file
if file == "-" {
name = "standard input"
Expand Down
26 changes: 26 additions & 0 deletions tests/scenarios/shell/pipe/basic/cat_head_no_broken_pipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
description: cat piped into head must not emit a broken-pipe error on stderr.
setup:
files:
- path: big.txt
content: |+
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
input:
allowed_paths: ["$DIR"]
script: |+
cat big.txt | head -n 3
expect:
stdout: |+
line 1
line 2
line 3
stderr: |+
exit_code: 0
Loading