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

Projects
None yet
5 participants
@dsander
Contributor

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

This comment has been minimized.

rails-bot commented Apr 17, 2018

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.

This comment has been minimized.

@rafaelfranca

rafaelfranca Apr 17, 2018

Member

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

This comment has been minimized.

@dsander

dsander Apr 17, 2018

Contributor

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.

@rafaelfranca rafaelfranca requested a review from pixeltrix Apr 17, 2018

activesupport/test/time_zone_test_helpers.rb Outdated
end
klass.clear
yield klass
end

This comment has been minimized.

@pixeltrix

pixeltrix Apr 18, 2018

Member

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.

activesupport/test/time_zone_test.rb Outdated
end
end
end

This comment has been minimized.

@pixeltrix

pixeltrix Apr 18, 2018

Member

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

This comment has been minimized.

@dsander

dsander Apr 18, 2018

Contributor

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

@pixeltrix

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

@dsander

This comment has been minimized.

Contributor

dsander commented Apr 18, 2018

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

@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Apr 18, 2018

should I squash the commits into one?

Yes

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

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@pixeltrix

This comment has been minimized.

Member

pixeltrix commented Apr 19, 2018

@dsander thanks! 👍

pixeltrix added a commit that referenced this pull request Apr 19, 2018

Merge pull request #32613 from dsander/fix-as-timezone-all
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