optimizer: include panic location and backtrace in caught panic errors#36877
Draft
antiguru wants to merge 1 commit into
Draft
optimizer: include panic location and backtrace in caught panic errors#36877antiguru wants to merge 1 commit into
antiguru wants to merge 1 commit into
Conversation
When a transform panics, `catch_unwind_optimize` demotes it to an internal error. Previously we only recovered the panic payload (the message), so the resulting error had no panic location and the backtrace was lost, making internal optimizer errors hard to track down from a bare message like "index out of bounds". The panic location and backtrace are only available inside the panic handler, which runs before the stack is unwound, but the enhanced handler returns early (without printing) while catching an unwind, so that context was discarded. Add an opt-in capture path in `mz_ore::panic`: a new `catch_unwind_with_details` instructs the enhanced panic handler to stash the panic location and a backtrace into a thread-local before letting the unwind proceed, and returns them alongside the message as a `CaughtPanic`. The backtrace is only captured when a panic actually fires, so there's no per-optimization cost. Other `catch_unwind` callers are unaffected. Use it in the optimizer to surface the panic location in the user-facing error and log the full backtrace.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When a transform panics,
catch_unwind_optimizedemotes it to an internal optimizer error. Today we only get the panic message — no location, no backtrace — so an error like:gives no hint about where in the optimizer the panic happened, making these bugs hard to track down.
The panic location and backtrace are only available inside the panic handler (which runs before the stack is unwound), but
mz_ore's enhanced panic handler returns early — without printing or reporting — while catching an unwind. So that context was discarded, andcatch_unwind_stronly recovered the payload message. There was even a long-standingTODO(teskje): collect and log a backtrace from the panic siteat the call site.Changes
mz_ore::panic— add an opt-in capture path:catch_unwind_with_details(...)works likecatch_unwind_str, but on panic returns aCaughtPanic { message, location, backtrace }.catch_unwindcallers are completely unaffected. If the enhanced handler isn't installed, the message is still recovered andlocation/backtraceare simply absent.mz_transform—catch_unwind_optimizenow usescatch_unwind_with_details:... unexpected panic during query optimization: <msg> (at <file:line:col>).tracing::error!(resolving the old TODO).Tips for reviewer
The behavioral change for existing
catch_unwind/catch_unwind_strcallers is nil — they don't set theCAPTURE_PANIC_DETAILSthread-local, so the handler's early-return path is unchanged for them.This PR was drafted by Claude Code at the request of @mh.
https://claude.ai/code/session_01Av4N3HccSLB4Y3EAjvdngx
Generated by Claude Code