{"record":{"id":"4b9dce27eedf56bb","repo":"sinelaw/fresh","slug":"server-closed-the-connection-before-answering","errorCode":null,"errorMessage":"server closed the connection before answering","messagePattern":"server closed the connection before answering","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/fresh-editor/src/main.rs","lineNumber":3691,"sourceCode":"    }\n    Ok(CmdConnection { conn, reader })\n}\n\n/// Read control replies until a `ScriptResult` arrives (or an error/EOF/timeout).\n/// Returns `(ok, error, output)`.\nfn read_script_result(\n    conn: &mut CmdConnection,\n    timeout: std::time::Duration,\n) -> AnyhowResult<(bool, Option<String>, Option<String>)> {\n    use fresh::server::protocol::ServerControl;\n    loop {\n        match conn.recv_within(timeout)? {\n            Some(ServerControl::ScriptResult { ok, error, output }) => {\n                return Ok((ok, error, output))\n            }\n            Some(ServerControl::Error { message }) => anyhow::bail!(\"server error: {}\", message),\n            Some(_) => continue,\n            None => anyhow::bail!(\"server closed the connection before answering\"),\n        }\n    }\n}\n\n/// Dispatch a `--cmd cmd ...` / `--cmd split ...` / `--cmd workspace ...` /\n/// `--cmd agent ...` invocation against a running editor. `tokens` is the full\n/// `--cmd` vector (leading verb included).\nfn run_cmd_command(tokens: &[&str]) -> AnyhowResult<()> {\n    let (session, rest) = extract_session_flag(tokens);\n    let session = session.as_deref();\n\n    match rest.first().copied() {\n        Some(\"script\") => {\n            match &rest[1..] {\n                [\"run\", from @ ..] => script_run(session, from),\n                [\"check\", from @ ..] => script_check(from),\n                [\"api\", query, flags @ ..] => script_api(query, flags),\n                [\"api\"] => {","sourceCodeStart":3673,"sourceCodeEnd":3709,"githubUrl":"https://github.com/sinelaw/fresh/blob/67894ca5463dbd7a89bb31add4627c27d6b79d83/crates/fresh-editor/src/main.rs#L3673-L3709","documentation":"Thrown when the control connection yielded no frames within the timeout (recv_within returned None), i.e. the server closed the connection before delivering a ScriptResult or Error. It indicates the editor dropped the client mid-request rather than answering it.","triggerScenarios":"Editor process exiting/crashing while a --cmd/script request is in flight; the command channel being shut down; an editor-side panic tearing down the connection before a reply is sent.","commonSituations":"Editor quitting concurrently with the command; long-running script triggering a crash; editor killed by OOM or user close during the request; stale socket briefly connecting before teardown completes.","solutions":["Check whether the editor is still running; restart it if it exited.","Retry the command once the editor is confirmed alive.","Look at the editor's logs/stderr for a panic around the time of the request.","Reduce script complexity or split long operations to avoid editor-side crashes."],"exampleFix":"// before\nfresh --cmd quit &  fresh --cmd split right   # quit tears down conn mid-request\n// after\nfresh --cmd split right; fresh --cmd quit","handlingStrategy":"retry","validationCode":"// ensure the editor is reachable before each submission\nconst { execSync } = require('child_process');\nfunction alive(session) {\n  try { execSync(`pgrep -f 'fresh.*${session}'`, { stdio: 'ignore' }); return true; }\n  catch { return false; }\n}\nif (!alive(process.env.FRESH_SESSION)) { console.error('editor not running; refusing to send'); process.exit(1); }","typeGuard":null,"tryCatchPattern":"// bash\nfor i in 1 2 3; do\n  fresh --cmd split right && break\n  rc=$?\n  if [ $rc -ne 0 ]; then sleep 1; pgrep -f 'fresh' >/dev/null || { echo 'editor died'; exit 1; }; fi\ndone","preventionTips":["Don't send commands concurrently with quit/close operations.","Retry idempotent commands once on connection drops.","Watch editor logs for panics that tear down the command channel.","Keep scripts short; long scripts increase the window for editor exit mid-request."],"tags":["cli","ipc","connection","rpc"],"backgroundTag":"broken-pipe","analyzedSha":"67894ca5463dbd7a89bb31add4627c27d6b79d83","analyzedAt":"2026-09-13T15:04:03.701Z","contentChangedAt":"2026-09-13T15:04:03.701Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}