{"record":{"id":"70987d40b0125c37","repo":"php-amqplib/php-amqplib","slug":"code","errorCode":"$code","errorMessage":"socket_strerror($code)","messagePattern":"socket_strerror\\(\\$code\\)","errorType":"exception","errorClass":"AMQPConnectionClosedException","httpStatus":null,"severity":"critical","filePath":"PhpAmqpLib/Wire/IO/SocketIO.php","lineNumber":214,"sourceCode":"                        $buffer = mb_substr($data, $written, self::BUFFER_SIZE, 'ASCII');\n                    }\n                    $result = socket_write($this->sock, $buffer);\n                }\n                $this->throwOnError();\n            } catch (\\ErrorException $e) {\n                $code = socket_last_error($this->sock);\n                $constants = SocketConstants::getInstance();\n                switch ($code) {\n                    case $constants->SOCKET_EPIPE:\n                    case $constants->SOCKET_ENETDOWN:\n                    case $constants->SOCKET_ENETUNREACH:\n                    case $constants->SOCKET_ENETRESET:\n                    case $constants->SOCKET_ECONNABORTED:\n                    case $constants->SOCKET_ECONNRESET:\n                    case $constants->SOCKET_ECONNREFUSED:\n                    case $constants->SOCKET_ETIMEDOUT:\n                        $this->close();\n                        throw new AMQPConnectionClosedException(socket_strerror($code), $code, $e);\n                    default:\n                        throw new AMQPIOException(sprintf(\n                            'Error sending data. Last SocketError: %s',\n                            socket_strerror($code)\n                        ), $code, $e);\n                }\n            } finally {\n                $this->restoreErrorHandler();\n            }\n\n            if ($result === false) {\n                throw new AMQPIOException(sprintf(\n                    'Error sending data. Last SocketError: %s',\n                    socket_strerror(socket_last_error($this->sock))\n                ));\n            }\n\n            $now = microtime(true);","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/php-amqplib/php-amqplib/blob/381b6f7c600e0e0c7463cdd7f7a1a3bc6268e5fd/PhpAmqpLib/Wire/IO/SocketIO.php#L196-L232","documentation":"In SocketIO::write(), when socket_write() raises an ErrorException, the code maps the errno: connection-fatal codes (EPIPE, ENETDOWN, ENETUNREACH, ENETRESET, ECONNABORTED, ECONNRESET, ECONNREFUSED, ETIMEDOUT) close the socket and throw AMQPConnectionClosedException whose message is socket_strerror($code) — the raw OS error text (e.g. 'Broken pipe'). This is the transport-level signal that the TCP connection is gone.","triggerScenarios":"Publishing or sending any frame when the peer has reset or closed the TCP connection: broker restart, ECONNREFUSED after broker shutdown, broken pipe after broker closed the socket, ETIMEDOUT on a dead network path.","commonSituations":"Long-running RabbitMQ consumer losing the broker (restart, failover, HA promotion); network partition or VPN drop; broker closing idle connections because heartbeats are disabled or too large; Docker/k8s restarting the RabbitMQ container under a producer.","solutions":["Catch AMQPConnectionClosedException and reconnect ($connection->reconnect(), re-open channels) before retrying the publish.","Enable client heartbeats and keepalive so dead TCP connections are detected and re-established proactively.","Check the broker is reachable and its advertised host/port/firewall rules allow sustained connections.","Use publisher confirms / message persistence so messages lost during connection drops can be resent."],"exampleFix":"// before\n$channel->basic_publish($msg, 'ex');\n\n// after\ntry {\n    $channel->basic_publish($msg, 'ex');\n} catch (PhpAmqpLib\\Exception\\AMQPConnectionClosedException $e) {\n    $connection->reconnect();\n    $channel = $connection->channel();\n    $channel->basic_publish($msg, 'ex');\n}","handlingStrategy":"retry","validationCode":"if (!$connection->isConnected()) { $connection->reconnect(); $channel = $connection->channel(); }","typeGuard":null,"tryCatchPattern":"try {\n    $channel->basic_publish($msg, 'ex');\n} catch (\\PhpAmqpLib\\Exception\\AMQPConnectionClosedException $e) {\n    $connection->reconnect();\n    $channel = $connection->channel();\n    $channel->basic_publish($msg, 'ex');\n}","preventionTips":["Set client heartbeat (e.g. 30s) and TCP keepalive","Use publisher confirms for at-least-once delivery","Monitor broker availability and restarts","Avoid holding connections open across deployments without health checks"],"tags":["network","socket","broken-pipe","connection-reset","amqp"],"backgroundTag":"broken-pipe","analyzedSha":"381b6f7c600e0e0c7463cdd7f7a1a3bc6268e5fd","analyzedAt":"2026-09-20T11:37:12.097Z","contentChangedAt":"2026-09-20T11:37:12.097Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}