-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scoped thread panic messages unexpectedly interleaving in stderr output #96706
Comments
This also happens for non-scoped threads. Panic messages are not buffered but written concurrently without any locking piece by piece, so if two threads panic at the same time their backtraces will interleave. The only way around this would be to either buffer the panic message (which may use a lot of memory and could cause the whole panic message to get lost in case an error occurs during panic formatting) or locking stderr (which may cause deadlocks) I agree it is not ideal. |
That's interesting, thanks for providing that information, I didn't know that. Can't we only allow the main thread to write panic messages and capture the panic messages of other threads in that |
You can use a custom panic hook using |
If you are running a concurrent Rust program at any significant scale, as we are at @MaterializeInc, interleaved panic output seems inevitable. I agree that panic messages interleaving with one another is a rare case, but panic messages interleaving with other output produced by the program seems somewhat inevitable. Many production services produce near-constant log output (@MaterializeInc certainly does, and especially so with debug logging enabled), and though panics are rare, when they do strike, it is a near guarantee that the panic message output will interleave badly with log output. Details in MaterializeInc/database-issues#7221. We've looked into some fixes on our side, but so far it seems like the simplest fix would be for the Rust panic handler to buffer the pieces of the panic message internally and then emit them in one single call to |
Digging a bit deeper, and it seems like a contributing factor is the fact that stderr is not line buffered: #64413 |
If anyone else runs into this and is looking for a fix, MaterializeInc/materialize#29325 contains what I believe is a panic handler, usable entirely from stable Rust, that will avoid the interleaving issue in most cases. It reconstructs the standard panic handler messages, attempts to print to stderr under the lock for 1s, and then falls back to printing to stderr not under the lock. |
I tried this code:
I expected to see the following output:
As it runs concurrently, of course the order could be reversed.
Instead, the panic messages were interleaved. Here are some examples:
thread 'thread '<unnamed>' panicked at 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBB<unnamed>', ' panicked at 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', src\main.rs:13:13src\main.rs:8:13
thread '<unnamed>' panicked at 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', src\main.rs:thread '<unnamed>' panicked at '8:13BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
thread '<unnamed>' panicked at 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', thread '<unnamed>src\main.rs:8' panicked at 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBB:13
thread '<unnamed>' panicked at 'thread '<unnamed>' panicked at 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBB', AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', src\main.rs:8:13
thread 'thread '<unnamed><unnamed>' panicked at '' panicked at 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBB', ', src\main.rssrc\main.rs::812::1313
As scoped threads panic directly to
stderr
, I see no measure to prevent this and provide more readable output.Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: