#5572: since80a47abbaa 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), at49f207c78+ this change; the targeted set again after rebasing ond4a221f79(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
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package stall
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func get(ctx context.Context, url string, after time.Duration) (int, error) {
|
|
ctx, w := Start(ctx, after)
|
|
defer w.Stop()
|
|
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return 0, w.Err(err)
|
|
}
|
|
defer res.Body.Close()
|
|
b, err := io.ReadAll(w.Reader(res.Body))
|
|
return len(b), w.Err(err)
|
|
}
|
|
|
|
// A body that keeps coming, slowly, for many times the stall's while is
|
|
// read whole: only silence ends it (#5572).
|
|
func TestSlowButMovingIsReadWhole(t *testing.T) {
|
|
const chunks = 12
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
for i := 0; i < chunks; i++ {
|
|
if _, err := w.Write([]byte("0123456789")); err != nil {
|
|
return
|
|
}
|
|
w.(http.Flusher).Flush()
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
n, err := get(context.Background(), srv.URL, 250*time.Millisecond)
|
|
if err != nil || n != chunks*10 {
|
|
t.Fatalf("got %d bytes, err %v; want %d", n, err, chunks*10)
|
|
}
|
|
}
|
|
|
|
// A server that says nothing, before its answer or part way through it,
|
|
// ends with ErrStalled.
|
|
func TestSilenceIsAStall(t *testing.T) {
|
|
for name, h := range map[string]http.HandlerFunc{
|
|
"no answer": func(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() },
|
|
"part way": func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Length", "100")
|
|
w.Write([]byte("0123456789"))
|
|
w.(http.Flusher).Flush()
|
|
<-r.Context().Done()
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
srv := httptest.NewServer(h)
|
|
defer srv.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
_, err := get(ctx, srv.URL, 100*time.Millisecond)
|
|
if !errors.Is(err, ErrStalled) || ctx.Err() != nil {
|
|
t.Fatalf("err = %v, want a stall", err)
|
|
}
|
|
})
|
|
}
|
|
}
|