Files
magpie/internal/stall/stall.go
T
yetone e484b4ff26 update: a slow download still moving is never ended by a total time; Bun, Claude Code and pf-serve too
#5572: since 80a47abba a silent download is broken off after 30s and
tried again, but the update's download still had a 10-minute total
(the update client's Timeout, the GUI run's ctx, `magpie update`'s ctx).
A download under ~140 KB/s, still receiving bytes, failed the 83 MB
Windows exe at 10 minutes part way. The stall timer is what ends a dead
one now; nothing ends one that is still moving.

- internal/update: fetch downloads through fileClient, the client without
  its Timeout. The feed and notes keep the 10-minute client. fetch's stall
  timer runs from the ask to the last byte, so a server that never answers
  still ends at stallAfter (30s), and download's retries stay bounded
  (len(retryWaits)+1 = 6 tries), so a connection that keeps going silent
  ends in about 4 minutes.
- internal/gui (Mac Stage, Windows/Linux StageBinary): run's 10 minutes
  (checkWait, a var for tests) bound only LatestIn; the download runs
  under a ctx with no deadline.
- internal/cli `magpie update` (Mac Stage, Windows/Linux ReplaceBinary):
  the same split.
- Siblings with the same fixed cap:
  - new internal/stall: Start(ctx, after) gives a ctx that ends with
    ErrStalled once `after` goes by with no byte read through w.Reader.
  - Bun (internal/plugin getURLOnce): watched with a 60s stall;
    KeepBunUpdated's 10-minute ctx around CheckBun is gone.
  - Claude Code (internal/claudecode downloadOnce, ~200 MB): watched with
    a 60s stall; keepOnce's 30-minute ctx around install is gone.
  - pf-serve (internal/pii upgrade): its 10-minute ctx is gone; get
    already had a 60s stall and the client a 60s header timeout, as the
    model's download (no total) does.
- Left as they are: plugin installs and npm updates (KeepUpdated's 10
  minutes) run `bun install`, whose network is Bun's own, small tarballs;
  rtk (5 min client), skills (2 min client), skilltree and market reads
  are small files; Grok CLI install has no cap of its own (its caller's
  ctx). None of them carries a large download behind a wall-clock total.

Run on mote (mote-run), at 49f207c78 + this change; the targeted
set again after rebasing on d4a221f79 (ok):
- go test ./internal/stall ./internal/update ./internal/claudecode
  ./internal/pii -run 'Stall|Slow|Silen|TestDownload|TestInstall|TestKeep';
  go test ./internal/plugin -run Bun; go test ./internal/gui -run
  TestUpdate: ok
- new: TestSlowButMovingIsReadWhole, TestSilenceIsAStall (no answer, part
  way), TestDownloadSlowButMovingOutlastsTheClientTimeout (client Timeout
  300ms, body ~1.2s: one try, no retry), TestUpdateSlowDownloadOutlastsTheCheckBound
  (checkWait 200ms, body ~600ms: ready), TestDownloadThatGoesSilentIsAStall
  (claudecode), TestBunDownloadThatGoesSilentIsAStall. The silent server
  after its retries stays covered by TestDownloadStallEveryTimeSaysSo
  (6 tries, errStalled).
- go test -race -count=3 ./internal/stall ./internal/update -run
  'Stall|Slow|Silen': ok
- win-run (Windows 11): ./internal/stall ./internal/update
  ./internal/claudecode ./internal/plugin with the same -run, and
  ./internal/gui -run 'TestUpdateSlowDownload|TestUpdateDownloadSaysStepAndSpeed': ok
- wsl-run (Ubuntu 24.04), -tags nogui: the same packages and ./internal/gui
  -run TestUpdate: ok
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build; GOOS=linux go build -tags nogui;
  GOOS=windows go build: clean
- Full suite: run after the push under the suite lock.

Broken by hand: fetch back on `client` fails
TestDownloadSlowButMovingOutlastsTheClientTimeout ("broke off ...
Client.Timeout or context cancellation while reading body"); the GUI's
download back under the check's ctx fails
TestUpdateSlowDownloadOutlastsTheCheckBound (state error, "context
deadline exceeded"); stall's reader not putting the timer off fails
TestSlowButMovingIsReadWhole (50 of 120 bytes); Bun's and Claude Code's
downloads unwatched fail their silent tests at the 20s bound ("context
deadline exceeded").

Not tried: a real update over a slow or throttled link, on any OS; a real
Bun or Claude Code download over one. The Mac's unpack and signature
check now run with no deadline (they had the 10 minutes before).

Fixes #5572
2026-11-07 06:09:42 +08:00

67 lines
1.8 KiB
Go

// Package stall ends a download that stops sending, and never one that is
// still moving, however slowly. A wall-clock cap on a whole download
// failed a slow link part way while its bytes were still coming (#5572:
// the 83 MB Windows update under ~140 KB/s against 10 minutes).
package stall
import (
"context"
"errors"
"fmt"
"io"
"time"
)
// ErrStalled is a download that got no byte for its Watch's while.
var ErrStalled = errors.New("no data came for a while")
// Watch is one download's idle timer.
type Watch struct {
after time.Duration
t *time.Timer
ctx context.Context
cancel context.CancelCauseFunc
}
// Start returns a ctx under ctx that ends, its cause ErrStalled, once
// after goes by with no byte read through w.Reader. The timer runs from
// now: a server that never answers ends the same way. Stop it when the
// download is over.
func Start(ctx context.Context, after time.Duration) (context.Context, *Watch) {
c, cancel := context.WithCancelCause(ctx)
w := &Watch{after: after, ctx: c, cancel: cancel}
w.t = time.AfterFunc(after, func() { cancel(ErrStalled) })
return c, w
}
// Reader is r, each byte read from it putting the stall off again.
func (w *Watch) Reader(r io.Reader) io.Reader { return &reader{r: r, w: w} }
// Stop ends the watch and the ctx Start gave.
func (w *Watch) Stop() {
w.t.Stop()
w.cancel(nil)
}
// Err is err, said as a stall (wrapping ErrStalled) when the watch is
// what ended the download.
func (w *Watch) Err(err error) error {
if err != nil && errors.Is(context.Cause(w.ctx), ErrStalled) {
return fmt.Errorf("%w (nothing for %s)", ErrStalled, w.after)
}
return err
}
type reader struct {
r io.Reader
w *Watch
}
func (r *reader) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
if n > 0 {
r.w.t.Reset(r.w.after)
}
return n, err
}