Skip to content

fix(mtproto): route reused auth key seen during key exchange#151

Merged
ernado merged 3 commits into
mainfrom
fix/mtproto-exchange-reused-key
Jun 14, 2026
Merged

fix(mtproto): route reused auth key seen during key exchange#151
ernado merged 3 commits into
mainfrom
fix/mtproto-exchange-reused-key

Conversation

@ernado

@ernado ernado commented Jun 14, 2026

Copy link
Copy Markdown
Member

Problem

When a client reuses an already-established auth key on a connection the server still treats as performing key exchange, the exchange read loop replied with a bare -404 (auth_key_not_found) to that encrypted frame without looking the key up. Telegram Desktop interprets a -404 on a temporary key as "key destroyed", discards it and re-runs key exchange — a tight reconnect/key-exchange storm (hundreds of Client connected / Key exchange failed: EOF per second; client logging 185× -404 ... temporary key ... destroyed).

Fix

Bumps gotd/td to v0.156.3 (gotd/td#1777), which surfaces such a frame as exchange.UnexpectedEncryptedError (carrying the raw frame) instead of failing the exchange.

serveConn now:

  • resolves the key via the registry and dispatches the frame as a normal encrypted RPC when known;
  • replies -404 with a log line only when the key is genuinely unknown (so the client correctly re-exchanges).

The blind, unlogged -404 path (exchangeConn) is removed.

Verification

Rebuilt and restarted the live service against the running Telegram Desktop client:

  • Before: storm — hundreds of connect/exchange/disconnect per second; 185 client -404 ... destroyed.
  • After: 0 -404 sent, 0 key-exchange failures, stable connections, normal sync resumed — sustained across multiple client retry intervals.

ernado and others added 3 commits June 14, 2026 14:43
When the server cannot resolve an incoming auth key it silently sends a
-404 (CodeAuthKeyNotFound), which makes clients discard the key and
reconnect. Log the key id so these can be diagnosed (e.g. media/upload
session reconnect storms).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Surface why a connection's handshake ends (e.g. client EOF mid-exchange)
and any proto-error code sent, to diagnose client reconnect loops such as
media/upload session churn.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Bumps gotd/td to v0.156.3, which surfaces a frame bearing a non-zero
auth_key_id during server key exchange as exchange.UnexpectedEncryptedError
instead of failing the exchange.

The server now resolves that key and dispatches the frame as a normal
encrypted RPC, replying -404 only when the key is genuinely unknown.
Previously the exchange read loop replied -404 to every such frame without
a lookup, making clients like Telegram Desktop discard a still-valid
temporary key and re-run key exchange in a tight reconnect storm.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ernado ernado merged commit 07c45c0 into main Jun 14, 2026
11 checks passed
@ernado ernado deleted the fix/mtproto-exchange-reused-key branch June 14, 2026 12:41
@codecov

codecov Bot commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 4.54545% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.19%. Comparing base (42f4acf) to head (6bdeee2).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
internal/mtproto/conn.go 4.54% 21 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #151      +/-   ##
==========================================
- Coverage   64.39%   64.19%   -0.21%     
==========================================
  Files          69       69              
  Lines        4098     4105       +7     
==========================================
- Hits         2639     2635       -4     
- Misses       1059     1073      +14     
+ Partials      400      397       -3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant