clemens/delocalize — Issue #32: is localized_date_time_parser.rb thread safe?
Author: shware
Created: 2011-10-12 22:51:08+00:00
State: open

Hi,

From localized_date_time_parser.rb - line 64:

(@input_formats ||= {})[type] ||= I18n.t(:"#{type}.formats").slice(*I18n.t(:"#{type}.input.formats")).values

Is the use of a class variable (@input_formats) thread safe in Rails?

We've been using the plugin version of delocalize and when we released the app to production (Apache with Phusion Passenger) targeting a diverse user base (multiple cultures / locales) we immediately started getting reports dates being rejected.

If the user's locale used mm/dd/yyyy then occasionally 12/01/2011 (Dec 12 2011) was rejected. Same for dd/mm/yyyy - 01/12/2011 (Dec 01 2011 in UK).

I think the issue is Phusion Passenger "reuses" threads so the class variable @input_formats is sometimes already set from the last user thus treating a US user like a UK user.

After debugging we modified localized_date_time_parser to simply return the formats each time:

I18n.t(:"#{type}.formats", :locale => Thread.current[:current_locale]).slice(*I18n.t(:"#{type}.input.formats", :locale => Thread.current[:current_locale])).values

The Thread.current[:current_locale] is set in our application_controller for each request. This stopped the issue.

I'm not a Rails / Phusion Passenger threading expert so thought I'd pass along in case there's an issue in the gem / plugin or I'm going about things the wrong way.

Thanks for the great software and hope this helps.

Shawn