Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter out broken pipe errors

I'm getting an error returned from an io.Copy call, to which I've passed a socket (TCPConn) as the destination. It's expected that the remote host will simply drop the connection when they've had enough, and I'm not receiving anything from them.

When the drop occurs, I get this error:

write tcp 192.168.26.5:21277: broken pipe

But all I have is an error interface. How can I differentiate broken pipe errors from other kinds of error?

if err.Errno == EPIPE...
like image 795
Matt Joiner Avatar asked Jun 12 '12 19:06

Matt Joiner


People also ask

How do you fix a broken pipe error?

This kind of error can easily be fixed with a command like “sudo apt install –f”. On rare occasions, you may have experienced a broken pipe error. A pipe in Linux / Unix connects two processes, one of them has read-end of the file and the other one has the write-end of the file.

What are broken pipe errors?

A broken Pipe Error is generally an Input/Output Error, which is occurred at the Linux System level. The error has occurred during the reading and writing of the files and it mainly occurs during the operations of the files.

What is broken pipe error in C?

24.2. 6 Operation Error Signals Broken pipe. If you use pipes or FIFOs, you have to design your application so that one process opens the pipe for reading before another starts writing. If the reading process never starts, or terminates unexpectedly, writing to the pipe or FIFO raises a SIGPIPE signal.


2 Answers

The broken pipe error is defined in the syscall package. You can use the equality operator to compare the error to the one in syscall. Check http://golang.org/pkg/syscall/#constants for a complete list of syscall errors. Search "EPIPE" on the page and you will find all the defined errors grouped together.

if err == syscall.EPIPE {
    /* ignore */
}

If you wish to get the actual errno number (although it is pretty useless) you can use a type assertion:

if e, ok := err.(syscall.Errno); ok {
    errno = uintptr(e)
}
like image 63
Stephen Weinberg Avatar answered Sep 22 '22 06:09

Stephen Weinberg


As of go 1.13, you can use errors.Is instead of type assertions.

if errors.Is(err, syscall.EPIPE) {
  // broken pipe
}
like image 43
PrestonP Avatar answered Sep 21 '22 06:09

PrestonP