From 1c71561977daa7c39da978563621271c08b044cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 23 Jul 2023 12:50:54 +0800 Subject: [PATCH] Fix panic --- h2mux.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/h2mux.go b/h2mux.go index df71dae..a67ef70 100644 --- a/h2mux.go +++ b/h2mux.go @@ -166,7 +166,8 @@ var _ abstractSession = (*h2MuxClientSession)(nil) type h2MuxClientSession struct { transport *http2.Transport clientConn *http2.ClientConn - done chan struct{} + access sync.RWMutex + closed bool } func newH2MuxClient(conn net.Conn) (*h2MuxClientSession, error) { @@ -178,7 +179,6 @@ func newH2MuxClient(conn net.Conn) (*h2MuxClientSession, error) { ReadIdleTimeout: idleTimeout, MaxReadFrameSize: buf.BufferSize, }, - done: make(chan struct{}), } session.transport.ConnPool = session clientConn, err := session.transport.NewClientConn(conn) @@ -228,21 +228,19 @@ func (s *h2MuxClientSession) NumStreams() int { } func (s *h2MuxClientSession) Close() error { - select { - case <-s.done: - default: - close(s.done) + s.access.Lock() + defer s.access.Unlock() + if s.closed { + return os.ErrClosed } + s.closed = true return s.clientConn.Close() } func (s *h2MuxClientSession) IsClosed() bool { - select { - case <-s.done: - return true - default: - } - return s.clientConn.State().Closed + s.access.RLock() + defer s.access.RUnlock() + return s.closed || s.clientConn.State().Closed } func (s *h2MuxClientSession) CanTakeNewRequest() bool {