2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-19 18:45:48 +08:00

Return an error in Accept() when root is closed.

As reported in #4, when the root listener is closed, calling Accept
on mux'd listeners would block forever, instead of returning an error.
This is because of a bug introduced in b90740d.

This commit fixes #4 by selecting on both donec and connc of muxed
listeners.

Added a test case to guard against this issue.
This commit is contained in:
Soheil Hassas Yeganeh 2015-12-19 22:28:37 -05:00
parent 44439c27f0
commit 89dd8ce1fd
2 changed files with 8 additions and 3 deletions

View File

@ -158,11 +158,12 @@ type muxListener struct {
} }
func (l muxListener) Accept() (c net.Conn, err error) { func (l muxListener) Accept() (c net.Conn, err error) {
c, ok := <-l.connc select {
if !ok { case c = <-l.connc:
return c, nil
case <-l.donec:
return nil, ErrListenerClosed return nil, ErrListenerClosed
} }
return c, nil
} }
type MuxConn struct { type MuxConn struct {

View File

@ -180,4 +180,8 @@ func TestClosed(t *testing.T) {
lis := mux.Match(Any()).(muxListener) lis := mux.Match(Any()).(muxListener)
close(lis.donec) close(lis.donec)
mux.serve(closerConn{}) mux.serve(closerConn{})
_, err := lis.Accept()
if _, ok := err.(errListenerClosed); !ok {
t.Errorf("expected errListenerClosed got %v", err)
}
} }