{"record":{"id":"48aa4654f995c050","repo":"spacedriveapp/spacedrive","slug":"failed-to-send-length","errorCode":null,"errorMessage":"Failed to send length: {}","messagePattern":"Failed to send length: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"core/src/service/network/protocol/sync/transport.rs","lineNumber":169,"sourceCode":"\t\t\t\t\"Failed to connect to device for sync request\"\n\t\t\t);\n\t\t\tanyhow::anyhow!(\"Failed to connect to {}: {}\", target_device, e)\n\t\t})?;\n\n\t\t// Open bidirectional stream\n\t\tlet (mut send, mut recv) = conn\n\t\t\t.open_bi()\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to open bidirectional stream: {}\", e))?;\n\n\t\t// Serialize and send request\n\t\tlet req_bytes = serde_json::to_vec(&request)\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to serialize sync request: {}\", e))?;\n\n\t\tlet len = req_bytes.len() as u32;\n\t\tsend.write_all(&len.to_be_bytes())\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to send length: {}\", e))?;\n\t\tsend.write_all(&req_bytes)\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to send request: {}\", e))?;\n\n\t\t// Properly close send stream\n\t\tsend.finish()\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to finish stream: {}\", e))?;\n\n\t\tdebug!(\"Sync request sent, waiting for response...\");\n\n\t\t// Read response with timeout\n\t\tlet result = timeout(Duration::from_secs(60), async {\n\t\t\tlet mut len_buf = [0u8; 4];\n\t\t\trecv.read_exact(&mut len_buf).await.map_err(|e| {\n\t\t\t\tanyhow::anyhow!(\"Failed to read response length: {}\", e)\n\t\t\t})?;\n\t\t\tlet resp_len = u32::from_be_bytes(len_buf) as usize;\n","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/spacedriveapp/spacedrive/blob/6dfeccf2113039e35f2ce735f945e70dc3e4ea45/core/src/service/network/protocol/sync/transport.rs#L151-L187","documentation":"Writing the 4-byte big-endian length prefix to the bidirectional stream's SendStream failed in send_sync_request. With iroh/quinn, write errors reflect connection state - the connection was lost or the peer reset the stream - not local I/O.","triggerScenarios":"Peer dropped right after connect; connection lost mid-handshake-completion; peer reset the stream because its handler rejected it; mobile peer backgrounded and killed by the OS.","commonSituations":"Flaky links (wifi or VPN flap); peer entering standby mid-request; sync started exactly as the peer app quits.","solutions":["Retry the whole send on a new connection with backoff","Check connection liveness (close_reason) before reusing it","If persistent, capture the peer's stream reset reason in its logs"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"Err(e) if e.to_string().contains(\"Failed to send length\") => {\n    // connection was lost mid-send: rebuild connection and retry the request once\n    retry_request_on_fresh_connection(device, request).await\n}","preventionTips":["Check conn.close_reason() before reusing connections","Retry sends once on a fresh connection; stream write errors are almost always connection loss","Keep payloads small so the write window stays short"],"tags":["network","iroh","quic","streams","sync","rust"],"backgroundTag":null,"analyzedSha":"6dfeccf2113039e35f2ce735f945e70dc3e4ea45","analyzedAt":"2026-08-16T11:26:17.074Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}