{"id":"981aa1244a9ec45a","repo":"redis/redis-py","slug":"timeout-writing-to-socket-981aa1","errorCode":null,"errorMessage":"Timeout writing to socket","messagePattern":"Timeout writing to socket","errorType":"exception","errorClass":"TimeoutError","httpStatus":null,"severity":"error","filePath":"redis/connection.py","lineNumber":1352,"sourceCode":"                self._ping_failed,\n                with_failure_count=True,\n            )\n\n    def send_packed_command(self, command, check_health=True):\n        \"\"\"Send an already packed command to the Redis server\"\"\"\n        if not self._sock:\n            self.connect_check_health(check_health=False)\n        # guard against health check recursion\n        if check_health:\n            self.check_health()\n        try:\n            if isinstance(command, str):\n                command = [command]\n            for item in command:\n                self._sock.sendall(item)\n        except socket.timeout:\n            self.disconnect()\n            raise TimeoutError(\"Timeout writing to socket\")\n        except OSError as e:\n            self.disconnect()\n            if len(e.args) == 1:\n                errno, errmsg = \"UNKNOWN\", e.args[0]\n            else:\n                errno = e.args[0]\n                errmsg = e.args[1]\n            raise ConnectionError(f\"Error {errno} while writing to socket. {errmsg}.\")\n        except BaseException:\n            # BaseExceptions can be raised when a socket send operation is not\n            # finished, e.g. due to a timeout.  Ideally, a caller could then re-try\n            # to send un-sent data. However, the send_packed_command() API\n            # does not support it so there is no point in keeping the connection open.\n            self.disconnect()\n            raise\n\n    def send_command(self, *args, **kwargs):\n        \"\"\"Pack and send a command to the Redis server\"\"\"","sourceCodeStart":1334,"sourceCodeEnd":1370,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/connection.py#L1334-L1370","documentation":"Raised as a TimeoutError in send_packed_command when socket.sendall raises socket.timeout while writing a command to the wire. The connection is disconnected first, then the error propagates. It means the socket's timeout elapsed before the full command could be transmitted.","triggerScenarios":"Sending a very large command/value over a slow or saturated network link where sendall cannot complete within socket_timeout; a stalled server not draining its read buffer; a congested connection. Fires for every command path that writes to the socket.","commonSituations":"Bulk-loading large payloads (SET of a huge blob, big pipeline/LUA script) on a tight socket_timeout; network contention; server CPU saturated so TCP buffers back up; cross-region links with high latency.","solutions":["Increase socket_timeout (and socket_connect_timeout) to accommodate large writes.","Batch large values into smaller chunks or use pipelines judiciously.","Upgrade network bandwidth / move the client closer to the server.","Configure retry_on_timeout=True or retry_on_error=[TimeoutError] so transient timeouts are retried."],"exampleFix":"// before\nr = redis.Redis(host=h, socket_timeout=0.5)\nr.set('k', huge_blob)\n// after\nr = redis.Redis(host=h, socket_timeout=5.0)\nr.set('k', huge_blob)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from redis.exceptions import TimeoutError\ntry:\n    r.set('k', huge_blob)\nexcept TimeoutError as e:\n    if 'writing to socket' in str(e):\n        # increase socket_timeout then retry idempotent writes\n        pass","preventionTips":["Size socket_timeout to your largest expected payload.","Chunk very large writes; configure retry_on_timeout=True for idempotent commands.","Monitor network saturation on bulk-load paths."],"tags":["network","timeout","write","transient"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}