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

I18n::Backend::Memoize is not thread-safe #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile.lock
@@ -1,10 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
atomic (1.1.14)
mocha (0.9.9)
rake
rake (0.8.7)
test_declarative (0.0.4)
thread_safe (0.1.3)
atomic

PLATFORMS
java
Expand All @@ -13,3 +16,4 @@ PLATFORMS
DEPENDENCIES
mocha
test_declarative
thread_safe (~> 0.1)
1 change: 1 addition & 0 deletions ci/Gemfile.no-rails
@@ -1,5 +1,6 @@
source 'https://rubygems.org'

gem 'thread_safe', '~> 0.1'
gem 'mocha'
gem 'test_declarative'

4 changes: 4 additions & 0 deletions ci/Gemfile.no-rails.lock
@@ -1,14 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
atomic (1.1.14)
mocha (0.9.9)
rake
rake (0.8.7)
test_declarative (0.0.4)
thread_safe (0.1.3)
atomic

PLATFORMS
ruby

DEPENDENCIES
mocha
test_declarative
thread_safe (~> 0.1)
1 change: 1 addition & 0 deletions ci/Gemfile.rails-2.3.x
@@ -1,5 +1,6 @@
source 'https://rubygems.org'

gem 'thread_safe', '~> 0.1'
gem 'activesupport', '~> 2.3'
gem 'sqlite3-ruby'
gem 'mocha'
Expand Down
4 changes: 4 additions & 0 deletions ci/Gemfile.rails-2.3.x.lock
Expand Up @@ -2,6 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
activesupport (2.3.10)
atomic (1.1.14)
ffi (0.6.3)
rake (>= 0.8.7)
mocha (0.9.9)
Expand All @@ -10,6 +11,8 @@ GEM
rufus-tokyo (1.0.7)
sqlite3-ruby (1.3.2)
test_declarative (0.0.4)
thread_safe (0.1.3)
atomic

PLATFORMS
ruby
Expand All @@ -21,3 +24,4 @@ DEPENDENCIES
rufus-tokyo
sqlite3-ruby
test_declarative
thread_safe (~> 0.1)
1 change: 1 addition & 0 deletions ci/Gemfile.rails-3.x
@@ -1,5 +1,6 @@
source 'https://rubygems.org'

gem 'thread_safe', '~> 0.1'
gem 'activesupport', '~> 3.0.0'
gem 'sqlite3-ruby'
gem 'mocha'
Expand Down
4 changes: 4 additions & 0 deletions ci/Gemfile.rails-3.x.lock
Expand Up @@ -2,6 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.3)
atomic (1.1.14)
ffi (0.6.3)
rake (>= 0.8.7)
mocha (0.9.9)
Expand All @@ -10,6 +11,8 @@ GEM
rufus-tokyo (1.0.7)
sqlite3-ruby (1.3.2)
test_declarative (0.0.4)
thread_safe (0.1.3)
atomic

PLATFORMS
ruby
Expand All @@ -21,3 +24,4 @@ DEPENDENCIES
rufus-tokyo
sqlite3-ruby
test_declarative
thread_safe (~> 0.1)
2 changes: 2 additions & 0 deletions i18n.gemspec
Expand Up @@ -19,6 +19,8 @@ Gem::Specification.new do |s|
s.rubyforge_project = '[none]'
s.required_rubygems_version = '>= 1.3.5'

s.add_dependency 'thread_safe', '~> 0.1'

s.add_development_dependency 'activesupport', '>= 3.0.0'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'mocha'
Expand Down
7 changes: 6 additions & 1 deletion lib/i18n.rb
@@ -1,6 +1,7 @@
require 'i18n/version'
require 'i18n/exceptions'
require 'i18n/interpolate/ruby'
require 'thread_safe'

module I18n
autoload :Backend, 'i18n/backend'
Expand Down Expand Up @@ -268,6 +269,10 @@ def normalize_keys(locale, key, scope, separator = nil)
keys
end

def new_double_nested_cache # :nodoc:
ThreadSafe::Cache.new { |h,k| h[k] = ThreadSafe::Cache.new }
end

# making these private until Ruby 1.9.2 can send to protected methods again
# see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
private
Expand Down Expand Up @@ -320,7 +325,7 @@ def normalize_key(key, separator)
end

def normalized_key_cache
@normalized_key_cache ||= Hash.new { |h,k| h[k] = {} }
@normalized_key_cache ||= new_double_nested_cache
end

# DEPRECATED. Use I18n.normalize_keys instead.
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/backend/flatten.rb
Expand Up @@ -43,7 +43,7 @@ def normalize_flat_keys(locale, key, scope, separator)

# Store flattened links.
def links
@links ||= Hash.new { |h,k| h[k] = {} }
@links ||= I18n.new_double_nested_cache
end

# Flatten keys for nested Hashes by chaining up keys:
Expand Down Expand Up @@ -99,7 +99,7 @@ def resolve_link(locale, key)
end

def find_link(locale, key) #:nodoc:
links[locale].each do |from, to|
links[locale].each_pair do |from, to|
return [from, to] if key[0, from.length] == from
end && nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/memoize.rb
Expand Up @@ -34,7 +34,7 @@ def lookup(locale, key, scope = nil, options = {})
end

def memoized_lookup
@memoized_lookup ||= Hash.new { |h, k| h[k] = {} }
@memoized_lookup ||= I18n.new_double_nested_cache
end

def reset_memoizations!(locale=nil)
Expand Down