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...
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.
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.
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.
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)
}
As of go 1.13, you can use errors.Is instead of type assertions.
if errors.Is(err, syscall.EPIPE) {
// broken pipe
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With