From a65b7a059a5fabd7a49be2d5ebbfaafd2f69a571 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Thu, 21 Aug 2025 22:12:19 +0800 Subject: [PATCH] try to fix http2 panic (#54) --- listener/http2/conn.go | 7 +++++++ listener/http2/listener.go | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/listener/http2/conn.go b/listener/http2/conn.go index b546f45f..956302e6 100644 --- a/listener/http2/conn.go +++ b/listener/http2/conn.go @@ -13,6 +13,7 @@ type conn struct { raddr net.Addr ctx context.Context cancel context.CancelFunc + closed chan struct{} } func (c *conn) Read(b []byte) (n int, err error) { @@ -27,6 +28,12 @@ func (c *conn) Close() error { if c.cancel != nil { c.cancel() } + + select { + case <-c.closed: + default: + close(c.closed) + } return nil } diff --git a/listener/http2/listener.go b/listener/http2/listener.go index 041d6d1d..dc914428 100644 --- a/listener/http2/listener.go +++ b/listener/http2/listener.go @@ -178,6 +178,7 @@ func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) { raddr: remoteAddr, ctx: ctx, cancel: cancel, + closed: make(chan struct{}), } select { @@ -187,5 +188,6 @@ func (l *http2Listener) handleFunc(w http.ResponseWriter, r *http.Request) { return } - <-ctx.Done() + // NOTE: we need to wait for conn closed, or the connection will be closed. + <-conn.closed }