{"record":{"id":"2a06e936a8efb820","repo":"spacedriveapp/spacedrive","slug":"failed-to-serialize-sync-message","errorCode":null,"errorMessage":"Failed to serialize sync message: {}","messagePattern":"Failed to serialize sync message: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"core/src/service/network/transports/sync.rs","lineNumber":57,"sourceCode":"\t\t\t\t.ok_or_else(|| {\n\t\t\t\t\tanyhow::anyhow!(\n\t\t\t\t\t\t\"Device {} not found in registry (not paired or offline)\",\n\t\t\t\t\t\ttarget_device\n\t\t\t\t\t)\n\t\t\t\t})?\n\t\t};\n\n\t\ttracing::info!(\n\t\t\t\"Sending sync message to device {} (node {}), type: {:?}, library: {}\",\n\t\t\ttarget_device,\n\t\t\tnode_id,\n\t\t\tstd::mem::discriminant(&message),\n\t\t\tmessage.library_id()\n\t\t);\n\n\t\t// 2. Serialize message to bytes\n\t\tlet bytes = serde_json::to_vec(&message)\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to serialize sync message: {}\", e))?;\n\n\t\t// 3. Get or create connection (with caching for massive performance improvement)\n\t\tlet endpoint = self\n\t\t\t.endpoint()\n\t\t\t.ok_or_else(|| anyhow::anyhow!(\"Network endpoint not initialized\"))?;\n\n\t\tlet active_connections = self.active_connections();\n\t\tlet cache_key = (node_id, SYNC_ALPN.to_vec());\n\n\t\t// Check cache first - reuse existing connection if alive\n\t\tlet conn = {\n\t\t\tlet connections = active_connections.read().await;\n\t\t\tif let Some(cached_conn) = connections.get(&cache_key) {\n\t\t\t\tif cached_conn.close_reason().is_none() {\n\t\t\t\t\ttracing::debug!(\n\t\t\t\t\t\tdevice_uuid = %target_device,\n\t\t\t\t\t\t\"Reusing cached connection (avoids TLS handshake)\"\n\t\t\t\t\t);","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/spacedriveapp/spacedrive/blob/6dfeccf2113039e35f2ce735f945e70dc3e4ea45/core/src/service/network/transports/sync.rs#L39-L75","documentation":"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.","triggerScenarios":"A SyncMessage variant embedding a type with a custom Serialize that errors; a newly added field type that cannot serialize to JSON; allocation failure.","commonSituations":"Adding a hand-written Serialize impl to a domain type reused in sync messages; refactoring wire payloads without round-trip tests.","solutions":["Read the appended serde error to find the offending field or type","Add a unit test that round-trips every SyncMessage variant through serde_json","Fix or replace the failing Serialize impl; prefer derive on wire types"],"exampleFix":"// before: serialization only exercised at runtime\n\n// after\n#[test]\nfn sync_message_wire_compat() {\n    let msg = sample_sync_message();\n    let bytes = serde_json::to_vec(&msg).expect(\"SyncMessage must serialize\");\n    assert!(serde_json::from_slice::<SyncMessage>(&bytes).is_ok());\n}","handlingStrategy":"validation","validationCode":"// Pre-flight the wire format in tests and message builders\n#[test]\nfn all_variants_serialize() {\n    for msg in all_sync_message_variants() {\n        assert!(serde_json::to_vec(&msg).is_ok());\n    }\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = net.send_sync_message(device, msg).await {\n    if e.to_string().contains(\"Failed to serialize sync message\") {\n        // code bug in a wire type: log loudly, never retry\n        tracing::error!(error = %e, \"SyncMessage serialization bug\");\n    }\n    return Err(e);\n}","preventionTips":["Round-trip test every message variant in CI","Avoid custom Serialize impls in types embedded in sync messages","Treat serialization failures as compile-time-class bugs, not runtime conditions"],"tags":["serde","json","serialization","sync","rust"],"backgroundTag":null,"analyzedSha":"6dfeccf2113039e35f2ce735f945e70dc3e4ea45","analyzedAt":"2026-08-16T11:26:17.074Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}