Skip to content

fix(transport): log directly sent envelopes in DebugTransport - #3113

Merged
solnic merged 1 commit into
masterfrom
fix/debug-transport-log-envelopes
Oct 6, 2026
Merged

solnic merged 1 commit into
masterfrom
fix/debug-transport-log-envelopes

Conversation

@solnic

@solnic solnic commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

DebugTransport now also logs envelopes passed to send_envelope, such as logs and metrics, not only events. Several processes can write to the log file at once, so each envelope is written in one go and reading skips any unfinished last line.

I stumbled upon this when working on #3057 as SolidQueue workers caused issues with concurrent writing to this debug transport log.

@solnic
solnic marked this pull request as ready for review October 6, 2026 12:32
@solnic
solnic added this pull request to stack #3114 October 6, 2026 12:46
@solnic
solnic force-pushed the fix/debug-transport-log-envelopes branch from 7cb926e to 5037e14 Compare October 6, 2026 13:07

def send_envelope(envelope)
log_envelope(envelope)
backend.send_envelope(envelope)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The logged_envelopes method can crash with an unhandled JSON::ParserError due to concurrent writes to the debug log file corrupting JSON data.
Severity: HIGH

Suggested Fix

Implement file locking using File#flock around the write operation to ensure atomic writes. Additionally, wrap the JSON.parse(line) call within the logged_envelopes method in a begin/rescue JSON::ParserError block to gracefully handle any corrupted lines and prevent crashes.

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/transport/debug_transport.rb#L32

Potential issue: The `DebugTransport` writes to a log file using `File.open(log_file,
"a")` without any file locking. In multi-threaded environments, such as with SolidQueue
workers, concurrent writes can corrupt the log file, especially with large payloads. The
`logged_envelopes` method reads these logs and attempts to parse each line with
`JSON.parse` but lacks a `rescue` block. If a line is corrupted due to interleaved
writes, `JSON.parse` will raise an unhandled `JSON::ParserError`, causing the method to
crash. The existing `fully_written?` check is insufficient as it only verifies a
trailing newline and does not prevent JSON corruption from race conditions.

Also affects:

  • sentry-ruby/lib/sentry/transport/debug_transport.rb:64~64

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

@solnic
solnic merged commit 08a4eca into master Oct 6, 2026
158 checks passed
@solnic
solnic deleted the fix/debug-transport-log-envelopes branch October 6, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants