@@ -95,7 +95,7 @@ type CloseWriter interface {
9595}
9696
9797type hijackHandler struct {
98- cb func (ctx context.Context , rw io.ReadWriter )
98+ cb func (ctx context.Context , conn io.ReadWriteCloser )
9999 ackTimeout time.Duration
100100 logger logging.Logger
101101}
@@ -131,7 +131,7 @@ func (h *hijackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
131131 return
132132 }
133133
134- defer conn .Close ()
134+ defer func () { _ = conn .Close () }() // Might have already been called inside the callback -> ignore double close error
135135
136136 done := make (chan struct {})
137137 go func () {
@@ -153,7 +153,8 @@ func (h *hijackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
153153 return
154154 }
155155
156- // Important: The server still owns net.Conn! When this handler returns, conn gets closed.
156+ // The server still owns net.Conn. When this handler returns, conn gets closed.
157+ // However, the callback might have to call Close() to abort/unblock any pending/blocking Read().
157158 h .cb (r .Context (), conn )
158159 }()
159160
@@ -163,6 +164,6 @@ func (h *hijackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
163164 }
164165}
165166
166- func NewHijackAcceptor (logger logging.Logger , cb func (context.Context , io.ReadWriter )) (string , http.Handler ) {
167+ func NewHijackAcceptor (logger logging.Logger , cb func (context.Context , io.ReadWriteCloser )) (string , http.Handler ) {
167168 return hijackPath , & hijackHandler {logger : logger , cb : cb , ackTimeout : hijackTimeout }
168169}
0 commit comments