Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions sentry-ruby/lib/sentry/backtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ class Backtrace
# holder for an Array of Backtrace::Line instances
attr_reader :lines

@in_app_pattern_cache = {}

def self.parse(backtrace, project_root, app_dirs_pattern, &backtrace_cleanup_callback)
ruby_lines = backtrace.is_a?(Array) ? backtrace : backtrace.split(/\n\s*/)

ruby_lines = backtrace_cleanup_callback.call(ruby_lines) if backtrace_cleanup_callback

in_app_pattern ||= begin
Regexp.new("^(#{project_root}/)?#{app_dirs_pattern}")
cache_key = app_dirs_pattern
in_app_pattern = @in_app_pattern_cache.fetch(cache_key) do
@in_app_pattern_cache[cache_key] = Regexp.new("^(#{project_root}/)?#{app_dirs_pattern}")
Copy link

Choose a reason for hiding this comment

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

Cache key omits project_root, returning stale Regexp

High Severity

The @in_app_pattern_cache key is only app_dirs_pattern, but the cached Regexp incorporates both project_root and app_dirs_pattern. If project_root changes while app_dirs_pattern stays the same (e.g., across test runs, SDK reconfiguration, or different configurations), the cache returns a stale Regexp built with the old project_root. This causes incorrect in_app classification of backtrace frames, affecting error grouping and display in Sentry.

Fix in Cursor Fix in Web

end

lines = ruby_lines.to_a.map do |unparsed_line|
Expand Down
52 changes: 43 additions & 9 deletions sentry-ruby/lib/sentry/backtrace/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,55 @@ class Line

attr_reader :in_app_pattern

# Cache parsed Line data (file, number, method, module_name) by unparsed line string.
# Same backtrace lines appear repeatedly (same code paths, same errors).
# Values are frozen arrays to avoid mutation.
# Limited to 2048 entries to prevent unbounded memory growth.
PARSE_CACHE_LIMIT = 2048
@parse_cache = {}

# Cache complete Line objects by (unparsed_line, in_app_pattern) to avoid
# re-creating identical Line objects across exceptions.
@line_object_cache = {}
Comment on lines +38 to +42
Copy link

Choose a reason for hiding this comment

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

Bug: The new caching mechanisms in Backtrace::Line use non-thread-safe Hash objects, creating race conditions in multi-threaded environments during cache read, write, and clear operations.
Severity: MEDIUM

Suggested Fix

Replace the plain Hash objects used for @parse_cache and @line_object_cache with a thread-safe alternative. Since the project already depends on concurrent-ruby, using Concurrent::Map is the recommended approach, as it is already used for other caches in the codebase.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: sentry-ruby/lib/sentry/backtrace/line.rb#L38-L42

Potential issue: The new caching mechanisms (`@parse_cache`, `@line_object_cache`) in
`Backtrace::Line` use plain `Hash` objects, which are not thread-safe. In a
multi-threaded environment, concurrent read/write operations can lead to race
conditions. Specifically, the non-atomic sequence of checking the cache size, clearing
it, and then writing a new entry can result in lost entries and an inconsistent cache
state. This can degrade cache effectiveness and potentially cause errors on non-GIL Ruby
implementations like JRuby.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link

Choose a reason for hiding this comment

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

Thread-unsafe plain Hash caches risk corruption on JRuby

Medium Severity

The three new class-level caches (@parse_cache, @line_object_cache, @in_app_pattern_cache) use plain {} hashes, but they're accessed concurrently from multiple threads during exception capture. On JRuby (which the SDK explicitly supports via JAVA_INPUT_FORMAT), there is no GIL, and concurrent reads/writes to a plain Hash can corrupt its internal structure. The existing line_cache in the same file already uses Concurrent::Map for this exact reason.

Additional Locations (1)
Fix in Cursor Fix in Web


# Parses a single line of a given backtrace
# @param [String] unparsed_line The raw line from +caller+ or some backtrace
# @return [Line] The parsed backtrace line
def self.parse(unparsed_line, in_app_pattern = nil)
ruby_match = unparsed_line.match(RUBY_INPUT_FORMAT)
# Try full Line object cache first (avoids creating new objects entirely)
object_cache_key = unparsed_line
pattern_cache = @line_object_cache[object_cache_key]
if pattern_cache
cached_line = pattern_cache[in_app_pattern]
return cached_line if cached_line
end

if ruby_match
_, file, number, _, module_name, method = ruby_match.to_a
file.sub!(/\.class$/, RB_EXTENSION)
module_name = module_name
else
java_match = unparsed_line.match(JAVA_INPUT_FORMAT)
_, module_name, method, file, number = java_match.to_a
cached = @parse_cache[unparsed_line]
unless cached
ruby_match = unparsed_line.match(RUBY_INPUT_FORMAT)

if ruby_match
_, file, number, _, module_name, method = ruby_match.to_a
file.sub!(/\.class$/, RB_EXTENSION)
else
java_match = unparsed_line.match(JAVA_INPUT_FORMAT)
_, module_name, method, file, number = java_match.to_a
end
cached = [file, number, method, module_name].freeze
@parse_cache.clear if @parse_cache.size >= PARSE_CACHE_LIMIT
@parse_cache[unparsed_line] = cached
end
new(file, number, method, module_name, in_app_pattern)

line = new(cached[0], cached[1], cached[2], cached[3], in_app_pattern)

# Cache the Line object — limited by parse cache limit
if @line_object_cache.size >= PARSE_CACHE_LIMIT
@line_object_cache.clear
end
pattern_cache = (@line_object_cache[object_cache_key] ||= {})
pattern_cache[in_app_pattern] = line

line
end

# Creates a Line from a Thread::Backtrace::Location object
Expand Down
Loading