Programatically set the backtrace_location on exception#1107
Open
Edouard-chin wants to merge 1 commit into
Open
Programatically set the backtrace_location on exception#1107Edouard-chin wants to merge 1 commit into
backtrace_location on exception#1107Edouard-chin wants to merge 1 commit into
Conversation
- ### Problem When a Future is rejected, we set the backtrace with an array of string locations. This makes for some inconsistency because `exception.backtrace_locations` return nil but `exception.backtrace` return the backtrace. I'd like to be able to programmatically set the backtrace_location as well. This would allow some libraries that depend on the backtrace_location to correcly detect the backtrace of an error. ### Solution Since Ruby 3.4, it's now possible to set the backtrace_locations programatically. The way do do it is to call `set_backtrace` but with an array of `Thread::Backtrace::Location` objects.
eregon
approved these changes
Jun 19, 2026
eregon
left a comment
Member
There was a problem hiding this comment.
Looks good, just needs one change
| raise Concurrent::Error, 'it is not rejected' unless rejected? | ||
| raise ArgumentError unless args.size <= 1 | ||
| reason = Array(internal_state.reason).flatten.compact | ||
| locations_supported = RUBY_VERSION >= '3.4' |
Member
There was a problem hiding this comment.
Let's make it a constant like:
Suggested change
| locations_supported = RUBY_VERSION >= '3.4' | |
| SET_BACKTRACE_LOCATIONS_SUPPORTED = RUBY_VERSION >= '3.4' |
I would prefer to feature-detect this but it's probably a bit messy (would cause an exception on older Ruby, not nice with -d) so this is fine enough.
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.
Hello 👋 , thank you for maintaining this library ! I'd like to introduce a change to allow for exception to be consistent when either calling
backtraceorbacktrace_locationson a rejected Future.Problem
When a Future is rejected, we set the backtrace with an array of string locations. This makes for some inconsistency because
exception.backtrace_locationsreturn nil butexception.backtracereturn the backtrace.I'd like to be able to programmatically set the backtrace_location as well. This would allow some libraries that depend on the backtrace_location to correcly detect the backtrace of an error.
Solution
Since Ruby 3.4, it's now possible to set the backtrace_locations programatically. The way do do it is to call
set_backtracebut with an array ofThread::Backtrace::Locationobjects.