dbg! prints can tear in multi-threading code
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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