Skip to content

fix(media): download on Windows without truncating an append handle - #234

Merged
neoromantic merged 1 commit into
mainfrom
fix/233-windows-media-download
Nov 21, 2026
Merged

neoromantic merged 1 commit into
mainfrom
fix/233-windows-media-download

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What

tg media download failed on every Windows run with EPERM: operation not permitted, ftruncate before the first upload.getFile (#233).

streamToCache opened the partial download with "a" and then called handle.truncate(offset). On Windows, Bun 1.4.3 refuses ftruncate on an append handle, even with offset 0. Writes, sync() and stat() through the same handle work.

Now the partial download is truncated by path, only when it holds more than the resume offset (an unfinished part, or a file larger than the expected size). Then it is opened with "a" as before. The resume offset and the size check don't change.

Proof on Windows

A throwaway branch ran a probe and bun test src/commands/media/ on windows-latest with Bun 1.4.3 from package.json, the runtime of the release binary. The probe also ran as a --compile --target=bun-windows-x64 exe.

  • Probe, bun run and the compiled exe: open(new, "a") + handle.truncate(0) gives EPERM ftruncate and leaves a 0-byte file, as in the issue. Append + sync() + stat() work. truncate(path, 4096) and truncate(path, 0) work, and so does a later append.
  • Media tests before the fix: 16 of 38 fail with EPERM ftruncate, including fresh download, resume from the last whole part and start over when the partial is too large.
  • Media tests with the fix: 38 of 38 pass.

The existing tests cover the three cases: no partial download, resume from a whole part, start over. They catch this defect only on Windows. PR CI runs on Linux.

Not tested: a real Telegram download on Windows. The Telegram calls don't change.

Closes #233

🤖 Generated with Claude Code

Windows refuses ftruncate on a file opened with "a" (EPERM), so every
tg media download failed before the first request. Truncate the partial
download by path, only when it holds more than the resume offset, then
open it for appending.

Closes #233

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

tg media download fails on Windows: EPERM ftruncate (truncating an append handle)

1 participant