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 exception in AS::Timezone.all when any tzinfo data is missing #32613

Merged
merged 2 commits into from Apr 19, 2018

Conversation

dsander
Copy link
Contributor

@dsander dsander commented Apr 17, 2018

Before this change missing timezone data for any of the time zones defined in ActiveSupport::Timezone::MAPPING caused a comparison of NilClass with ActiveSupport::TimeZone failed exception.

Attempting to get a timezone by passing a number/duration to [] or calling all directly will try to sort sort the values of zones_map. Those values are initialized by the return value of create(zonename) which returns nil if TZInfo is unable to find the timezone information.

In our case the exception was triggered by an outdated tzdata package which did not include information for the "recently" added time zones.

Before 078421b zones_map only returned the information that have been loaded into @lazy_zone_map which ignored time zones for which the data could not be loaded, this change restores the previous behaviour.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@@ -1,3 +1,8 @@
* Fix bug where `ActiveSupport::Timezone.all` would fail when tzinfo data for
any timezone defined in `ActiveSupport::MAPPING` is missing.
Copy link
Member

Choose a reason for hiding this comment

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

How would this happen in real world? All zones defined in that constant we know exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They do exist if the tzdata system package is current, but when it is not TZInfo does not know anything about them and raises an exception itself. Before 078421b AS::Timezone.all just omitted those timezones.

end
klass.clear
yield klass
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace this method with this:

def with_tz_mappings(mappings)
  old_mappings = ActiveSupport::TimeZone::MAPPING.dup
  ActiveSupport::TimeZone.clear
  ActiveSupport::TimeZone::MAPPING.clear
  ActiveSupport::TimeZone::MAPPING.merge!(mappings)

  yield
ensure
  ActiveSupport::TimeZone.clear
  ActiveSupport::TimeZone::MAPPING.clear
  ActiveSupport::TimeZone::MAPPING.merge!(old_mappings)
end

and use as explained above.

end
end
end

Copy link
Contributor

Choose a reason for hiding this comment

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

While creating a subclass works as a way of reproducing the error it doesn't really reflect real-life usage so I'd prefer a test that more accurately reflects the error which we can do by changing the mappings for this test using the helper method in the comment below. With that you can then change the test to this:

def test_all_not_raises_exception_with_mizzing_tzinfo_data
  mappings = {
    "Puerto Rico" => "America/Unknown",
    "Pittsburgh"  => "America/New_York"
  }

  with_tz_mappings(mappings) do
    assert_nil ActiveSupport::TimeZone["Puerto Rico"]
    assert_nil ActiveSupport::TimeZone[-9]
    assert_nothing_raised do
      ActiveSupport::TimeZone.all
    end
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I wasn't particular happy with creating the subclass and now realize that I created the test from the wrong "perspective".

Copy link
Contributor

@pixeltrix pixeltrix left a comment

Choose a reason for hiding this comment

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

I think the change is 👍 we just need to change the tests as I've outlined, thanks.

@dsander
Copy link
Contributor Author

dsander commented Apr 18, 2018

@pixeltrix Thanks for the feedback, should I squash the commits into one?

@rafaelfranca
Copy link
Member

should I squash the commits into one?

Yes

Before this change missing timezone data for any of the time zones
defined in `ActiveSupport::Timezone::MAPPING` caused a `comparison of
NilClass with ActiveSupport::TimeZone failed` exception.

Attempting to get a timezone by passing a number/duration to `[]` or
calling `all` directly will try to sort sort the values of `zones_map`.
Those values are initialized by the return value of `create(zonename)`
which returns `nil` if `TZInfo` is unable to find the timezone
information.

In our case the exception was triggered by an outdated tzdata package
which did not include information for the "recently" added time zones.

Before 078421b `zones_map` only
returned the information that have been loaded into `@lazy_zone_map`
which ignored time zones for which the data could not be loaded, this
change restores the previous behaviour.
@pixeltrix pixeltrix merged commit b6577cb into rails:master Apr 19, 2018
@pixeltrix
Copy link
Contributor

@dsander thanks! 👍

pixeltrix added a commit that referenced this pull request Apr 19, 2018
Fix exception in AS::Timezone.all when any tzinfo data is missing
bogdanvlviv added a commit to bogdanvlviv/rails that referenced this pull request Apr 19, 2018
pixeltrix added a commit that referenced this pull request Apr 20, 2018
pixeltrix added a commit that referenced this pull request Apr 20, 2018
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.

None yet

5 participants