{"id":"efcf397892e70792","repo":"redis/redis-py","slug":"timed-out-closing-connection-after-self-socket-co","errorCode":null,"errorMessage":"Timed out closing connection after {self.socket_connect_timeout}","messagePattern":"Timed out closing connection after (.+?)","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"warning","filePath":"redis/asyncio/connection.py","lineNumber":1106,"sourceCode":"            async with async_timeout(self.socket_connect_timeout):\n                self._parser.on_disconnect()\n                # Reset the reconnect flag\n                self.reset_should_reconnect()\n                if not self.is_connected:\n                    return\n                try:\n                    self._writer.close()  # type: ignore[union-attr]\n                    # wait for close to finish, except when handling errors and\n                    # forcefully disconnecting.\n                    if not nowait:\n                        await self._writer.wait_closed()  # type: ignore[union-attr]\n                except OSError:\n                    pass\n                finally:\n                    self._reader = None\n                    self._writer = None\n        except asyncio.TimeoutError:\n            raise TimeoutError(\n                f\"Timed out closing connection after {self.socket_connect_timeout}\"\n            ) from None\n\n        if error:\n            if health_check_failed:\n                close_reason = CloseReason.HEALTHCHECK_FAILED\n            else:\n                close_reason = CloseReason.ERROR\n\n            if failure_count is not None and failure_count > self.retry.get_retries():\n                await record_error_count(\n                    server_address=getattr(self, \"host\", None),\n                    server_port=getattr(self, \"port\", None),\n                    network_peer_address=getattr(self, \"host\", None),\n                    network_peer_port=getattr(self, \"port\", None),\n                    error_type=error,\n                    retry_attempts=failure_count,\n                )","sourceCodeStart":1088,"sourceCodeEnd":1124,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/asyncio/connection.py#L1088-L1124","documentation":"Raised as a TimeoutError from disconnect() when the wrapped async_timeout(socket_connect_timeout) context expires while trying to cleanly close the writer (wait_closed()). The teardown of a dead/slow socket took longer than the configured connect timeout (which is deliberately reused here as the close deadline), so the library aborts the graceful shutdown. The connection is still torn down locally; the timeout is reported so the caller knows the peer did not respond to FIN.","triggerScenarios":"Calling disconnect() (explicitly, or via pool.aclose / client.aclose / connection teardown) when self._writer.wait_closed() blocks beyond socket_connect_timeout. Common during shutdown after a network partition or when the remote is unresponsive but the TCP socket is half-open.","commonSituations":"Network partitions; kernel-level half-open sockets after a server crash; aggressive socket_connect_timeout values; shutting down a client under heavy load where the event loop is starved; containers/VMs where the network namespace is being torn down.","solutions":["Call disconnect(nowait=True) (or aclose() with the equivalent) to skip wait_closed() during shutdown.","Raise socket_connect_timeout to give teardown more room.","Investigate the network path (firewall idle timeouts, NAT, half-open sockets) between client and server.","Ensure the event loop is not being blocked by other long-running coroutines during shutdown."],"exampleFix":"// before\nawait conn.disconnect()\n\n// after\nawait conn.disconnect(nowait=True)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from redis.exceptions import TimeoutError\ntry:\n    await client.aclose()\nexcept TimeoutError:\n    await client.connection_pool.disconnect(inuse_connections=False)  # best-effort nowait","preventionTips":["Use nowait=True during shutdown.","Keep socket_connect_timeout reasonable for your network RTT.","Don't block the event loop during teardown."],"tags":["connection","disconnect","timeout","network","async"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}