Skip to content

Instantly share code, notes, and snippets.

@nebril
Created April 20, 2018 13:26
Show Gist options
  • Save nebril/00e34c372da247f29e15955a323a3501 to your computer and use it in GitHub Desktop.
Save nebril/00e34c372da247f29e15955a323a3501 to your computer and use it in GitHub Desktop.
Golang broken pipe message
package main
import (
"fmt"
"net"
"os"
"syscall"
"time"
)
func main() {
syscall.Unlink("/tmp/test.sock")
ln, err := net.Listen("unix", "/tmp/test.sock")
if err != nil {
fmt.Println(err)
}
go func() {
for {
_, err := ln.Accept()
syscall.Kill(syscall.Getpid(), syscall.SIGPIPE)
if err != nil {
fmt.Println(err)
}
fmt.Println("accepted")
}
}()
conn, err := net.Dial("unix", "/tmp/test.sock")
if err != nil {
fmt.Println(err)
}
buffer := make([]byte, 10000000000)
for {
fmt.Println("writing")
go func() {
_, err = conn.Write(buffer)
if err != nil {
if val, ok := err.(*net.OpError); ok {
if val2, ok := val.Err.(*os.SyscallError); ok {
if val3, ok := val2.Err.(syscall.Errno); ok {
if val3 == syscall.EPIPE {
fmt.Println("Monitor client disconnected")
}
}
}
}
}
}()
go func() {
time.Sleep(5 * time.Millisecond)
syscall.Unlink("/tmp/test.sock")
}()
}
time.Sleep(5 * time.Second)
syscall.Unlink("/tmp/test.sock")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment