From: Matt Joiner Date: Tue, 5 Aug 2025 14:00:32 +0000 (+1000) Subject: Try to show context cancellation cause after io.ReadFull X-Git-Tag: v1.59.0~2^2~45 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=23f52690acff5b6fe540603984db95eac2564097;p=btrtrc.git Try to show context cancellation cause after io.ReadFull --- diff --git a/webseed-peer.go b/webseed-peer.go index 5dcef078..b1ff7dd4 100644 --- a/webseed-peer.go +++ b/webseed-peer.go @@ -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) } diff --git a/webseed/client.go b/webseed/client.go index dc5858bc..10c8fcdf 100644 --- a/webseed/client.go +++ b/webseed/client.go @@ -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,