@@ -168,11 +168,15 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
168168 }
169169
170170 ctx , cancel := context .WithCancel (ctx )
171- timer := signal .CancelAfterInactivity (ctx , cancel , h .timeout )
171+ terminate := func () {
172+ cancel ()
173+ conn .Close ()
174+ }
175+ timer := signal .CancelAfterInactivity (ctx , terminate , h .timeout )
176+ defer timer .SetTimeout (0 )
172177
173178 request := func () error {
174- defer conn .Close ()
175-
179+ defer timer .SetTimeout (0 )
176180 for {
177181 b , err := reader .ReadMessage ()
178182 if err == io .EOF {
@@ -219,6 +223,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, d internet.
219223 }
220224
221225 response := func () error {
226+ defer timer .SetTimeout (0 )
222227 for {
223228 b , err := connReader .ReadMessage ()
224229 if err == io .EOF {
@@ -371,6 +376,7 @@ type outboundConn struct {
371376
372377 conn net.Conn
373378 connReady chan struct {}
379+ closed bool
374380}
375381
376382func (c * outboundConn ) dial () error {
@@ -385,12 +391,16 @@ func (c *outboundConn) dial() error {
385391
386392func (c * outboundConn ) Write (b []byte ) (int , error ) {
387393 c .access .Lock ()
394+ if c .closed {
395+ c .access .Unlock ()
396+ return 0 , errors .New ("outbound connection closed" )
397+ }
388398
389399 if c .conn == nil {
390400 if err := c .dial (); err != nil {
391401 c .access .Unlock ()
392402 errors .LogWarningInner (context .Background (), err , "failed to dial outbound connection" )
393- return len ( b ), nil
403+ return 0 , err
394404 }
395405 }
396406
@@ -418,6 +428,7 @@ func (c *outboundConn) Read(b []byte) (int, error) {
418428
419429func (c * outboundConn ) Close () error {
420430 c .access .Lock ()
431+ c .closed = true
421432 close (c .connReady )
422433 if c .conn != nil {
423434 c .conn .Close ()
0 commit comments