| 1 | Subject: [PATCH] Use last received image data for download timeouts |
| 2 | |
| 3 | Remove the total attempt deadline, renew the inactivity deadline only for body |
| 4 | data, and retain partial downloads for automatic and manual continuation. |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | @@ -12,6 +12,7 @@ |
| 11 | "os" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | + "sync" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/progressbar" |
| 18 | @@ -44,10 +45,8 @@ |
| 19 | if ctx.Err() != nil { |
| 20 | return ctx.Err() |
| 21 | } |
| 22 | - attemptCtx, cancel := context.WithTimeout(ctx, 30*time.Minute) |
| 23 | var retry bool |
| 24 | - retry, err = resumeHTTPOnce(attemptCtx, local, modified, contentType, remote, description, expected) |
| 25 | - cancel() |
| 26 | + retry, err = resumeHTTPOnce(ctx, local, modified, contentType, remote, description, expected, time.Minute) |
| 27 | if err == nil { |
| 28 | return publishPartial(part, local, identity) |
| 29 | } |
| 30 | @@ -73,7 +72,7 @@ |
| 31 | return os.Remove(identity) |
| 32 | } |
| 33 | |
| 34 | -func resumeHTTPOnce(ctx context.Context, local, modified, contentType, remote, description string, expected digest.Digest) (bool, error) { |
| 35 | +func resumeHTTPOnce(ctx context.Context, local, modified, contentType, remote, description string, expected digest.Digest, idleTimeout time.Duration) (bool, error) { |
| 36 | part := local + ".part" |
| 37 | var offset int64 |
| 38 | if st, err := os.Stat(part); err == nil { |
| 39 | @@ -83,10 +82,10 @@ |
| 40 | } |
| 41 | ctx, cancel := context.WithCancel(ctx) |
| 42 | defer cancel() |
| 43 | - // A slow but progressing transfer may run for 30 minutes. A stalled |
| 44 | - // connection is interrupted after one minute so a fresh attempt can resume. |
| 45 | - idle := time.AfterFunc(time.Minute, cancel) |
| 46 | - defer idle.Stop() |
| 47 | + // Only received body bytes renew the timeout. Headers and progress messages |
| 48 | + // cannot keep a stalled transfer alive; a progressing transfer has no cap. |
| 49 | + idle := newDownloadIdleWatchdog(idleTimeout, cancel) |
| 50 | + defer idle.stop() |
| 51 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, remote, nil) |
| 52 | if err != nil { |
| 53 | return false, err |
| 54 | @@ -156,7 +155,7 @@ |
| 55 | logrus.Infof("Downloading %s; continuing at %d bytes", description, offset) |
| 56 | bar.Start() |
| 57 | defer bar.Finish() |
| 58 | - reader := &downloadActivityReader{reader: resp.Body, timer: idle} |
| 59 | + reader := &downloadActivityReader{reader: resp.Body, activity: idle.touch} |
| 60 | // The desktop workbench consumes complete lines when stderr is a pipe. |
| 61 | // Range responses count the bytes already present in the partial file. |
| 62 | if os.Getenv("CAGE_SETUP_PROGRESS") == "1" { |
| 63 | @@ -169,6 +168,7 @@ |
| 64 | reader.lastReport = time.Now() |
| 65 | } |
| 66 | n, err := io.Copy(out, bar.NewProxyReader(reader)) |
| 67 | + idle.stop() // Local syncing and checksum validation are not network waits. |
| 68 | if err != nil { |
| 69 | return true, err |
| 70 | } |
| 71 | @@ -202,7 +202,7 @@ |
| 72 | |
| 73 | type downloadActivityReader struct { |
| 74 | reader io.Reader |
| 75 | - timer *time.Timer |
| 76 | + activity func() |
| 77 | received, total int64 |
| 78 | report func(int64, int64) |
| 79 | lastReport time.Time |
| 80 | @@ -211,7 +211,7 @@ |
| 81 | func (r *downloadActivityReader) Read(p []byte) (int, error) { |
| 82 | n, err := r.reader.Read(p) |
| 83 | if n > 0 { |
| 84 | - r.timer.Reset(time.Minute) |
| 85 | + r.activity() |
| 86 | if r.report != nil { |
| 87 | r.received += int64(n) |
| 88 | if time.Since(r.lastReport) >= time.Second { |
| 89 | @@ -223,6 +223,48 @@ |
| 90 | return n, err |
| 91 | } |
| 92 | |
| 93 | +// Check the latest receive time under the same lock as touch. An expired |
| 94 | +// callback that was already scheduled must not cancel a renewed download. |
| 95 | +type downloadIdleWatchdog struct { |
| 96 | + mu sync.Mutex |
| 97 | + timer *time.Timer |
| 98 | + last time.Time |
| 99 | + timeout time.Duration |
| 100 | + cancel context.CancelFunc |
| 101 | + stopped bool |
| 102 | +} |
| 103 | + |
| 104 | +func newDownloadIdleWatchdog(timeout time.Duration, cancel context.CancelFunc) *downloadIdleWatchdog { |
| 105 | + w := &downloadIdleWatchdog{last: time.Now(), timeout: timeout, cancel: cancel} |
| 106 | + w.timer = time.AfterFunc(timeout, func() { |
| 107 | + w.mu.Lock() |
| 108 | + defer w.mu.Unlock() |
| 109 | + if w.stopped { |
| 110 | + return |
| 111 | + } |
| 112 | + if remaining := w.timeout - time.Since(w.last); remaining > 0 { |
| 113 | + w.timer.Reset(remaining) |
| 114 | + } else { |
| 115 | + w.stopped = true |
| 116 | + w.cancel() |
| 117 | + } |
| 118 | + }) |
| 119 | + return w |
| 120 | +} |
| 121 | + |
| 122 | +func (w *downloadIdleWatchdog) touch() { |
| 123 | + w.mu.Lock() |
| 124 | + defer w.mu.Unlock() |
| 125 | + w.last = time.Now() |
| 126 | +} |
| 127 | + |
| 128 | +func (w *downloadIdleWatchdog) stop() { |
| 129 | + w.mu.Lock() |
| 130 | + defer w.mu.Unlock() |
| 131 | + w.stopped = true |
| 132 | + w.timer.Stop() |
| 133 | +} |
| 134 | + |
| 135 | func parseDownloadRange(value string) (start, end, total int64, ok bool) { |
| 136 | span, size, found := strings.Cut(strings.TrimPrefix(value, "bytes "), "/") |
| 137 | first, last, dash := strings.Cut(span, "-") |
| 138 | |
| 139 | |
| 140 | |
| 141 | @@ -182,3 +182,56 @@ |
| 142 | t.Fatal("cancelled data published") |
| 143 | } |
| 144 | } |
| 145 | + |
| 146 | +func TestPinnedDownloadUsesLatestBodyActivity(t *testing.T) { |
| 147 | + for _, mode := range []string{"slow", "stalled", "headers-only", "no-response"} { |
| 148 | + t.Run(mode, func(t *testing.T) { |
| 149 | + const data = "abcdef" |
| 150 | + const idle = 200 * time.Millisecond |
| 151 | + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 152 | + if mode != "no-response" { |
| 153 | + w.Header().Set("Content-Length", fmt.Sprint(len(data))) |
| 154 | + w.(http.Flusher).Flush() |
| 155 | + } |
| 156 | + if mode == "headers-only" || mode == "no-response" { |
| 157 | + <-r.Context().Done() |
| 158 | + return |
| 159 | + } |
| 160 | + for i := range len(data) { |
| 161 | + fmt.Fprint(w, data[i:i+1]) |
| 162 | + w.(http.Flusher).Flush() |
| 163 | + if mode == "stalled" { |
| 164 | + <-r.Context().Done() |
| 165 | + return |
| 166 | + } |
| 167 | + select { |
| 168 | + case <-r.Context().Done(): |
| 169 | + return |
| 170 | + case <-time.After(idle / 3): |
| 171 | + } |
| 172 | + } |
| 173 | + })) |
| 174 | + defer s.Close() |
| 175 | + local := filepath.Join(t.TempDir(), "data") |
| 176 | + start := time.Now() |
| 177 | + retry, err := resumeHTTPOnce(t.Context(), local, "", "", s.URL, "test", digest.FromString(data), idle) |
| 178 | + if mode == "slow" { |
| 179 | + if err != nil || retry || time.Since(start) <= idle { |
| 180 | + t.Fatalf("progressing transfer interrupted: retry=%v err=%v", retry, err) |
| 181 | + } |
| 182 | + got, err := os.ReadFile(local + ".part") |
| 183 | + if err != nil || string(got) != data { |
| 184 | + t.Fatalf("received %q: %v", got, err) |
| 185 | + } |
| 186 | + } else if !retry || err == nil { |
| 187 | + t.Fatalf("stalled transfer not retried: retry=%v err=%v", retry, err) |
| 188 | + } |
| 189 | + if mode == "stalled" { |
| 190 | + got, err := os.ReadFile(local + ".part") |
| 191 | + if err != nil || string(got) != data[:1] { |
| 192 | + t.Fatalf("partial data lost: %q %v", got, err) |
| 193 | + } |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | +} |