spacedriveapp/spacedrive · error · anyhow::Error

Failed to serialize sync request: {}

Error message

Failed to serialize sync request: {}

What it means

serde_json::to_vec(&request) failed while encoding the outgoing SyncMessage in send_sync_request. For derive-based structs this is nearly infallible; failure points to a nested type whose Serialize impl returned an error (hand-written serializer, exotic field content) rather than any runtime network condition.

Source

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

		let conn = endpoint.connect(node_id.into(), SYNC_ALPN).await.map_err(|e| {
			warn!(
				device_uuid = %target_device,
				node_id = %node_id,
				error = %e,
				"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];

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Read the appended serde error to identify the offending field or type
  2. Add a unit test that round-trips every SyncMessage variant through serde_json
  3. Fix or replace the failing Serialize impl; prefer derive on wire types
  4. Fail fast in tests with an assertion that serialization succeeds

Example fix

// before: no coverage, bug surfaces at runtime in production

// after
#[test]
fn sync_messages_round_trip() {
    for msg in all_sync_message_variants() {
        let bytes = serde_json::to_vec(&msg).expect("SyncMessage must serialize");
        let back: SyncMessage = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(
            std::mem::discriminant(&msg),
            std::mem::discriminant(&back)
        );
    }
}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight in tests and at message construction boundaries
fn assert_serializable(msg: &SyncMessage) -> anyhow::Result<Vec<u8>> {
    serde_json::to_vec(msg).map_err(|e| anyhow::anyhow!("unserializable SyncMessage: {}", e))
}

Try / catch

let bytes = match serde_json::to_vec(&request) {
    Ok(b) => b,
    Err(e) => {
        // serialization bugs must fail loudly in development, never be retried
        tracing::error!(error = %e, "SyncMessage serialization bug");
        return Err(e.into());
    }
};

Prevention

When it happens

Trigger: A SyncMessage variant embedding a type with a custom Serialize that errors; a newly added field type that cannot serialize to JSON; memory exhaustion during allocation.

Common situations: Adding a custom Serialize impl to a domain type reused in wire messages; refactoring shared types into sync payloads without round-trip tests.

Related errors


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