Repository navigation
fix(media): download on Windows without truncating an append handle - #234
Merged
Merged
Conversation
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]>
Merged
This was referenced Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
tg media downloadfailed on every Windows run withEPERM: operation not permitted, ftruncatebefore the firstupload.getFile(#233).streamToCacheopened the partial download with"a"and then calledhandle.truncate(offset). On Windows, Bun 1.4.3 refusesftruncateon an append handle, even with offset 0. Writes,sync()andstat()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/onwindows-latestwith Bun 1.4.3 frompackage.json, the runtime of the release binary. The probe also ran as a--compile --target=bun-windows-x64exe.bun runand the compiled exe:open(new, "a")+handle.truncate(0)givesEPERM ftruncateand leaves a 0-byte file, as in the issue. Append +sync()+stat()work.truncate(path, 4096)andtruncate(path, 0)work, and so does a later append.EPERM ftruncate, including fresh download, resume from the last whole part and start over when the partial is too large.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