2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-09 19:21:52 +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) {
c, ok := <-l.connc
if !ok {
select {
case c = <-l.connc:
return c, nil
case <-l.donec:
return nil, ErrListenerClosed
}
return c, nil
}
type MuxConn struct {

View File

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