spacedriveapp/spacedrive · error · anyhow::Error
Failed to write length prefix: {}
Error message
Failed to write length prefix: {} What it means
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.
Source
Thrown at core/src/service/network/transports/sync.rs:133
node_id,
conn: new_conn.clone(),
});
}
new_conn
};
// Open a unidirectional stream and send the message
let mut send = conn
.open_uni()
.await
.map_err(|e| anyhow::anyhow!("Failed to open stream: {}", e))?;
// Write length prefix (required by multiplexer)
let len = bytes.len() as u32;
send.write_all(&len.to_be_bytes())
.await
.map_err(|e| anyhow::anyhow!("Failed to write length prefix: {}", e))?;
// Write message bytes
send.write_all(&bytes).await.map_err(|e| {
warn!(
device_uuid = %target_device,
error = %e,
"Failed to write sync message to stream"
);
anyhow::anyhow!("Failed to write message: {}", e)
})?;
send.finish()
.map_err(|e| anyhow::anyhow!("Failed to finish stream: {}", e))?;
tracing::info!(
"Sync message sent successfully to device {} ({} bytes via uni stream)",
target_device,
bytes.len()View on GitHub (pinned to 6dfeccf211)
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
Defensive patterns
Strategy: retry
Try / catch
Err(e) if e.to_string().contains("Failed to write length prefix") => {
// connection lost at stream start: evict cache entry, reconnect, resend once
active_connections.write().await.remove(&cache_key);
retry_send_on_fresh_connection(device, node_id, bytes).await
} Prevention
- 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
When it happens
Trigger: Cached or fresh connection dropping right at stream open; peer resetting uni streams it cannot match to a protocol; mobile peer suspended mid-send.
Common situations: Flaky links; peer standby; peer restart with the same identity invalidating the cached connection.
Related errors
- Failed to open bidirectional stream: {}
- Failed to send length: {}
- Failed to send request: {}
- Failed to write message: {}
- Failed to finish stream: {}
AI-assisted analysis of spacedriveapp/spacedrive@6dfeccf211 (2026-08-16).
Data as JSON: /api/errors/aff81cfdafe11a96.
Report an issue: GitHub.