From 836e84b8510a9478bc00dd8690cb71a51a607d11 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+RPRX@users.noreply.github.com> Date: Wed, 8 Mar 2023 14:06:20 +0000 Subject: [PATCH] Add recover() to H2 server's flushWriter.Write() Fixes https://github.com/XTLS/Xray-core/issues/1748 --- transport/internet/http/dialer.go | 8 +++----- transport/internet/http/hub.go | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/transport/internet/http/dialer.go b/transport/internet/http/dialer.go index 75adc249..010a95a5 100644 --- a/transport/internet/http/dialer.go +++ b/transport/internet/http/dialer.go @@ -204,7 +204,7 @@ type WaitReadCloser struct { func (w *WaitReadCloser) Set(rc io.ReadCloser) { w.ReadCloser = rc defer func() { - if err := recover(); err != nil { + if recover() != nil { rc.Close() } }() @@ -225,10 +225,8 @@ func (w *WaitReadCloser) Close() error { return w.ReadCloser.Close() } defer func() { - if err := recover(); err != nil { - if w.ReadCloser != nil { - w.ReadCloser.Close() - } + if recover() != nil && w.ReadCloser != nil { + w.ReadCloser.Close() } }() close(w.Wait) diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index 54abe298..551f897e 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -51,6 +51,13 @@ func (fw flushWriter) Write(p []byte) (n int, err error) { return 0, io.ErrClosedPipe } + defer func() { + if recover() != nil { + fw.d.Close() + err = io.ErrClosedPipe + } + }() + n, err = fw.w.Write(p) if f, ok := fw.w.(http.Flusher); ok && err == nil { f.Flush()