rust-lang / rust-lang/rust

dbg! prints can tear in multi-threading code

Open
#136,703 27 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-fmt C-enhancement T-libs WG-debugging
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

View all comments

This is technically not a bug, but simply a rather frustrating behavior. If you write dbg!(a, b), then the implementation doesn't take a lock on stderr for the entire debug print, but for a and b separately. This means that they can tear with other (debug) prints on stderr. E.g. if you have

 // Thread A.
let (a, b) = (1, 2);
dbg!(a, b);

 // Thread B.
let (a, b) = (3, 4);
dbg!(a, b);

You can see the output (or various other interleavings, it gets worse with more statements and more threads)

[src/some/path.rs] a = 3
[src/some/path.rs] a = 1
[src/some/path.rs] b = 4
[src/some/path.rs] b = 2

A workaround is to use dbg!((a, b)) but I'd rather we just fix the implementation to take a lock once for the entire dbg! call to make the entire debug print atomic.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the dbg! macro implementation and trace how it writes to stderr for multiple arguments. Reproduce the interleaving with the threaded example, then add coverage showing that one dbg! call does not tear with another; done means the complete debug output is emitted atomically.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.