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

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

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 64.01%. Comparing base (cc1dc53) to head (1b9344a).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
sentry-rails/lib/sentry/rails/active_job.rb 66.66% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (cc1dc53) and HEAD (1b9344a). Click for more details.

HEAD has 22 uploads less than BASE
Flag BASE (cc1dc53) HEAD (1b9344a)
129 107
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2548      +/-   ##
==========================================
- Coverage   69.25%   64.01%   -5.25%     
==========================================
  Files         126      123       -3     
  Lines        4759     4707      -52     
==========================================
- Hits         3296     3013     -283     
- Misses       1463     1694     +231     
Components Coverage Δ
sentry-ruby 59.68% <ø> (-0.31%) ⬇️
sentry-rails 59.22% <66.66%> (-37.52%) ⬇️
sentry-sidekiq 97.16% <ø> (ø)
sentry-resque 92.85% <ø> (ø)
sentry-delayed_job 95.65% <ø> (ø)
sentry-opentelemetry 99.31% <ø> (ø)
Files with missing lines Coverage Δ
sentry-rails/lib/sentry/rails/active_job.rb 86.66% <66.66%> (-13.34%) ⬇️

... and 30 files with indirect coverage changes

@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
@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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@st0012 we don't but it's the original behavior - is it OK to change it in a patch/bugfix release?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah is it because it was entering the Enumerable branch? In that case, let's leave it as it is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@st0012 yes exactly :)

@solnic solnic force-pushed the solnic/2545-activejob-typeerror-cant-iterate-from-activesupporttimewithzone branch 6 times, most recently from d3a6db5 to e7db3e1 Compare February 12, 2025 13:32
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 e7db3e1 to bc76440 Compare February 12, 2025 13:44
@solnic
Copy link
Collaborator Author

solnic commented Feb 12, 2025

This PR introduced a new challenge where we have specs that now have min Ruby version requirements due to syntax that's used (end-less and begin-less ranges in this case).

To solve this, I introduced a new rake task called spec:versioned that allows you to define spec/versioned/x.y/**/*_spec.rb specs and they will be run when ruby meets the min version requirements specified via directory name. See 5609d89

@sl0thentr0py @st0012 are you OK with such a solution? One remaining bit is to figure out what to do with Rubocop that has Lint/Syntax and target ruby version set to 2.6, so now it fails.

@sl0thentr0py
Copy link
Member

let's just not use fancy syntax please then

@solnic
Copy link
Collaborator Author

solnic commented Feb 12, 2025

let's just not use fancy syntax please then

oh, it's not that - we just need verify that such ranges work with activejob context param serialization 😅

@sl0thentr0py
Copy link
Member

I assume a simple if RUBY_VERSION guard around the spec doesn't work? If so, fine with this.

@solnic
Copy link
Collaborator Author

solnic commented Feb 12, 2025

I assume a simple if RUBY_VERSION guard around the spec doesn't work? If so, fine with this.

hah yeah that was my instinct but then I realized that ruby parses entire file first, it won't skip anything due to conditionals so we were ending up with syntax errors.

Alright, gonna merge this in, FWIW I think it's gonna be useful for other stuff too.

@solnic solnic force-pushed the solnic/2545-activejob-typeerror-cant-iterate-from-activesupporttimewithzone branch from ed9cba9 to a56bbf8 Compare February 13, 2025 12:46
Our target ruby version is 2.6 so...
@solnic solnic force-pushed the solnic/2545-activejob-typeerror-cant-iterate-from-activesupporttimewithzone branch from a56bbf8 to 1b9344a Compare February 13, 2025 12:51
@solnic solnic merged commit ca7d386 into master Feb 13, 2025
148 of 151 checks passed
@solnic solnic deleted the solnic/2545-activejob-typeerror-cant-iterate-from-activesupporttimewithzone branch February 13, 2025 13:01
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
4 participants