Created
April 20, 2018 13:26
-
-
Save nebril/00e34c372da247f29e15955a323a3501 to your computer and use it in GitHub Desktop.
Golang broken pipe message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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