The search for "pyrogram uvloop unable to perform operation on TCPTransport closed=True the handler is closed" returned the following summary: This error—`RuntimeError: unable to perform operation on ; the handler is closed`—occurs when Pyrogram (or any library using `asyncio`) tries to send data over a network socket (TCP transport)[1][2] that has **already been closed or disconnected** by Telegram's servers or due to a network interruption[3]. When combined with **`uvloop`**, this error is surfaced more strictly than default Python `asyncio`. While standard asyncio might sometimes swallow or handle certain transport closure states gracefully, `uvloop` immediately raises a `RuntimeError` if an operation is attempted on a dead file descriptor/transport handle[4]. ### Why this happens in Pyrogram: 1. **Network Instability / Timeouts:** A sudden drop in internet connection, a Telegram server-side reset, or a prolonged timeout causes the underlying socket to close. 2. **ConcurrentNode/Session Exhaustion:** If your application is handling too many concurrent requests[4], high traffic, or multiple client group tasks[1] without proper rate-limiting, connections can desync or drop abruptly. 3. **Improper Event Loop Initialization:** If `uvloop` is installed incorrectly or conflicts with how Python handles loops (especially common in newer Python versions or when using multi-threading/multiprocessing), transport handling breaks down. --- ### How to Fix It #### 1. Fallback to standard `asyncio` (Quickest Fix) If you don't strictly need the performance boost of `uvloop`, or if you want to see if it eliminates the crash entirely, remove or comment out the `uvloop` initialization code at the very top of your entry script: ```python # REMOVE OR COMMENT OUT THESE LINES: # import uvloop # uvloop.install() ``` Instead, let Pyrogram/Python rely on the default built-in `asyncio` event loop[4], which is generally more forgiving of sudden transport closures. #### 2. Implement Reconnection and Error Handling If you want to keep using `uvloop`, you must gracefully handle network drops inside your script. Pyrogram usually handles reconnections automatically, but if you are wrapping calls inside custom loops or workers, unhandled exceptions can crash the script. Catch the specific `RuntimeError` or generic ` OSError` / `RPCError` and allow your script to recover[1]: ```python import asyncio import logging from pyrogram.errors import FloodWait, NetworkError async def safe_telegram_call(client, function_to_call, *args, **kwargs): while True: try: return await function_to_call(*args, **kwargs) except (RuntimeError, OSError) as e: if "the handler is closed" in str(e): logging.warning("TCP transport closed unexpectedly. Reconnecting...") await asyncio.sleep(3) # Ensure client is connected if not client.is_connected: await client.connect() else: raise e except FloodWait as e: logging.warning(f"FloodWait encountered. Sleeping for {e.value} seconds.") await asyncio.sleep(e.value) except NetworkError: logging.warning("Network error encountered. Retrying in 5 seconds...") await asyncio.sleep(5) ``` #### 3. Update `uvloop` and `pyrogram` Old versions of `uvloop` have known edge-case bugs regarding transport tracking. Ensure you have the latest stable releases installed: ```bash pip install --upgrade uvloop pyrogram tgcrypto ``` #### 4. Check for Concurrency Bottlenecks If your application scales up tasks (e.g., handling thousands of incoming updates or making rapid-fire API requests)[4][1], wrap your requests with semaphores (`asyncio.Semaphore`) to limit concurrent execution. Flooding Telegram's servers with too many simultaneous requests on a single session can trigger forceful socket closures from their end. Sources: [1] [stackoverflow.com](https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGHEeWXBVi6g3IeB3HlcdikpzpnJmpRk26h8E6DGRPKrFFaw5CLyVOjzjVqZonia9F853lJqQVBequ1M0LW7bVM6S8Q7pmadFBck2F3o0NWUl2d2y2AYbW3WBotSZBsSXgeVbhCxMH3mAtJT3oagPbT9mrL9EOKnubVP4iiG1qJE---jrHb-7EZXcmfGCVr6aU4) [2] [github.com](https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGCUW1FhJNbW85ewsVfpAPxqjmYUH8oK5KPyN0q7rQg1fVNZJQZVnUhZ75s-W0YEIUN-PDU8Yfism8qeugiafw0GsqCq32nX4o7MItfKHh_bebBbSN4pBSqkpsdyUd-n4yVzW7So_JYBmT-UocUGjE=) [3] [telli.sh](https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQFAvpY383E9OXoEhE5_2OyySFHUCKtiAxAPcGyejE_SEe7dZTEIxCve9G9AvJmBLEO91zqttewE3u1ycBwgGbRXD8-htvaM7Zzfx6Ep9u2i7BNxpBwZeb_eENLxEeJPhyptpt86qTBYS8GDp0N8bVVBW6O_) [4] [github.com](https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQHJzBsKq4wlK7JloDK3Dk0I0rM124C5H4izH8KbeWbeMQ5RIKZ-Ml8HUwcyYVCRn1NL3atxyQ1eZjtfEqPQyiBre_kI-lsSg87wetDOXVwplbUzUKjIWfrElM3VRUTFOCbnR-ow7w==) Note: Source URLs are HTTP redirects; check where they resolve if you need the direct link.