Transport, reliability and replay
Framed TCP transport
Section titled “Framed TCP transport”The configured transport selects TCP abridged, intermediate or padded-intermediate framing. The transport enforces positive connect/read/write deadlines and a maximum decoded payload size; it creates a fresh frame codec when reconnecting so partial bytes from the old stream cannot cross into the new connection.
TransportConfig.max_payload_size defaults to 16 MiB. The sender also uses it as the decompressed-output limit for each gzip wrapper in message, container and RPC-result bodies. One decode path may contain up to 16 nested wrappers. A malformed frame, malformed gzip stream or gzip payload over the configured limit raises ProtocolValidationError.
One task must own recv() and its underlying stream reader. asyncio.StreamReader rejects concurrent reads, so calling recv, read_event or read_packet concurrently on the same transport is not supported. The sender is the normal single receive owner and allows concurrent callers to wait on independently resolved RPC futures while it serializes message-ID assignment and transport writes.
Retry after transport loss
Section titled “Retry after transport loss”When a transport drops, the sender reconnects under its connection lock and resends in-flight requests only when miniproto classifies them as retry-safe. The default list covers selected read methods and the messages.sendMessage and messages.sendMedia writes, which use Telegram random_id de-duplication. messages.getBotCallbackAnswer presses a bot button, so miniproto excludes it even though its name starts with get. Client.invoke(retry=...) can override the classification; set it only when you understand how Telegram handles a repeated request.
For msgs_resend_req, the sender wraps the original request in a new container and preserves its original message ID and sequence number. A valid bad_server_salt or clock-correction bad_msg_notification updates the sender state before retrying. Sequence-number errors 32 and 33 reset the MTProto session. Repeated incoming messages are acknowledged without being delivered again. A duplicate child in a fresh container does not suppress fresh siblings.
If an unsafe request may have reached Telegram before the transport failed, the sender reports AmbiguousRpcResult instead of silently replaying it. An ambiguity is a product decision point: inspect the application’s own idempotency key, query state where appropriate and avoid assuming that a retry is harmless.
Quick acknowledgement is an early receipt
Section titled “Quick acknowledgement is an early receipt”Pass quick_ack=True or a callback to request Telegram’s transport acknowledgement. The sender derives its receipt token while encoding the padded plaintext. Await the RPC separately for its result or error.
Failure boundaries
Section titled “Failure boundaries”Malformed framing and failed cryptographic validation close the transport. The read watchdog measures incoming progress, so outgoing writes alone cannot keep an unresponsive connection open. Read/write/connect failures, reconnect limits, pings, acknowledgement flushing, pending-RPC capacity and caller cancellation follow their own rules.
The default transport mode is tcp_abridged. TransportConfig sets the reconnect backoff. Measure a comparable workload before tuning it, and preserve the retry rules above.