Skip to content

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

Closed

Description

@Andr-ey

What happens

Every tg media download on Windows fails, for photos and for documents:

$ tg media download --chat -1001420000565 --id 802
{
  "success": false,
  "error": {
    "code": "TELEGRAM_ERROR",
    "message": "Failed to download media: EPERM: operation not permitted, ftruncate",
    "retryable": false
  }
}

The partial file is created (0 bytes, .photo.jpg.part), then the download stops before the first upload.getFile call — tg status shows no upload.getFile traffic and no FLOOD_WAIT.

Tried and failed the same way:

  • a photo (802), a document (355), a video (487);
  • --out <dir>, --thumb, --overwrite;
  • repeated runs (the stale .part is not the cause).

Environment

  • Windows 11 x64 (10.0.26300), release binary tg-windows-x64.exe from v0.15.1
  • tg --version → 0.15.1, the bundled runtime reports mtcute on Bun/1.4.3, initConnection appVersion 0.32.6
  • account signed in by QR, readOnly: true, no daemon running
  • cache DB and media cache on a normal NTFS volume under %USERPROFILE%; the same directory accepts writes from PowerShell, Node (fs.ftruncateSync) and .NET (FileStream.SetLength), so it is not a file-system or ACL problem

Cause

src/commands/media/download-media.ts, streamToCache:

const handle = await open(part, "a", 0o600);
...
await handle.truncate(offset);

On Windows an "a" handle is opened with FILE_APPEND_DATA and without FILE_WRITE_DATA, and SetEndOfFile on such a handle fails with EPERM. offset is 0 here, so the truncate is a no-op on POSIX and fatal on Windows. The .part file is created by the open and stays 0 bytes, which matches what I see on disk.

Suggested fix

Do not truncate through the append handle. Any of these should do it:

  • open with "r+" (create first when missing) and pass { flags } only for appends, or
  • truncate by path before opening for append: await truncate(part, offset) / ftruncateSync, or
  • when offset === 0, open with "w" instead of "a".

Workaround I use

The session database is a plain mtcute SQLite file, so a small Node client opens a copy of it and calls downloadToFile (Node's writer opens with "w", no append handle): all 153 files of the channel download fine, sizes match message_attachments.file_size byte for byte. Only the CLI's own writer is affected.

Activity

  1. added 2 commits that reference this issue on Nov 21, 2026
    daeb0ed
    24d1671
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions