2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-19 10:35:47 +08:00

do not propagate "use of closed conn" error if expected

There's unfortunately no way (AFAIK) of actively interrupting a
net.Listener.Accept() call, so we have to deal with it erroring out when
its net.Listener is closed.
Unfortunately(2) there's also no better way of matching the low-level
error than string checks, since it is not exported by the stdlib.
This commit is contained in:
Leo Antunes 2022-02-09 22:26:56 +01:00
parent 5ec6847320
commit d58b409a09
2 changed files with 10 additions and 1 deletions

View File

@ -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
}

View File

@ -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
}
}