#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
342 lines
12 KiB
Go
342 lines
12 KiB
Go
package update
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A download that drops part way is tried again, and reports its progress.
|
|
func TestDownloadRetriesAndReports(t *testing.T) {
|
|
body := make([]byte, 256<<10)
|
|
for i := range body {
|
|
body[i] = byte(i)
|
|
}
|
|
sum := sha256.Sum256(body)
|
|
tries := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tries++
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
|
|
if tries == 1 {
|
|
w.Write(body[:1000]) // then the connection goes
|
|
return
|
|
}
|
|
w.Write(body)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
var done, total int64
|
|
ctx := WithProgress(context.Background(), func(d, t int64) { done, total = d, t })
|
|
path := filepath.Join(t.TempDir(), "magpie.new")
|
|
if err := download(ctx, Asset{URL: srv.URL, SHA256: hex.EncodeToString(sum[:])}, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tries != 2 {
|
|
t.Fatalf("tries = %d, want 2", tries)
|
|
}
|
|
if done != int64(len(body)) || total != int64(len(body)) {
|
|
t.Fatalf("progress = %d/%d, want %d/%d", done, total, len(body), len(body))
|
|
}
|
|
if b, _ := os.ReadFile(path); len(b) != len(body) {
|
|
t.Fatalf("file has %d bytes", len(b))
|
|
}
|
|
}
|
|
|
|
// quickRetries has download's tries follow each other at once.
|
|
func quickRetries(t *testing.T) {
|
|
t.Helper()
|
|
old := retryWaits
|
|
retryWaits = []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond, time.Millisecond, time.Millisecond}
|
|
t.Cleanup(func() { retryWaits = old })
|
|
}
|
|
|
|
// A download the connection cuts part way goes on from where it broke
|
|
// off: the next try asks for the rest with Range, and the hash is checked
|
|
// over the whole file, the bytes of the first try included.
|
|
func TestDownloadResumesWhereItBrokeOff(t *testing.T) {
|
|
quickRetries(t)
|
|
body := make([]byte, 512<<10)
|
|
for i := range body {
|
|
body[i] = byte(i * 7)
|
|
}
|
|
sum := sha256.Sum256(body)
|
|
var ranges []string
|
|
cut := []int{100 << 10, 300 << 10} // where the first two tries break off
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ranges = append(ranges, r.Header.Get("Range"))
|
|
from := 0
|
|
if rg := r.Header.Get("Range"); rg != "" {
|
|
from, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(rg, "bytes="), "-"))
|
|
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", from, len(body)-1, len(body)))
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)-from))
|
|
w.WriteHeader(http.StatusPartialContent)
|
|
} else {
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
|
|
}
|
|
to := len(body)
|
|
if n := len(ranges) - 1; n < len(cut) {
|
|
to = cut[n]
|
|
}
|
|
w.Write(body[from:to])
|
|
if to < len(body) {
|
|
w.(http.Flusher).Flush()
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
conn.Close() // mid-stream
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
var done, total int64
|
|
ctx := WithProgress(context.Background(), func(d, t int64) { done, total = d, t })
|
|
path := filepath.Join(t.TempDir(), "magpie.new")
|
|
if err := download(ctx, Asset{URL: srv.URL, SHA256: hex.EncodeToString(sum[:])}, path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []string{"", "bytes=102400-", "bytes=307200-"}
|
|
if !slices.Equal(ranges, want) {
|
|
t.Fatalf("ranges asked = %q, want %q", ranges, want)
|
|
}
|
|
if b, _ := os.ReadFile(path); !bytes.Equal(b, body) {
|
|
t.Fatalf("file has %d bytes, not the release's", len(b))
|
|
}
|
|
if done != int64(len(body)) || total != int64(len(body)) {
|
|
t.Fatalf("progress = %d/%d, want %d/%d", done, total, len(body), len(body))
|
|
}
|
|
}
|
|
|
|
// The rest of a file sent with other bytes than the release's (a host
|
|
// that resumes wrongly) fails the hash, which covers the whole file, and
|
|
// nothing is kept.
|
|
func TestDownloadResumedFileIsStillChecked(t *testing.T) {
|
|
quickRetries(t)
|
|
body := bytes.Repeat([]byte("magpie"), 50<<10)
|
|
sum := sha256.Sum256(body)
|
|
tries := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tries++
|
|
if tries == 1 {
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
|
|
w.Write(body[:1000])
|
|
w.(http.Flusher).Flush()
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
conn.Close()
|
|
return
|
|
}
|
|
w.Header().Set("Content-Range", fmt.Sprintf("bytes 1000-%d/%d", len(body)-1, len(body)))
|
|
w.WriteHeader(http.StatusPartialContent)
|
|
w.Write(bytes.Repeat([]byte("x"), len(body)-1000))
|
|
}))
|
|
defer srv.Close()
|
|
path := filepath.Join(t.TempDir(), "magpie.new")
|
|
err := download(context.Background(), Asset{URL: srv.URL, SHA256: hex.EncodeToString(sum[:])}, path)
|
|
if err == nil || !strings.Contains(err.Error(), "does not match its checksum") {
|
|
t.Fatalf("err = %v, want a checksum mismatch", err)
|
|
}
|
|
if _, serr := os.Stat(path); !os.IsNotExist(serr) {
|
|
t.Fatal("the mismatched file was kept")
|
|
}
|
|
}
|
|
|
|
// A connection dropped before any answer (EOF at the connect, what the
|
|
// reporter's Windows box got from GitHub's asset host) is tried again
|
|
// every time retryWaits allows, and the error that ends it names the file
|
|
// and the cause, not the signed address it was redirected to.
|
|
func TestDownloadDroppedConnectionErrorKeepsAddressOut(t *testing.T) {
|
|
quickRetries(t)
|
|
tries := 0
|
|
assets := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tries++
|
|
conn, _, _ := w.(http.Hijacker).Hijack()
|
|
conn.Close() // EOF, no answer at all
|
|
}))
|
|
defer assets.Close()
|
|
signed := assets.URL + "/github-production-release-asset/1?sig=SECRETSIG&jwt=SECRETJWT"
|
|
gh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, signed, http.StatusFound)
|
|
}))
|
|
defer gh.Close()
|
|
path := filepath.Join(t.TempDir(), "magpie.new")
|
|
err := download(context.Background(), Asset{URL: gh.URL + "/magpie-windows-amd64.exe", SHA256: "00"}, path)
|
|
if err == nil || !Network(err) {
|
|
t.Fatalf("err = %v, want a network error", err)
|
|
}
|
|
if tries != len(retryWaits)+1 {
|
|
t.Fatalf("tries = %d, want %d", tries, len(retryWaits)+1)
|
|
}
|
|
if msg := err.Error(); strings.Contains(msg, "SECRET") || strings.Contains(msg, "http") || !strings.Contains(msg, "magpie.new") || !strings.Contains(msg, "EOF") {
|
|
t.Fatalf("error = %q", msg)
|
|
}
|
|
if _, serr := os.Stat(path); !os.IsNotExist(serr) {
|
|
t.Fatal("a partial file was kept")
|
|
}
|
|
}
|
|
|
|
// A refusal that isn't the network's (404: not uploaded yet) isn't tried
|
|
// again and again.
|
|
func TestDownloadNotFoundIsNotRetried(t *testing.T) {
|
|
quickRetries(t)
|
|
tries := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tries++
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer srv.Close()
|
|
err := download(context.Background(), Asset{URL: srv.URL, SHA256: "00"}, filepath.Join(t.TempDir(), "magpie.new"))
|
|
if err == nil || Network(err) || tries != 1 {
|
|
t.Fatalf("err = %v after %d tries, want one 404", err, tries)
|
|
}
|
|
}
|
|
|
|
// A connection that goes silent part way, its bytes held back with the
|
|
// connection kept open (what left esir_tech's update at 17% on Discord),
|
|
// is broken off after stallAfter and tried again from where it stopped,
|
|
// and the steps say so: the download, the wait to try again, the rest,
|
|
// then the checksum.
|
|
func TestDownloadStallIsTriedAgain(t *testing.T) {
|
|
quickRetries(t)
|
|
old := stallAfter
|
|
stallAfter = 300 * time.Millisecond
|
|
t.Cleanup(func() { stallAfter = old })
|
|
body := make([]byte, 600<<10)
|
|
for i := range body {
|
|
body[i] = byte(i * 13)
|
|
}
|
|
sum := sha256.Sum256(body)
|
|
cut := len(body) * 17 / 100
|
|
var mu sync.Mutex
|
|
var ranges []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
ranges = append(ranges, r.Header.Get("Range"))
|
|
first := len(ranges) == 1
|
|
mu.Unlock()
|
|
if first {
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
|
|
w.Write(body[:cut])
|
|
w.(http.Flusher).Flush()
|
|
<-r.Context().Done() // silent, the connection open, until the client goes
|
|
return
|
|
}
|
|
from, _ := strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(r.Header.Get("Range"), "bytes="), "-"))
|
|
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", from, len(body)-1, len(body)))
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)-from))
|
|
w.WriteHeader(http.StatusPartialContent)
|
|
w.Write(body[from:])
|
|
}))
|
|
defer srv.Close()
|
|
|
|
var steps []string
|
|
var done, total int64
|
|
ctx := WithProgress(context.Background(), func(d, t int64) { done, total = d, t })
|
|
ctx = WithStep(ctx, func(s string) {
|
|
if len(steps) == 0 || steps[len(steps)-1] != s {
|
|
steps = append(steps, s)
|
|
}
|
|
})
|
|
// the update's own bound, far over the stall's, is what the old code
|
|
// waited out
|
|
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
|
|
defer cancel()
|
|
path := filepath.Join(t.TempDir(), "magpie.new")
|
|
start := time.Now()
|
|
if err := download(ctx, Asset{URL: srv.URL, Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])}, path); err != nil {
|
|
t.Fatalf("after %s at %d of %d bytes: %v", time.Since(start).Round(time.Millisecond), done, total, err)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if want := []string{"", fmt.Sprintf("bytes=%d-", cut)}; !slices.Equal(ranges, want) {
|
|
t.Fatalf("ranges asked = %q, want %q", ranges, want)
|
|
}
|
|
if b, _ := os.ReadFile(path); !bytes.Equal(b, body) {
|
|
t.Fatalf("file has %d bytes, not the release's", len(b))
|
|
}
|
|
if want := []string{StepDownload, StepRetry, StepDownload, StepVerify}; !slices.Equal(steps, want) {
|
|
t.Fatalf("steps = %q, want %q", steps, want)
|
|
}
|
|
}
|
|
|
|
// A stall that never ends is the network's, said so: tried every time
|
|
// retryWaits allows, and the error names what happened.
|
|
func TestDownloadStallEveryTimeSaysSo(t *testing.T) {
|
|
quickRetries(t)
|
|
old := stallAfter
|
|
stallAfter = 50 * time.Millisecond
|
|
t.Cleanup(func() { stallAfter = old })
|
|
var tries atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tries.Add(1)
|
|
<-r.Context().Done() // no answer at all
|
|
}))
|
|
defer srv.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
err := download(ctx, Asset{URL: srv.URL, SHA256: "00"}, filepath.Join(t.TempDir(), "magpie.new"))
|
|
if err == nil || !Network(err) || !errors.Is(err, errStalled) || ctx.Err() != nil {
|
|
t.Fatalf("err = %v, want a stall", err)
|
|
}
|
|
if n := int(tries.Load()); n != len(retryWaits)+1 {
|
|
t.Fatalf("tries = %d, want %d", n, len(retryWaits)+1)
|
|
}
|
|
}
|
|
|
|
// A download still moving, however slowly, is never ended by a total time
|
|
// (#5572: the 83 MB Windows exe under ~140 KB/s failed at the client's 10
|
|
// minutes). Here the feed's client gives up after 300ms and the body
|
|
// takes ~1.2s, a byte every 100ms well inside the stall's while: one try
|
|
// reads it whole, with no break and no retry.
|
|
func TestDownloadSlowButMovingOutlastsTheClientTimeout(t *testing.T) {
|
|
quickRetries(t)
|
|
oldStall, oldClient := stallAfter, client
|
|
stallAfter = time.Second
|
|
client = &http.Client{Timeout: 300 * time.Millisecond, Transport: oldClient.Transport}
|
|
t.Cleanup(func() { stallAfter, client = oldStall, oldClient })
|
|
const chunk, chunks = 4 << 10, 12
|
|
body := make([]byte, chunk*chunks)
|
|
for i := range body {
|
|
body[i] = byte(i * 11)
|
|
}
|
|
sum := sha256.Sum256(body)
|
|
var tries atomic.Int32
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tries.Add(1)
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(body)))
|
|
for i := 0; i < chunks; i++ {
|
|
if _, err := w.Write(body[i*chunk : (i+1)*chunk]); err != nil {
|
|
return
|
|
}
|
|
w.(http.Flusher).Flush()
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
var steps []string
|
|
ctx := WithStep(context.Background(), func(s string) { steps = append(steps, s) })
|
|
path := filepath.Join(t.TempDir(), "magpie.new")
|
|
if err := download(ctx, Asset{URL: srv.URL, Size: int64(len(body)), SHA256: hex.EncodeToString(sum[:])}, path); err != nil {
|
|
t.Fatalf("a moving download failed: %v", err)
|
|
}
|
|
if n := tries.Load(); n != 1 {
|
|
t.Fatalf("asked %d times, want one try that wasn't broken off", n)
|
|
}
|
|
if want := []string{StepDownload, StepVerify}; !slices.Equal(steps, want) {
|
|
t.Fatalf("steps = %q, want %q", steps, want)
|
|
}
|
|
if b, _ := os.ReadFile(path); !bytes.Equal(b, body) {
|
|
t.Fatalf("file has %d bytes, not the release's", len(b))
|
|
}
|
|
}
|