diff --git a/crates/ruff_metrics/src/json.rs b/crates/ruff_metrics/src/json.rs index f52d03da60..02b150396f 100644 --- a/crates/ruff_metrics/src/json.rs +++ b/crates/ruff_metrics/src/json.rs @@ -82,10 +82,6 @@ struct Metric { value: AtomicU64, } -thread_local! { - static BUFFERS: RefCell = RefCell::new(String::new()); -} - impl Metric { fn new(key: &Key, dest: Arc>) -> Metric { let mut json = Map::default(); @@ -108,6 +104,11 @@ impl Metric { where F: FnOnce(&mut String), { + // Render into a thread-local String buffer, and then output the resulting line in a single + // call. This ensures that the output from multiple threads does not get intermingled. + thread_local! { + static BUFFERS: RefCell = RefCell::new(String::new()); + } BUFFERS.with(|buffer| { let mut buffer = buffer.borrow_mut(); buffer.clear();