Author: Reinhard Tartler Subject: utils/ioutil: Respect context cancellation in NewContextReader/Writer In NewContextReader and NewContextWriter, reading or writing after context cancellation can non-deterministically succeed with data rather than returning (0, ctx.Err()) (Debian bug #1142868). In the v5.x series, these helpers delegate to the legacy, unmaintained github.com/jbenet/go-context/io (ctxio) library. Its Read() implementation selects between a worker goroutine channel and <-ctx.Done(). For non-blocking inputs like in-memory buffers, both channels are ready simultaneously, and Go's select pseudo-randomly picks the data channel instead of the cancelled context. In v6, upstream dropped the ctxio dependency (commit 21fd3e1d) and inlined context.go, fixing this race in commit 6e16689 (#1323) by verifying ctx.Err(). Rather than backporting the intrusive v6 refactor to v5.x, this patch adapts upstream's fix by wrapping ctxio in utils/ioutil/common.go to check ctx.Err() before and after I/O. Origin: vendor, based on https://github.com/go-git/go-git/commit/6e1668944ca7ecf52656c6044987c01b89b4d6c5 Bug: https://github.com/go-git/go-git/issues/1323 Bug-Debian: https://bugs.debian.org/1142868 Forwarded: not-needed Last-Update: 2026-09-21 --- utils/ioutil/common.go | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/utils/ioutil/common.go b/utils/ioutil/common.go index 235af71..5b7b1e6 100644 --- a/utils/ioutil/common.go +++ b/utils/ioutil/common.go @@ -131,30 +131,60 @@ func CheckClose(c io.Closer, err *error) { } } +type contextWriter struct { + ctx context.Context + w io.Writer +} + +func (w *contextWriter) Write(p []byte) (int, error) { + if err := w.ctx.Err(); err != nil { + return 0, err + } + n, err := w.w.Write(p) + if ctxErr := w.ctx.Err(); ctxErr != nil { + return 0, ctxErr + } + return n, err +} + +type contextReader struct { + ctx context.Context + r io.Reader +} + +func (r *contextReader) Read(p []byte) (int, error) { + if err := r.ctx.Err(); err != nil { + return 0, err + } + n, err := r.r.Read(p) + if ctxErr := r.ctx.Err(); ctxErr != nil { + return 0, ctxErr + } + return n, err +} + // NewContextWriter wraps a writer to make it respect given Context. // If there is a blocking write, the returned Writer will return whenever the // context is cancelled (the return values are n=0 and err=ctx.Err()). func NewContextWriter(ctx context.Context, w io.Writer) io.Writer { - return ctxio.NewWriter(ctx, w) + return &contextWriter{ctx: ctx, w: ctxio.NewWriter(ctx, w)} } // NewContextReader wraps a reader to make it respect given Context. // If there is a blocking read, the returned Reader will return whenever the // context is cancelled (the return values are n=0 and err=ctx.Err()). func NewContextReader(ctx context.Context, r io.Reader) io.Reader { - return ctxio.NewReader(ctx, r) + return &contextReader{ctx: ctx, r: ctxio.NewReader(ctx, r)} } // NewContextWriteCloser as NewContextWriter but with io.Closer interface. func NewContextWriteCloser(ctx context.Context, w io.WriteCloser) io.WriteCloser { - ctxw := ctxio.NewWriter(ctx, w) - return NewWriteCloser(ctxw, w) + return NewWriteCloser(NewContextWriter(ctx, w), w) } // NewContextReadCloser as NewContextReader but with io.Closer interface. func NewContextReadCloser(ctx context.Context, r io.ReadCloser) io.ReadCloser { - ctxr := ctxio.NewReader(ctx, r) - return NewReadCloser(ctxr, r) + return NewReadCloser(NewContextReader(ctx, r), r) } type readerOnError struct {