You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

37 lines
732 B

  1. //+build darwin,go1.9
  2. package nosigpipe
  3. import (
  4. "net"
  5. "syscall"
  6. "github.com/google/martian/log"
  7. )
  8. // IgnoreSIGPIPE prevents SIGPIPE from being raised on TCP sockets when remote hangs up
  9. // See: https://github.com/golang/go/issues/17393
  10. func IgnoreSIGPIPE(c net.Conn) {
  11. if c == nil {
  12. return
  13. }
  14. s, ok := c.(syscall.Conn)
  15. if !ok {
  16. return
  17. }
  18. r, e := s.SyscallConn()
  19. if e != nil {
  20. log.Errorf("Failed to get SyscallConn: %s", e)
  21. return
  22. }
  23. e = r.Control(func(fd uintptr) {
  24. intfd := int(fd)
  25. if e := syscall.SetsockoptInt(intfd, syscall.SOL_SOCKET, syscall.SO_NOSIGPIPE, 1); e != nil {
  26. log.Errorf("Failed to set SO_NOSIGPIPE: %s", e)
  27. }
  28. })
  29. if e != nil {
  30. log.Errorf("Failed to set SO_NOSIGPIPE: %s", e)
  31. }
  32. }