spacedriveapp/spacedrive · error · anyhow::Error

Failed to write message: {}

Error message

Failed to write message: {}

What it means

Writing the serialized SyncMessage body to the unidirectional stream failed after the length prefix was accepted. Same class as the prefix write - connection lost or stream reset mid-write - with a longer exposure window for larger payloads.

Source

Thrown at core/src/service/network/transports/sync.rs:142

		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()
		);

		Ok(())
	}

	/// Send a sync request and wait for response
	///
	/// Uses bidirectional streams for proper request/response pattern (Iroh best practice)
	async fn send_sync_request(

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Retry the send on a fresh connection
  2. Cap message size and chunk large payloads at the protocol level
  3. Log bytes.len() with the error to identify oversized messages
Defensive patterns

Strategy: retry

Try / catch

Err(e) if e.to_string().contains("Failed to write message") => {
    // body write failed mid-transfer: reconnect and resend once
    active_connections.write().await.remove(&cache_key);
    retry_send_on_fresh_connection(device, node_id, bytes).await
}

Prevention

When it happens

Trigger: Connection drop during body transfer; peer resetting the stream after reading only the prefix; oversized messages failing on degraded links.

Common situations: Large messages (big diffs, many records) over slow links; peer standby mid-transfer.

Related errors


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