{"record":{"id":"917807ff5a29887c","repo":"nautechsystems/nautilus_trader","slug":"failed-to-serialize-msgpack-payload-e","errorCode":null,"errorMessage":"Failed to serialize msgpack `payload`: {e}","messagePattern":"Failed to serialize msgpack `payload`: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/redis/queries.rs","lineNumber":87,"sourceCode":"#[derive(Debug)]\npub struct DatabaseQueries;\n\nimpl DatabaseQueries {\n    /// Serializes the given `payload` using the specified `encoding` to a byte vector.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if serialization to the chosen encoding fails.\n    pub fn serialize_payload<T: Serialize>(\n        encoding: SerializationEncoding,\n        payload: &T,\n    ) -> anyhow::Result<Vec<u8>> {\n        match encoding {\n            SerializationEncoding::MsgPack => {\n                let mut value = serde_json::to_value(payload)?;\n                convert_timestamps(&mut value);\n                rmp_serde::to_vec(&value)\n                    .map_err(|e| anyhow::anyhow!(\"Failed to serialize msgpack `payload`: {e}\"))\n            }\n            SerializationEncoding::Json => {\n                let mut value = serde_json::to_value(payload)?;\n                convert_timestamps(&mut value);\n                serde_json::to_vec(&value)\n                    .map_err(|e| anyhow::anyhow!(\"Failed to serialize json `payload`: {e}\"))\n            }\n            SerializationEncoding::Sbe => {\n                anyhow::bail!(\"SBE encoding is not supported for Redis cache payloads\")\n            }\n            SerializationEncoding::Capnp => {\n                anyhow::bail!(\"Cap'n Proto encoding is not supported for Redis cache payloads\")\n            }\n        }\n    }\n\n    /// Deserializes the given byte slice `payload` into type `T` using the specified `encoding`.\n    ///","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/redis/queries.rs#L69-L105","documentation":"serialize_payload in the Redis queries module converts a payload to serde_json::Value, rewrites timestamps, then encodes as MessagePack via rmp_serde::to_vec. This error wraps any rmp_serde serialization failure — typically values not representable in msgpack after the JSON round-trip.","triggerScenarios":"Calling serialize_payload(SerializationEncoding::MsgPack, payload) where the value cannot be encoded by rmp_serde (e.g. non-string map keys introduced by the JSON value conversion, f64 NaN/Infinity payload fields).","commonSituations":"Payloads containing NaN/Infinity floats (invalid in JSON and unsupported in msgpack), map types with non-string keys, or deeply custom serde impls that produce structures msgpack cannot encode after convert_timestamps mutates the tree.","solutions":["Sanitize the payload: replace or reject NaN/Infinity floats before calling serialize_payload.","Ensure all payload map keys serialize as strings (HashMap<serde-friendly key, _>).","Fall back to SerializationEncoding::Json to see if the failure is msgpack-specific.","Check whether convert_timestamps corrupted the value shape for your custom types."],"exampleFix":"// before\nlet payload = Payload { price: f64::NAN };\nserialize_payload(SerializationEncoding::MsgPack, &payload)?;\n// after\nassert!(payload.price.is_finite());\nserialize_payload(SerializationEncoding::MsgPack, &payload)?;","handlingStrategy":"try-catch","validationCode":"// reject non-finite floats before serialization\nfn payload_is_msgpack_safe(v: &serde_json::Value) -> bool {\n    match v {\n        serde_json::Value::Number(n) => n.as_f64().map(|f| f.is_finite()).unwrap_or(true),\n        serde_json::Value::Array(a) => a.iter().all(payload_is_msgpack_safe),\n        serde_json::Value::Object(o) => o.values().all(payload_is_msgpack_safe),\n        _ => true,\n    }\n}","typeGuard":null,"tryCatchPattern":"match serialize_payload(SerializationEncoding::MsgPack, &payload) {\n    Ok(bytes) => write_to_redis(bytes),\n    Err(e) if e.to_string().contains(\"Failed to serialize msgpack\") => {\n        log::error!(\"payload not msgpack-encodable: {e}; falling back to json\");\n        let bytes = serialize_payload(SerializationEncoding::Json, &payload)?;\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Keep payload types free of NaN/Infinity floats.","Use string-keyed maps in serializable payload structs.","Unit-test serialize_payload round-trips for every payload type."],"tags":["redis","serialization","msgpack"],"backgroundTag":"json-serialization-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}