Skip to content
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

MT: reduce interleaved backtraces in spawn unhandled exceptions #14220

Conversation

ysbaddaden
Copy link
Contributor

Instead of doing many individual writes to STDERR that may happen concurrently with other threads, we buffer the message in memory so we can write once.

I tried with 10 000 fibers and 16 threads and I couldn't get any interleaved message. That doesn't mean it can't happen, especially with other concurrent writes to STDERR —which an alternative solution to use a mutex wouldn't prevent it either.

Note: I chose to use a HEAP allocated buffer rather than a stack allocated one because the latter wouldn't be resizeable, and there is no guarantee that the backtrace would fit within 4KB (PIPE_BUF on Linux).

closes #8299

Instead of doing many individual writes to STDERR, that may happen
concurrently with other threads, we can buffer the message in memory so
we can write it once.
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern is that if the error comes from memory exhaustion, this would not help. OTOH, if the exception comes from stack exhaustion, then a stack allocated buffer wouldn't help either...

@beta-ziliani beta-ziliani added this to the 1.12.0 milestone Jan 23, 2024
@straight-shoota straight-shoota merged commit f01de65 into crystal-lang:master Jan 24, 2024
57 checks passed
@ysbaddaden ysbaddaden deleted the mt/8299/reduce-interleaved-backtraces-in-spawn-unhandled-exceptions branch January 24, 2024 10:43
@ysbaddaden
Copy link
Contributor Author

You're right @beta-ziliani. Hum, in case of stack exhaustion this will trigger a segfault that will be handled in a signal handler (no exception raised), but for OOM we will get an exception (which also allocates) that we might be able to catch, so we can use a fixed-size stack buffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Hard to trace errors if raise multiple errors on MT
4 participants