Updates and recovery
Client.connect() restores the update cursor. Signed-in sessions with a saved PTS fetch updates received while offline. Sessions without a cursor first fetch Telegram’s current state. The manager commits cursor state before it exposes an update to iter_updates() or a handler. A crash after that commit but before handler delivery can leave the cursor ahead of application work, and miniproto may not deliver that update again. Idempotency helps when an update repeats, but it cannot recover missed work. Applications that cannot accept missed effects need a separate reconciliation strategy. Miniproto does not promise exactly-once or at-least-once delivery to application code.
from miniproto import Client, ClientConfig, Update
async def consume(config: ClientConfig) -> None: async with Client(config) as client: async for update in client.iter_updates(): await process_update(update)
async def process_update(update: Update) -> None: # Use application state to deduplicate repeats and reconcile missed work. ...The stream is FIFO for the normalized updates it emits. An iterator becomes active on its first anext() and stays active while the consumer processes each yielded event. Close a retained iterator with await iterator.aclose() or contextlib.aclosing() when stopping between events. Multiple iterators compete for the same queue.
The stream is deliberately open-ended: stopping the update manager does not close the public queue or insert a sentinel. A consumer blocked in iter_updates() remains blocked until it is cancelled or a later event arrives. Arrange shutdown so the task consuming the iterator is cancelled, then let the client context manager disconnect.
Queue pressure is a policy choice
Section titled “Queue pressure is a policy choice”ClientConfig.update_queue_size defaults to 1000. Both the raw intake queue and the public normalized-update queue use that bound. ClientConfig.update_queue_overflow defaults to "drop_oldest".
"raise"raisesasyncio.QueueFullwhen a full queue receives another item. Treat it as backpressure or a capacity incident rather than silently losing state."drop_newest"discards the incoming item."drop_oldest"removes the oldest queued item before accepting the incoming item. A client that never starts an iterator retains its most recent updates."wait"waits for capacity. Use this when an active iterator or callback regularly drains the queue. An idle iterator can keep the producer waiting until shutdown.
When handlers are registered and no iterator is active, updates go directly to handlers without filling an unused iterator queue. A later iterator does not receive that callback-only history. With no registered handlers, the bounded pre-iteration backlog remains available. When callbacks and an active iterator share delivery, queue admission still precedes callbacks: either dropping policy also skips handlers for the rejected event. Raising a queue size or choosing a loss policy does not create delivery guarantees, it changes which overload failure your application must handle.
Measure handler latency and queue pressure before increasing the bound. Handlers registered with client.on(...) are awaited sequentially in registration-mapping order for matching types. One slow handler delays later matching handlers and the next public delivery work. Keep handler work short, hand durable jobs to an application-owned worker and retain enough application state to make a recovered update safe to replay.
Gaps and recovery
Section titled “Gaps and recovery”The manager detects gaps in global PTS, QTS or update-container sequence state and uses updates.getDifference to restore global cursor state. It uses updates.getChannelDifference for a channel PTS gap when it has that channel’s cached access hash. Recovery follows Telegram’s intermediate responses while the cursor advances. A first message from an unseen channel seeds its independent PTS. RPC replies carrying update counters advance their corresponding common or channel cursor. Events in a difference response are processed by date; this does not order independent network inputs or application workers.
By default, a background handler error is logged and the remaining handlers and subsequent updates continue. Background RPC or persistence failures are logged and the last committed cursor remains available for recovery. Applications that select "raise" get fail-fast processing. In that mode await client.wait_for_updates(), later raw intake and disconnect surface a failed drainer. Cancelling the waiter leaves the drainer running. The observability guide describes the logging hooks.
Practical checklist
Section titled “Practical checklist”- Make externally visible effects idempotent or deduplicate them with application-owned durable state.
- Choose
"raise"when loss must surface and catchasyncio.QueueFullat the ownership boundary. - Choose a dropping policy only when the discarded update class is acceptable for the application.
- Cancel the iterator task during shutdown. Waiting for an end-of-stream signal will wait indefinitely.
- Run recovery tests through a fake or controlled invoker before enabling a live account. Credentialed integration runs are opt-in. See development notes.