From dadbbc7649cfcbcdedf2ca79b98ba630adc1a834 Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 2 Oct 2026 22:45:32 +0800 Subject: [PATCH] fix: close temp downloads before removing them on error (#340) Both release downloaders removed the temp file while it was still open, relying on the deferred Close. Windows refuses to delete an open file, so an interrupted or oversized download could leave up to ~100 MB behind in %TEMP%. Closing first makes the cleanup work on every platform. Raised in review of #335; downloadToTemp in uidist had the same pattern. --- cmd/skillshare/upgrade.go | 2 ++ cmd/skillshare/upgrade_checksum_test.go | 27 +++++++++++++++++++++++++ internal/uidist/uidist.go | 2 ++ 3 files changed, 31 insertions(+) diff --git a/cmd/skillshare/upgrade.go b/cmd/skillshare/upgrade.go index 21a62e67c..388400fc1 100644 --- a/cmd/skillshare/upgrade.go +++ b/cmd/skillshare/upgrade.go @@ -462,10 +462,12 @@ func downloadArchive(url string, onProgress utils.ProgressFunc) (string, error) limited := io.LimitReader(utils.NewProgressReader(resp.Body, resp.ContentLength, onProgress), maxArchiveSize+1) n, err := io.Copy(tmp, limited) if err != nil { + tmp.Close() os.Remove(tmp.Name()) return "", err } if n > maxArchiveSize { + tmp.Close() os.Remove(tmp.Name()) return "", fmt.Errorf("archive exceeds the maximum size (%d MB)", maxArchiveSize/(1024*1024)) } diff --git a/cmd/skillshare/upgrade_checksum_test.go b/cmd/skillshare/upgrade_checksum_test.go index 677c995b1..c8c287181 100644 --- a/cmd/skillshare/upgrade_checksum_test.go +++ b/cmd/skillshare/upgrade_checksum_test.go @@ -162,3 +162,30 @@ func TestDownloadAndReplace_UnreachableChecksumsLeavesBinaryAlone(t *testing.T) t.Errorf("dest = %q, want the running binary untouched", got) } } + +func TestDownloadArchive_InterruptedDownloadLeavesNoTempFile(t *testing.T) { + tmpDir := t.TempDir() + t.Setenv("TMPDIR", tmpDir) + t.Setenv("TMP", tmpDir) + t.Setenv("TEMP", tmpDir) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Length", "1024") + w.Write([]byte("partial")) + w.(http.Flusher).Flush() + panic(http.ErrAbortHandler) + })) + defer srv.Close() + + if _, err := downloadArchive(srv.URL+"/"+releaseAsset, nil); err == nil { + t.Fatal("expected an error for an interrupted download") + } + + entries, err := os.ReadDir(tmpDir) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Fatalf("interrupted download left %d file(s) in the temp dir", len(entries)) + } +} diff --git a/internal/uidist/uidist.go b/internal/uidist/uidist.go index 598fc7871..da63b4807 100644 --- a/internal/uidist/uidist.go +++ b/internal/uidist/uidist.go @@ -138,10 +138,12 @@ func downloadToTemp(url string, onProgress utils.ProgressFunc) (string, error) { limited := io.LimitReader(utils.NewProgressReader(resp.Body, resp.ContentLength, onProgress), maxDownloadSize+1) n, err := io.Copy(tmp, limited) if err != nil { + tmp.Close() os.Remove(tmp.Name()) return "", err } if n > maxDownloadSize { + tmp.Close() os.Remove(tmp.Name()) return "", fmt.Errorf("download exceeds maximum size (%d MB)", maxDownloadSize/(1024*1024)) }