{"record":{"id":"d08ee4286065b012","repo":"phpseclib/phpseclib","slug":"connection-closed-attempting-to-forward-data-to-ssh-agent","errorCode":null,"errorMessage":"Connection closed attempting to forward data to SSH agent","messagePattern":"Connection closed attempting to forward data to SSH agent","errorType":"exception","errorClass":"ConnectionClosedException","httpStatus":null,"severity":"error","filePath":"phpseclib/System/SSH/Agent.php","lineNumber":271,"sourceCode":"     * Forward data to SSH Agent and return data reply\n     */\n    public function forwardData(string $data): ?string\n    {\n        if ($this->expected_bytes > 0) {\n            $this->socket_buffer .= $data;\n            $this->expected_bytes -= strlen($data);\n        } else {\n            $agent_data_bytes = current(unpack('N', $data));\n            $current_data_bytes = strlen($data);\n            $this->socket_buffer = $data;\n            if ($current_data_bytes != $agent_data_bytes + 4) {\n                $this->expected_bytes = ($agent_data_bytes + 4) - $current_data_bytes;\n                return null;\n            }\n        }\n\n        if (strlen($this->socket_buffer) != fwrite($this->fsock, $this->socket_buffer)) {\n            throw new ConnectionClosedException('Connection closed attempting to forward data to SSH agent');\n        }\n\n        $this->socket_buffer = '';\n        $this->expected_bytes = 0;\n\n        $agent_reply_bytes = current(unpack('N', $this->readBytes(4)));\n\n        $agent_reply_data = $this->readBytes($agent_reply_bytes);\n        $agent_reply_data = current(unpack('a*', $agent_reply_data));\n\n        return pack('Na*', $agent_reply_bytes, $agent_reply_data);\n    }\n}\n","sourceCodeStart":253,"sourceCodeEnd":285,"githubUrl":"https://github.com/phpseclib/phpseclib/blob/1da055d9189071dcad03d25a47b908449b8a5c78/phpseclib/System/SSH/Agent.php#L253-L285","documentation":"forwardData() relays agent protocol frames received over an SSH channel to the local ssh-agent socket. If fwrite fails to write the complete buffered frame to the agent socket, ConnectionClosedException is thrown indicating the agent connection dropped mid-forwarding.","triggerScenarios":"An SSH client on the remote side is using agent forwarding and sends a signing request; the local agent socket ($this->fsock) is closed or errors while forwardData writes the accumulated socket_buffer to it.","commonSituations":"ssh-agent exited or restarted during an active forwarded session; timeout killed the local unix socket connection; too many concurrent forwarded requests exhausting the agent.","solutions":["Confirm ssh-agent is still running and responsive (ssh-add -l from the same environment).","Catch ConnectionClosedException around SSH operations that trigger forwarding and recreate the Agent to re-open the socket.","Restart the SSH session/forwarding channel after the agent connection is re-established.","Check for local firewalls/AV or ulimits killing long-lived unix sockets.","Reduce idle time between forwarded requests to avoid stale sockets."],"exampleFix":"// before\n$data = $agent->forwardData($channelData); // throws if agent socket died\n// after\ntry {\n    $data = $agent->forwardData($channelData);\n} catch (\\phpseclib4\\Exception\\ConnectionClosedException $e) {\n    $agent = new \\phpseclib4\\System\\SSH\\Agent(); // reconnect to agent\n    $data = null; // drop this frame; client can retry\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    $reply = $agent->forwardData($data);\n} catch (\\phpseclib4\\Exception\\ConnectionClosedException $e) {\n    $agent = new \\phpseclib4\\System\\SSH\\Agent(); // reopen agent socket\n    // signal the forwarding channel to retry the request\n}","preventionTips":["Keep ssh-agent running and supervise it (systemd/Windows service auto-restart).","Avoid long idle forwarding sessions that let sockets go stale.","Catch ConnectionClosedException at the forwarding boundary and reconnect.","Watch ulimits/AV software that may kill unix socket connections."],"tags":["ssh-agent","agent-forwarding","connection-closed","fwrite"],"backgroundTag":"broken-pipe","analyzedSha":"1da055d9189071dcad03d25a47b908449b8a5c78","analyzedAt":"2026-09-18T03:54:50.436Z","contentChangedAt":"2026-09-18T03:54:50.436Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}