Skip to content
This repository was archived by the owner on Apr 5, 2026. It is now read-only.
This repository was archived by the owner on Apr 5, 2026. It is now read-only.

Direct mode: incomplete media loading — diagnostics and improvements #69

@dvershinin

Description

@dvershinin

Problem

Users report that in --direct mode, some media in channels fails to fully load, while normal ME relay mode works fine (reported in #64).

Analysis

The direct relay code (tcp_direct_relay()) was audited — no data loss or corruption bugs found. The relay does a clean zero-copy move from input buffer to output queue. DC table covers all 5 production DCs, media DCs (negative IDs) are correctly handled, and TCP keepalive is enabled on DC sockets.

The root cause is inherent to direct mode's architecture vs ME relays:

Aspect ME Relay Mode Direct Mode
Network path to DCs ME relays are close to DCs, optimized links Proxy server connects from wherever it is
Connection failure ME may retry/reroute internally Both sides closed immediately
DC addressing ME knows all DC IPs dynamically 1 hardcoded IPv4 per DC, no IPv6
Flow control ME handles buffering/backpressure No backpressure mechanism

Most likely causes

  1. Network quality: proxy server's path to Telegram DC IPs may have higher latency/loss — media downloads (large, long-lived) are more affected than chat messages
  2. No retry: DC connection drop = immediate close of both sides, no recovery
  3. Single IP per DC: one hardcoded IPv4 address, no alternatives
  4. No IPv6: some networks have better IPv6 paths to Telegram
  5. DPI/ISP interference: direct obfuscated2 connections (no TLS) to well-known Telegram IPs may be throttled

What's been done

Added diagnostic logging and stats counters (commit 1eb2fa8):

  • Per-connection close logging: DC number, duration, which side (client vs DC) initiated close
  • direct_dc_connections_failed — connections that failed to establish (unknown DC, socket error)
  • direct_dc_connections_dc_closed — connections closed by the DC side (not the client)
  • Exposed in plain text stats (:8888), Prometheus metrics, and verbose logs (-v 1)

Planned improvements

  • Add IPv6 DC addresses with fallback
  • Multiple IPs per DC for redundancy
  • Connection retry with backoff on DC connection failure
  • Configurable DC address table (runtime override)

Information needed

To diagnose specific cases, we need:

  • Server location (country/provider)
  • Verbose logs (-v 2) captured during media loading failure
  • Which type of media fails (images, videos, large files, specific channels?)

Related: #64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions