1
0
mirror of https://github.com/golang/go synced 2024-11-20 05:24:41 -07:00

net: return error from pollster rather than panicing

Fixes #3590.

R=bradfitz, mikioh.mikioh, iant, bsiegert
CC=golang-dev
https://golang.org/cl/6684054
This commit is contained in:
Dave Cheney 2012-10-17 09:41:00 +11:00
parent 7565726875
commit 8c2b131cd1
2 changed files with 64 additions and 8 deletions

View File

@ -97,15 +97,11 @@ func (s *pollServer) AddFD(fd *netFD, mode int) error {
} }
wake, err := s.poll.AddFD(intfd, mode, false) wake, err := s.poll.AddFD(intfd, mode, false)
if err != nil {
panic("pollServer AddFD " + err.Error())
}
if wake {
doWakeup = true
}
s.Unlock() s.Unlock()
if err != nil {
if doWakeup { return &OpError{"addfd", fd.net, fd.laddr, err}
}
if wake || doWakeup {
s.Wakeup() s.Wakeup()
} }
return nil return nil

View File

@ -0,0 +1,60 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd linux netbsd openbsd
package net
import (
"testing"
)
// Issue 3590. netFd.AddFD should return an error
// from the underlying pollster rather than panicing.
func TestAddFDReturnsError(t *testing.T) {
l, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer l.Close()
go func() {
for {
c, err := l.Accept()
if err != nil {
return
}
defer c.Close()
}
}()
c, err := Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
defer c.Close()
// replace c's pollServer with a closed version.
ps, err := newPollServer()
if err != nil {
t.Fatal(err)
}
ps.poll.Close()
c.(*TCPConn).conn.fd.pollServer = ps
var b [1]byte
_, err = c.Read(b[:])
if err, ok := err.(*OpError); ok {
if err.Op == "addfd" {
return
}
if err, ok := err.Err.(*OpError); ok {
// the err is sometimes wrapped by another OpError
if err.Op == "addfd" {
return
}
}
}
t.Error(err)
}