{"record":{"id":"baf75b1c05619272","repo":"spacedriveapp/spacedrive","slug":"failed-to-serialize-sync-request","errorCode":null,"errorMessage":"Failed to serialize sync request: {}","messagePattern":"Failed to serialize sync request: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"core/src/service/network/protocol/sync/transport.rs","lineNumber":164,"sourceCode":"\t\tlet conn = endpoint.connect(node_id.into(), SYNC_ALPN).await.map_err(|e| {\n\t\t\twarn!(\n\t\t\t\tdevice_uuid = %target_device,\n\t\t\t\tnode_id = %node_id,\n\t\t\t\terror = %e,\n\t\t\t\t\"Failed to connect to device for sync request\"\n\t\t\t);\n\t\t\tanyhow::anyhow!(\"Failed to connect to {}: {}\", target_device, e)\n\t\t})?;\n\n\t\t// Open bidirectional stream\n\t\tlet (mut send, mut recv) = conn\n\t\t\t.open_bi()\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to open bidirectional stream: {}\", e))?;\n\n\t\t// Serialize and send request\n\t\tlet req_bytes = serde_json::to_vec(&request)\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to serialize sync request: {}\", e))?;\n\n\t\tlet len = req_bytes.len() as u32;\n\t\tsend.write_all(&len.to_be_bytes())\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to send length: {}\", e))?;\n\t\tsend.write_all(&req_bytes)\n\t\t\t.await\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to send request: {}\", e))?;\n\n\t\t// Properly close send stream\n\t\tsend.finish()\n\t\t\t.map_err(|e| anyhow::anyhow!(\"Failed to finish stream: {}\", e))?;\n\n\t\tdebug!(\"Sync request sent, waiting for response...\");\n\n\t\t// Read response with timeout\n\t\tlet result = timeout(Duration::from_secs(60), async {\n\t\t\tlet mut len_buf = [0u8; 4];","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/spacedriveapp/spacedrive/blob/6dfeccf2113039e35f2ce735f945e70dc3e4ea45/core/src/service/network/protocol/sync/transport.rs#L146-L182","documentation":"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.","triggerScenarios":"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.","commonSituations":"Adding a custom Serialize impl to a domain type reused in wire messages; refactoring shared types into sync payloads without round-trip tests.","solutions":["Read the appended serde error to identify 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","Fail fast in tests with an assertion that serialization succeeds"],"exampleFix":"// before: no coverage, bug surfaces at runtime in production\n\n// after\n#[test]\nfn sync_messages_round_trip() {\n    for msg in all_sync_message_variants() {\n        let bytes = serde_json::to_vec(&msg).expect(\"SyncMessage must serialize\");\n        let back: SyncMessage = serde_json::from_slice(&bytes).unwrap();\n        assert_eq!(\n            std::mem::discriminant(&msg),\n            std::mem::discriminant(&back)\n        );\n    }\n}","handlingStrategy":"validation","validationCode":"// Pre-flight in tests and at message construction boundaries\nfn assert_serializable(msg: &SyncMessage) -> anyhow::Result<Vec<u8>> {\n    serde_json::to_vec(msg).map_err(|e| anyhow::anyhow!(\"unserializable SyncMessage: {}\", e))\n}","typeGuard":null,"tryCatchPattern":"let bytes = match serde_json::to_vec(&request) {\n    Ok(b) => b,\n    Err(e) => {\n        // serialization bugs must fail loudly in development, never be retried\n        tracing::error!(error = %e, \"SyncMessage serialization bug\");\n        return Err(e.into());\n    }\n};","preventionTips":["Round-trip test every SyncMessage variant through serde_json in CI","Prefer derived Serialize on wire types; avoid custom impls in payloads","Treat any serialization failure as a code bug: fix the type, do not add runtime handling"],"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"}