spacedriveapp/spacedrive · error · anyhow::Error

Failed to write message length: {}

Error message

Failed to write message length: {}

What it means

While streaming 64 KiB encrypted chunks, write_all of the 4-byte length prefix failed on the send stream. This is a connection-level failure surfaced at the next write: the peer disconnected, the connection was reset, or a connection error occurred after the previous chunk. Failing at the length prefix rather than mid-payload means the stream broke between chunks.

Source

Thrown at core/src/ops/files/copy/strategy.rs:1217

		let encrypted_data = chunk_data.to_vec();
		let nonce = [0u8; 12]; // Dummy nonce since we're not encrypting

		let chunk_message =
			crate::service::network::protocol::file_transfer::FileTransferMessage::FileChunk {
				transfer_id,
				chunk_index,
				data: encrypted_data,
				nonce,
				chunk_checksum: *chunk_checksum.as_bytes(),
			};

		let message_data = rmp_serde::to_vec(&chunk_message)?;

		send_stream.write_u8(0).await?;
		send_stream
			.write_all(&(message_data.len() as u32).to_be_bytes())
			.await
			.map_err(|e| anyhow::anyhow!("Failed to write message length: {}", e))?;
		send_stream
			.write_all(&message_data)
			.await
			.map_err(|e| anyhow::anyhow!("Failed to write chunk data: {}", e))?;
		send_stream
			.flush()
			.await
			.map_err(|e| anyhow::anyhow!("Failed to flush stream: {}", e))?;

		file_transfer_protocol.record_chunk_received(
			&transfer_id,
			chunk_index,
			bytes_read as u64,
		)?;

		bytes_transferred += bytes_read as u64;
		if let Some(callback) = progress_callback {
			callback(bytes_transferred, total_size);

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Retry the copy job - chunk state is recorded per session and the transfer restarts cleanly
  2. Check the peer daemon for crashes or OOM kills at the failure timestamp
  3. Stabilize the network path (avoid network switching during transfers)
  4. Verify the peer has free disk space for the incoming file
Defensive patterns

Strategy: retry

Try / catch

// Stream writes fail with the connection; retry the whole transfer, not the single write.
let mut attempt = 0;
loop {
    attempt += 1;
    match stream_file_data(/* ... */).await {
        Ok(()) => break,
        Err(ref e) if e.to_string().contains("write") && attempt < 3 => {
            tokio::time::sleep(std::time::Duration::from_secs(attempt as u64)).await;
        }
        Err(e) => return Err(e),
    }
}

Prevention

When it happens

Trigger: Peer drops the connection between chunk writes; peer daemon crashes while processing a previous chunk; network path change (IP rotation) kills the QUIC connection before the next frame is queued.

Common situations: Peer OOM or crash mid-transfer, mobile or VPN networks forcing reconnection, peer disk-full causing its receive side to abort the session.

Related errors


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