diff --git a/cmux.go b/cmux.go index 5ba921e..63142fa 100644 --- a/cmux.go +++ b/cmux.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "net" + "strings" "sync" "time" ) @@ -169,6 +170,14 @@ func (m *cMux) Serve() error { for { c, err := m.root.Accept() if err != nil { + if strings.Contains(err.Error(), "use of closed network connection") { + select { + case <-m.donec: // error is expected + return nil + default: + } + } + if !m.handleErr(err) { return err } diff --git a/cmux_test.go b/cmux_test.go index 7b3cfb7..b0af1db 100644 --- a/cmux_test.go +++ b/cmux_test.go @@ -47,7 +47,7 @@ const ( ) func safeServe(errCh chan<- error, muxl CMux) { - if err := muxl.Serve(); !strings.Contains(err.Error(), "use of closed") { + if err := muxl.Serve(); err != nil && !strings.Contains(err.Error(), "use of closed") { errCh <- err } }