Skip to content

Settle a failed download only after its partial file is closed and removed - #804

Merged
Zaldaryon merged 1 commit into
devfrom
fix/download-refusal-cleanup-windows
Nov 11, 2026
Merged

Zaldaryon merged 1 commit into
devfrom
fix/download-refusal-cleanup-windows

Conversation

@Pixnop

@Pixnop Pixnop commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Summary

The Windows, Node 26 leg of the CI on #801 failed once in tests/ipc/download.test.ts ("refuses a streamed response once it exceeds its request-specific ceiling", run 42904847462). The error was ENOTEMPTY, thrown by the test's own cleanup when it removed its temporary folder. #801 does not touch that test, and a rerun of the same leg passed.

The cause is in the product, not the test. When a download failed, fail() in src/ipc/workers/download.ts destroyed the write stream, started removing the .part file without waiting for it, and rejected right away. The stream closes asynchronously, so runDownload rejected while the partial file was still open and its removal was still pending. Every failure path goes through fail(): size ceiling, digest mismatch, request errors and the rest.

On Windows, a caller that removes the folder or retries right after the rejection can hit ENOTEMPTY or EBUSY on a file that is still open, or find a leftover .part file. A player could meet this by retrying a failed download straight away, or by cleaning up after one.

Now fail() closes the stream, then removes the .part file, then rejects. Success, cancel, the error type and the rules about which files are left behind are unchanged.

Type

  • Bug fix
  • Feature
  • Performance
  • Refactor or cleanup
  • Tests only
  • Docs or build

Checklist

  • Targets dev, not main.
  • npm run typecheck passes.
  • npm run lint:ci passes.
  • npm run format:check passes.
  • npm run test:coverage passes, coverage at or above the floor in vitest.config.ts.
  • npm run build:unpack passes.

I did not run npm run build:unpack locally. The packaged build is left to CI.

Testing

On dev, the .part file was still on disk at the moment of rejection in 40 runs out of 40.

I added assert.deepEqual(leftoverParts(), []) to the existing size ceiling test, and a new test that checks the partial file is gone when a digest mismatch rejects. Run against dev, both fail, and they failed on all three runs. With the fix, tests/ipc/download.test.ts passes (40 tests).

Full run: typecheck clean, lint with 0 errors (12 warnings, none in files I touched), format check clean. test:coverage: 303 files, 5911 tests passed and 4 skipped. Coverage is 95.08% statements, 91.3% branches, 95.89% functions and 97.02% lines.

Windows itself was only exercised by CI, not by me.

Related issues

None.

@Pixnop
Pixnop requested a review from Zaldaryon November 10, 2026 21:46

@Zaldaryon Zaldaryon left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure path installs its close listener before destroying the writer, waits for that close, then attempts removal before rejecting. The size-limit and digest-mismatch tests both assert that no partial file remains when rejection reaches the caller. Focused tests passed: 40. Required CI checks are green.

@Zaldaryon
Zaldaryon merged commit 7500260 into dev Nov 11, 2026
13 checks passed
@Zaldaryon
Zaldaryon deleted the fix/download-refusal-cleanup-windows branch November 11, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants