]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix leaked goroutines when webseed requests are cancelled
authorMatt Joiner <anacrolix@gmail.com>
Wed, 2 Jul 2025 05:04:40 +0000 (15:04 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 2 Jul 2025 05:12:18 +0000 (15:12 +1000)
webseed/client.go

index edf1ef6e9a2c140f7aa8c4b9147fa4a54ee6c217..ac58ec54ca706eb9a42d3eaf6559eddb9d10a38c 100644 (file)
@@ -39,13 +39,15 @@ type requestPart struct {
 
 type Request struct {
        cancel func()
-       // Closed in the machinery when cancelled?
-       Body io.Reader
-       err  chan error
+       Body   io.Reader
+       // Closed with error to unstick copy routine when context isn't checked.
+       bodyPipe *io.PipeReader
+       err      chan error
 }
 
 func (r Request) Cancel() {
        r.cancel()
+       r.bodyPipe.CloseWithError(context.Canceled)
 }
 
 type Client struct {
@@ -127,8 +129,9 @@ func (ws *Client) StartNewRequest(r RequestSpec) Request {
        }
        body, w := io.Pipe()
        req := Request{
-               cancel: cancel,
-               Body:   body,
+               cancel:   cancel,
+               Body:     body,
+               bodyPipe: body,
        }
        go func() {
                err := ws.readRequestPartResponses(ctx, w, requestParts)