Go Hackers,
Sometimes when calling `TCPConn.Read([]byte)(n int, err error)` you get the following error:
"read ip:port use of closed network connection"
This seems like an EOF and I want to treat it as one. It happens via this chain of events
http://golang.org/src/pkg/net/tcpsock_posix.go?s=1875:1926#L73 82 // Read implements the Conn Read method.
83 func (c *TCPConn) Read(b []byte) (n int, err error) {
84 if !c.ok() {
85 return 0, syscall.EINVAL
86 }
87 return c.fd.Read(b)
88 }http://golang.org/src/pkg/net/fd.go 416 func (fd *netFD) Read(p []byte) (n int, err error) {
417 fd.rio.Lock()
418 defer fd.rio.Unlock()
419 if err := fd.incref(false); err != nil {
420 return 0, err
421 }
422 defer fd.decref()
423 for {
424 n, err = syscall.Read(int(fd.sysfd), p)
425 if err == syscall.EAGAIN {
426 err = errTimeout
427 if fd.rdeadline >= 0 {
428 if err = pollserver.WaitRead(fd); err == nil {
429 continue
430 }
431 }
432 }
433 if err != nil {
434 n = 0
435 } else if n == 0 && err == nil && fd.sotype != syscall.SOCK_DGRAM {
436 err = io.EOF
437 }
438 break
439 }
440 if err != nil && err != io.EOF {
441 err = &OpError{"read", fd.net, fd.raddr, err}
442 }
443 return
444 }http://golang.org/src/pkg/net/fd.go 342 var errClosing = errors.New("use of closed network connection")
343
344 // Add a reference to this fd.
345 // If closing==true, pollserver must be locked; mark the fd as closing.
346 // Returns an error if the fd cannot be used.
347 func (fd *netFD) incref(closing bool) error {
348 if fd == nil {
349 return errClosing
350 }
351 fd.sysmu.Lock()
352 if fd.closing {
353 fd.sysmu.Unlock()
354 return errClosing
355 }
356 fd.sysref++
357 if closing {
358 fd.closing = true
359 }
360 fd.sysmu.Unlock()
361 return nil
362 }It seems like errClosing is actually an EOF but maybe this is incorrect? Should you treat it as an EOF or has something different happened? Should these errors be differentiated in anyway and if not should errClosing be changed to an io.EOF in (*netFD).incref()?
-Tim