spacedriveapp/spacedrive · error · anyhow::Error

Failed to send length: {}

Error message

Failed to send length: {}

What it means

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.

Source

Thrown at core/src/service/network/protocol/sync/transport.rs:169

				"Failed to connect to device for sync request"
			);
			anyhow::anyhow!("Failed to connect to {}: {}", target_device, e)
		})?;

		// Open bidirectional stream
		let (mut send, mut recv) = conn
			.open_bi()
			.await
			.map_err(|e| anyhow::anyhow!("Failed to open bidirectional stream: {}", e))?;

		// Serialize and send request
		let req_bytes = serde_json::to_vec(&request)
			.map_err(|e| anyhow::anyhow!("Failed to serialize sync request: {}", e))?;

		let len = req_bytes.len() as u32;
		send.write_all(&len.to_be_bytes())
			.await
			.map_err(|e| anyhow::anyhow!("Failed to send length: {}", e))?;
		send.write_all(&req_bytes)
			.await
			.map_err(|e| anyhow::anyhow!("Failed to send request: {}", e))?;

		// Properly close send stream
		send.finish()
			.map_err(|e| anyhow::anyhow!("Failed to finish stream: {}", e))?;

		debug!("Sync request sent, waiting for response...");

		// Read response with timeout
		let result = timeout(Duration::from_secs(60), async {
			let mut len_buf = [0u8; 4];
			recv.read_exact(&mut len_buf).await.map_err(|e| {
				anyhow::anyhow!("Failed to read response length: {}", e)
			})?;
			let resp_len = u32::from_be_bytes(len_buf) as usize;

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Retry the whole send on a new connection with backoff
  2. Check connection liveness (close_reason) before reusing it
  3. If persistent, capture the peer's stream reset reason in its logs
Defensive patterns

Strategy: retry

Try / catch

Err(e) if e.to_string().contains("Failed to send length") => {
    // connection was lost mid-send: rebuild connection and retry the request once
    retry_request_on_fresh_connection(device, request).await
}

Prevention

When it happens

Trigger: 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.

Common situations: Flaky links (wifi or VPN flap); peer entering standby mid-request; sync started exactly as the peer app quits.

Related errors


AI-assisted analysis of spacedriveapp/spacedrive@6dfeccf211 (2026-08-16). Data as JSON: /api/errors/48aa4654f995c050. Report an issue: GitHub.