]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Try to show context cancellation cause after io.ReadFull
authorMatt Joiner <anacrolix@gmail.com>
Tue, 5 Aug 2025 14:00:32 +0000 (00:00 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 5 Aug 2025 14:00:32 +0000 (00:00 +1000)
webseed-peer.go
webseed/client.go

index 5dcef0786beb8ffa5340829086591f97d2b2133e..b1ff7dd4446c5bf91276cf22ccc0813c6d380f0a 100644 (file)
@@ -288,6 +288,10 @@ func (ws *webseedPeer) readChunks(wr *webseedRequest) (err error) {
                var n int
                n, err = io.ReadFull(wr.request.Body, buf)
                ws.peer.readBytes(int64(n))
+               reqCtxErr := context.Cause(wr.request.Context())
+               if errors.Is(err, reqCtxErr) {
+                       err = reqCtxErr
+               }
                if webseed.PrintDebug && wr.cancelled.Load() {
                        fmt.Printf("webseed read %v after cancellation: %v\n", n, err)
                }
index dc5858bc299024e3faca566a47e2fe67b6239a7f..10c8fcdf216594b89f56923ce99f745b2d49c6b2 100644 (file)
@@ -46,17 +46,23 @@ type requestPart struct {
 }
 
 type Request struct {
+       // So you can view it from externally.
+       ctx    context.Context
        cancel context.CancelCauseFunc
        Body   io.Reader
        // Closed with error to unstick copy routine when context isn't checked.
        bodyPipe *io.PipeReader
 }
 
-func (r Request) Cancel(cause error) {
+func (r *Request) Context() context.Context {
+       return r.ctx
+}
+
+func (r *Request) Cancel(cause error) {
        r.cancel(cause)
 }
 
-func (r Request) Close() {
+func (r *Request) Close() {
        // We aren't cancelling because we want to know if we can keep receiving buffered data after
        // cancellation. PipeReader.Close always returns nil.
        _ = r.bodyPipe.Close()
@@ -144,6 +150,7 @@ func (ws *Client) StartNewRequest(ctx context.Context, r RequestSpec, debugLogge
        panicif.Zero(len(requestParts))
        body, w := io.Pipe()
        req := Request{
+               ctx:      ctx,
                cancel:   cancel,
                Body:     body,
                bodyPipe: body,