spacedriveapp/spacedrive · error · anyhow::Error

Failed to serialize sync message: {}

Error message

Failed to serialize sync message: {}

What it means

serde_json::to_vec(&message) failed while encoding the outgoing SyncMessage in send_sync_message (transports/sync.rs). Derive-based serialization is nearly infallible, so this points to a nested type whose Serialize impl returned an error - a code bug in a wire type, not a runtime network condition.

Source

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

				.ok_or_else(|| {
					anyhow::anyhow!(
						"Device {} not found in registry (not paired or offline)",
						target_device
					)
				})?
		};

		tracing::info!(
			"Sending sync message to device {} (node {}), type: {:?}, library: {}",
			target_device,
			node_id,
			std::mem::discriminant(&message),
			message.library_id()
		);

		// 2. Serialize message to bytes
		let bytes = serde_json::to_vec(&message)
			.map_err(|e| anyhow::anyhow!("Failed to serialize sync message: {}", e))?;

		// 3. Get or create connection (with caching for massive performance improvement)
		let endpoint = self
			.endpoint()
			.ok_or_else(|| anyhow::anyhow!("Network endpoint not initialized"))?;

		let active_connections = self.active_connections();
		let cache_key = (node_id, SYNC_ALPN.to_vec());

		// Check cache first - reuse existing connection if alive
		let conn = {
			let connections = active_connections.read().await;
			if let Some(cached_conn) = connections.get(&cache_key) {
				if cached_conn.close_reason().is_none() {
					tracing::debug!(
						device_uuid = %target_device,
						"Reusing cached connection (avoids TLS handshake)"
					);

View on GitHub (pinned to 6dfeccf211)

Solutions

  1. Read the appended serde error to find 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

Example fix

// before: serialization only exercised at runtime

// after
#[test]
fn sync_message_wire_compat() {
    let msg = sample_sync_message();
    let bytes = serde_json::to_vec(&msg).expect("SyncMessage must serialize");
    assert!(serde_json::from_slice::<SyncMessage>(&bytes).is_ok());
}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight the wire format in tests and message builders
#[test]
fn all_variants_serialize() {
    for msg in all_sync_message_variants() {
        assert!(serde_json::to_vec(&msg).is_ok());
    }
}

Try / catch

if let Err(e) = net.send_sync_message(device, msg).await {
    if e.to_string().contains("Failed to serialize sync message") {
        // code bug in a wire type: log loudly, never retry
        tracing::error!(error = %e, "SyncMessage serialization bug");
    }
    return Err(e);
}

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; allocation failure.

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

Related errors


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