Debian Codegraph @

debian/patches/0007-utils-ioutil-Respect-context-cancellation-after-read.patch

raw
1 Author: Reinhard Tartler <[email protected]>
2 Subject: utils/ioutil: Respect context cancellation in NewContextReader/Writer
3
4 In NewContextReader and NewContextWriter, reading or writing after context
5 cancellation can non-deterministically succeed with data rather than returning
6 (0, ctx.Err()) (Debian bug #1142868).
7
8 In the v5.x series, these helpers delegate to the legacy, unmaintained
9 github.com/jbenet/go-context/io (ctxio) library. Its Read() implementation
10 selects between a worker goroutine channel and <-ctx.Done(). For non-blocking
11 inputs like in-memory buffers, both channels are ready simultaneously, and Go's
12 select pseudo-randomly picks the data channel instead of the cancelled context.
13
14 In v6, upstream dropped the ctxio dependency (commit 21fd3e1d) and inlined
15 context.go, fixing this race in commit 6e16689 (#1323) by verifying ctx.Err().
16 Rather than backporting the intrusive v6 refactor to v5.x, this patch adapts
17 upstream's fix by wrapping ctxio in utils/ioutil/common.go to check ctx.Err()
18 before and after I/O.
19
20 Origin: vendor, based on https://github.com/go-git/go-git/commit/6e1668944ca7ecf52656c6044987c01b89b4d6c5
21 Bug: https://github.com/go-git/go-git/issues/1323
22 Bug-Debian: https://bugs.debian.org/1142868
23 Forwarded: not-needed
24 Last-Update: 2026-09-21
25 ---
26 utils/ioutil/common.go | 42 ++++++++++++++++++++++++++++++++++++------
27 1 file changed, 36 insertions(+), 6 deletions(-)
28
29 diff --git a/utils/ioutil/common.go b/utils/ioutil/common.go
30 index 235af71..5b7b1e6 100644
31 --- a/utils/ioutil/common.go
32 +++ b/utils/ioutil/common.go
33 @@ -131,30 +131,60 @@ func CheckClose(c io.Closer, err *error) {
34 }
35 }
36
37 +type contextWriter struct {
38 + ctx context.Context
39 + w io.Writer
40 +}
41 +
42 +func (w *contextWriter) Write(p []byte) (int, error) {
43 + if err := w.ctx.Err(); err != nil {
44 + return 0, err
45 + }
46 + n, err := w.w.Write(p)
47 + if ctxErr := w.ctx.Err(); ctxErr != nil {
48 + return 0, ctxErr
49 + }
50 + return n, err
51 +}
52 +
53 +type contextReader struct {
54 + ctx context.Context
55 + r io.Reader
56 +}
57 +
58 +func (r *contextReader) Read(p []byte) (int, error) {
59 + if err := r.ctx.Err(); err != nil {
60 + return 0, err
61 + }
62 + n, err := r.r.Read(p)
63 + if ctxErr := r.ctx.Err(); ctxErr != nil {
64 + return 0, ctxErr
65 + }
66 + return n, err
67 +}
68 +
69 // NewContextWriter wraps a writer to make it respect given Context.
70 // If there is a blocking write, the returned Writer will return whenever the
71 // context is cancelled (the return values are n=0 and err=ctx.Err()).
72 func NewContextWriter(ctx context.Context, w io.Writer) io.Writer {
73 - return ctxio.NewWriter(ctx, w)
74 + return &contextWriter{ctx: ctx, w: ctxio.NewWriter(ctx, w)}
75 }
76
77 // NewContextReader wraps a reader to make it respect given Context.
78 // If there is a blocking read, the returned Reader will return whenever the
79 // context is cancelled (the return values are n=0 and err=ctx.Err()).
80 func NewContextReader(ctx context.Context, r io.Reader) io.Reader {
81 - return ctxio.NewReader(ctx, r)
82 + return &contextReader{ctx: ctx, r: ctxio.NewReader(ctx, r)}
83 }
84
85 // NewContextWriteCloser as NewContextWriter but with io.Closer interface.
86 func NewContextWriteCloser(ctx context.Context, w io.WriteCloser) io.WriteCloser {
87 - ctxw := ctxio.NewWriter(ctx, w)
88 - return NewWriteCloser(ctxw, w)
89 + return NewWriteCloser(NewContextWriter(ctx, w), w)
90 }
91
92 // NewContextReadCloser as NewContextReader but with io.Closer interface.
93 func NewContextReadCloser(ctx context.Context, r io.ReadCloser) io.ReadCloser {
94 - ctxr := ctxio.NewReader(ctx, r)
95 - return NewReadCloser(ctxr, r)
96 + return NewReadCloser(NewContextReader(ctx, r), r)
97 }
98
99 type readerOnError struct {