rust-lang/rust-analyzer · error
Unable to serialize data
Error message
Unable to serialize data
What it means
In the JSON profiling layer's on_close, span timing data is serialized with serde_json before being appended to the output writer. serde_json::to_string on a struct of a string name plus an integer elapsed_ms cannot realistically fail, so expect is used as a sanity check; a panic here would indicate corrupted extensions data or serde internals.
Source
Thrown at crates/rust-analyzer/src/tracing/json.rs:70
span.extensions_mut().insert(data);
}
fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, S>) {}
fn on_close(&self, id: Id, ctx: Context<'_, S>) {
#[derive(serde_derive::Serialize)]
struct JsonDataInner {
name: &'static str,
elapsed_ms: u128,
}
let span = ctx.span(&id).unwrap();
let Some(data) = span.extensions_mut().remove::<JsonData>() else {
return;
};
let data = JsonDataInner { name: data.name, elapsed_ms: data.start.elapsed().as_millis() };
let mut out = serde_json::to_string(&data).expect("Unable to serialize data");
out.push('\n');
self.writer.make_writer().write_all(out.as_bytes()).expect("Unable to write data");
}
}
#[derive(Default, Clone, Debug)]
pub(crate) struct JsonFilter {
pub(crate) allowed_names: Option<FxHashSet<String>>,
}
impl JsonFilter {
pub(crate) fn from_spec(spec: &str) -> Self {
let allowed_names = if spec == "*" {
None
} else {
Some(FxHashSet::from_iter(spec.split('|').map(String::from)))
};
View on GitHub (pinned to e8f7e90aa3)
Solutions
- Check any local modifications to JsonDataInner for fields serde cannot serialize (NaN floats, non-string map keys).
- Switch to serde_json::to_string(&data).unwrap_or_default() or log-and-continue if you extend the payload.
- Update/verify serde_json version in the lockfile for known regressions.
Example fix
// before
let mut out = serde_json::to_string(&data).expect("Unable to serialize data");
// after
let mut out = serde_json::to_string(&data)
.unwrap_or_else(|e| format!("{{\"name\":\"serialize-error\",\"elapsed_ms\":-1}}", )); let _ = e; Defensive patterns
Strategy: try-catch
Try / catch
match serde_json::to_string(&data) {
Ok(mut out) => { out.push('\n'); ... }
Err(e) => eprintln!("profile serialization failed: {e}"),
} Prevention
- Keep JsonDataInner fields plain serializable types (String, integers)
- Avoid adding floats that can be NaN to the payload
- Add tests when extending profiler payloads
When it happens
Trigger: Essentially only if JsonData stored in the tracing span extensions was constructed with a name that cannot serialize (practically impossible with a String) or a serde/alloc failure; hit when a profiled span closes while the JSON profiler layer is active.
Common situations: Developers rarely hit this; it may surface when patching the profiler to add new fields to JsonDataInner that are not serializable (e.g. non-string keys in maps, inf/nan floats).
Related errors
- Unable to write data
- A receiver has been dropped, something panicked!
- Unable to convert to an AbsPath
- invalid profile longer_than
- invalid profile depth
AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03).
Data as JSON: /api/errors/89ea25a805ca69e6.
Report an issue: GitHub.