Stop building a backtrace in the default rescue handler - #2931
Closed
ericproulx wants to merge 1 commit into
Closed
Conversation
`Middleware::Error#default_rescue_handler`, which renders any exception matched by `rescue_from :all` or `rescue_from SomeError` given without a block, passed `backtrace: exception.backtrace` to the error payload. `Exception#backtrace` builds the whole backtrace as Strings, which at a request's stack depth is the dearest part of rendering the error, and the result was then discarded unless the API had asked for `rescue_from ..., backtrace: true`. `#resolved_backtrace` already falls back to `original_exception.backtrace` when a backtrace is wanted, and the handler passes `original_exception`, so the eager read goes. A 500 from `rescue_from :all` went from 31.5k to 60.8k requests per second. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
perf/lazy-rescue-backtraces
branch
from
September 11, 2026 08:37
e6e5f50 to
5d6b4f7
Compare
Danger ReportNo issues found. |
This was referenced Sep 11, 2026
Contributor
Author
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.
Summary
Middleware::Error#default_rescue_handlerrenders any exception matched byrescue_from :all, or byrescue_from SomeErrorgiven without a block, a common way to turn unexpected errors into 500s. It passedbacktrace: exception.backtraceto the error payload.Exception#backtracebuilds the whole backtrace as Strings, which at a request's stack depth is the dearest part of rendering the error, and the result was then discarded unless the API had asked forrescue_from ..., backtrace: true.#resolved_backtracealready falls back tooriginal_exception.backtracewhen a backtrace is wanted, and the handler passesoriginal_exception, so the eager read goes. What an API that asked for the backtrace renders is unchanged.Benchmarks
Median of 7 interleaved subprocess rounds against master, Ruby 4.0.6, no JIT:
rescue_from :all(no block)rescue_from :allblock callingerror!(control, does not use the default handler)Not changed
Formatterbuilds its payload the same way in two places, for anInvalidFormatterand for a custom parser raising a non-Grape error, but a spec (formatter_spec.rb:509) pins thebacktraceof the payload it throws, and neither path is hot: Grape's own parsers turn a parse failure intoInvalidMessageBody, which does not go through that branch (a malformed JSON body measured +0.1% with both changed). They are left as they are.Behaviour
Byte-identical to master over a 30-case matrix of
rescue_from :allAPIs, with and withoutbacktrace: trueandoriginal_exception: true, in JSON, txt and XML, for raised Ruby and Grape exceptions and malformed bodies: 18 of the responses render a backtrace, identical once paths are normalized. The 228-case error matrix from #2929 is unchanged too.Test plan
original_exception:from the handler, so no backtrace is available when one is asked for, fails 5 specs.🤖 Generated with Claude Code