@@ -131,15 +131,22 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
131131
132132 behaviorSeed := crc64 .Checksum (hashkdf .Sum (nil ), crc64 .MakeTable (crc64 .ISO ))
133133
134+ var newCtx context.Context
135+ var newCancel context.CancelFunc
134136 if session .TimeoutOnlyFromContext (ctx ) {
135- ctx = context .WithoutCancel ( ctx )
137+ newCtx , newCancel = context .WithCancel ( context . Background () )
136138 }
137139
138140 session := encoding .NewClientSession (ctx , int64 (behaviorSeed ))
139141 sessionPolicy := h .policyManager .ForLevel (request .User .Level )
140142
141143 ctx , cancel := context .WithCancel (ctx )
142- timer := signal .CancelAfterInactivity (ctx , cancel , sessionPolicy .Timeouts .ConnectionIdle )
144+ timer := signal .CancelAfterInactivity (ctx , func () {
145+ cancel ()
146+ if newCancel != nil {
147+ newCancel ()
148+ }
149+ }, sessionPolicy .Timeouts .ConnectionIdle )
143150
144151 if request .Command == protocol .RequestCommandUDP && h .cone && request .Port != 53 && request .Port != 443 {
145152 request .Command = protocol .RequestCommandMux
@@ -205,6 +212,10 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
205212 return buf .Copy (bodyReader , output , buf .UpdateActivity (timer ))
206213 }
207214
215+ if newCtx != nil {
216+ ctx = newCtx
217+ }
218+
208219 responseDonePost := task .OnSuccess (responseDone , task .Close (output ))
209220 if err := task .Run (ctx , requestDone , responseDonePost ); err != nil {
210221 return errors .New ("connection ends" ).Base (err )
0 commit comments