{"record":{"id":"aff81cfdafe11a96","repo":"spacedriveapp/spacedrive","slug":"failed-to-write-length-prefix","errorCode":null,"errorMessage":"Failed to write length prefix: {}","messagePattern":"Failed to write length prefix: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"core/src/service/network/transports/sync.rs","lineNumber":133,"sourceCode":"\t\t\t\t\tnode_id,\n\t\t\t\t\tconn: new_conn.clone(),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tnew_conn\n\t\t};\n\n\t\t// Open a unidirectional stream and send the message\n\t\tlet mut send = conn\n\t\t\t.open_uni()\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to open stream: {}\", e))?;\n\n\t\t// Write length prefix (required by multiplexer)\n\t\tlet len = 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 write length prefix: {}\", e))?;\n\n\t\t// Write message bytes\n\t\tsend.write_all(&bytes).await.map_err(|e| {\n\t\t\twarn!(\n\t\t\t\tdevice_uuid = %target_device,\n\t\t\t\terror = %e,\n\t\t\t\t\"Failed to write sync message to stream\"\n\t\t\t);\n\t\t\tanyhow::anyhow!(\"Failed to write message: {}\", e)\n\t\t})?;\n\n\t\tsend.finish()\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to finish stream: {}\", e))?;\n\n\t\ttracing::info!(\n\t\t\t\"Sync message sent successfully to device {} ({} bytes via uni stream)\",\n\t\t\ttarget_device,\n\t\t\tbytes.len()","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/spacedriveapp/spacedrive/blob/6dfeccf2113039e35f2ce735f945e70dc3e4ea45/core/src/service/network/transports/sync.rs#L115-L151","documentation":"Writing the 4-byte big-endian length prefix required by the remote multiplexer failed on the unidirectional stream. As with all stream writes here, the error reflects connection state - connection lost or the peer reset the stream - rather than local I/O.","triggerScenarios":"Cached or fresh connection dropping right at stream open; peer resetting uni streams it cannot match to a protocol; mobile peer suspended mid-send.","commonSituations":"Flaky links; peer standby; peer restart with the same identity invalidating the cached connection.","solutions":["Retry the send on a new connection, evicting any cached entry first","Check the cached connection's close_reason before reuse","Inspect peer logs for stream reset reasons if persistent"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"Err(e) if e.to_string().contains(\"Failed to write length prefix\") => {\n    // connection lost at stream start: evict cache entry, reconnect, resend once\n    active_connections.write().await.remove(&cache_key);\n    retry_send_on_fresh_connection(device, node_id, bytes).await\n}","preventionTips":["Evict the connection cache entry on any write failure before retrying","Check close_reason before reusing cached connections","Keep messages small to shorten the write window"],"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"}