Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash when ActiveJob context include a range with TimeWithZone #2548

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

solnic
Copy link
Collaborator

@solnic solnic commented Feb 6, 2025

If a range consists for ActiveSupport::TimeWithZone,
it will be serialized as a string.

Previously it would raise an error

TypeError: can't iterate from ActiveSupport::TimeWithZone

Closes #2545

@solnic solnic linked an issue Feb 6, 2025 that may be closed by this pull request
Copy link

codecov bot commented Feb 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.10%. Comparing base (cfd5059) to head (6d8f774).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2548      +/-   ##
==========================================
+ Coverage   67.90%   68.10%   +0.20%     
==========================================
  Files         118      118              
  Lines        4483     4480       -3     
==========================================
+ Hits         3044     3051       +7     
+ Misses       1439     1429      -10     
Components Coverage Δ
sentry-ruby 60.71% <ø> (+0.59%) ⬆️
sentry-rails 95.72% <100.00%> (-1.76%) ⬇️
sentry-sidekiq ∅ <ø> (∅)
sentry-resque 92.64% <ø> (ø)
sentry-delayed_job 95.65% <ø> (ø)
sentry-opentelemetry 100.00% <ø> (ø)
Files with missing lines Coverage Δ
sentry-rails/lib/sentry/rails/active_job.rb 93.33% <100.00%> (-6.67%) ⬇️

... and 14 files with indirect coverage changes

If a range consists for ActiveSupport::TimeWithZone,
it will be serialized as a string.

Previously it would raise an error

```
TypeError: can't iterate from ActiveSupport::TimeWithZone
```

Closes #2545
@solnic solnic force-pushed the solnic/2545-activejob-typeerror-cant-iterate-from-activesupporttimewithzone branch from 56ebdce to 5a73d4a Compare February 6, 2025 15:34
@solnic solnic marked this pull request as ready for review February 6, 2025 15:42
@solnic solnic requested review from sl0thentr0py and st0012 and removed request for sl0thentr0py February 6, 2025 15:42
@solnic solnic requested a review from sl0thentr0py February 6, 2025 15:53
{
"integer" => 1,
"post" => "gid://rails-test-app/Post/#{post.id}",
"range" => [1, 2, 3]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's probably not a good idea to do this, but in a way this is public API so I didn't change it.

Copy link

Choose a reason for hiding this comment

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

Yeah, this can cause some crazy performance and memory issues I imagine. 😬

@@ -79,6 +79,12 @@ def sentry_context(job)

def sentry_serialize_arguments(argument)
case argument
when Range
if argument.first.is_a?(ActiveSupport::TimeWithZone)
Copy link

Choose a reason for hiding this comment

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

This would still not support ranges of floats for example. (E.g. (1.1...1.5)) Is that intentional, just to keep this fix small?

Copy link

Choose a reason for hiding this comment

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

@solnic Also, beware that Range#first raises an exception if it's a beginless range (..1).

(..1).first
'Range#first': cannot get the first element of beginless range (RangeError)

Unlike begin, which would return nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@maxim I just left original behavior untouched, but eventually I'd vote for changing it to always dump ranges to a string "{left}..{right}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, beware that Range#first raises an exception if it's a beginless range (..1).

@maxim ah great catch, thanks I addressed it via 6d8f774

@solnic solnic force-pushed the solnic/2545-activejob-typeerror-cant-iterate-from-activesupporttimewithzone branch from 1bee091 to 6d8f774 Compare February 6, 2025 16:09
@solnic solnic changed the title Add a spec checking range serialization in ActiveJob Fix a crash when ActiveJob context include a range with TimeWithZone Feb 7, 2025
if (argument.begin || argument.end).is_a?(ActiveSupport::TimeWithZone)
argument.to_s
else
argument.map { |v| sentry_serialize_arguments(v) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to serialize individual components in the range? If the range is (1..10000) then it'd probably be pretty slow AND it'd allocate a new array with 10k elements in it.
Maybe in this case we just leave the argument until we find another element type that requires special treatments?

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.

ActiveJob TypeError: can't iterate from ActiveSupport::TimeWithZone
3 participants