Skip to content

Commit

Permalink
Do not call Model.name while holding a mutex when setting temporary n…
Browse files Browse the repository at this point in the history
…ames for anonymous modules (Fixes #2273)
  • Loading branch information
jeremyevans committed Feb 3, 2025
1 parent 89c5d9b commit 00d263d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== master

* Do not call Model.name while holding a mutex when setting temporary names for anonymous modules (jeremyevans) (#2273)

=== 5.89.0 (2025-02-01)

* Make connection_validator extension handle case where Database#valid_connection? raises an exception (jeremyevans)
Expand Down
8 changes: 6 additions & 2 deletions lib/sequel/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ def def_column_accessor(*columns)
# default behavior.
def dataset_methods_module
return @dataset_methods_module if defined?(@dataset_methods_module)
Sequel.synchronize{@dataset_methods_module ||= Sequel.set_temp_name(Module.new){"#{name}::@dataset_methods_module"}}
mod_name = "#{name}::@dataset_methods_module"
Sequel.synchronize{@dataset_methods_module ||= Sequel.set_temp_name(Module.new){mod_name}}
extend(@dataset_methods_module)
@dataset_methods_module
end
Expand Down Expand Up @@ -956,7 +957,10 @@ def method_added(meth)
# Module that the class includes that holds methods the class adds for column accessors and
# associations so that the methods can be overridden with +super+.
def overridable_methods_module
include(@overridable_methods_module = Sequel.set_temp_name(Module.new){"#{name}::@overridable_methods_module"}) unless @overridable_methods_module
return @overridable_methods_module if defined?(@overridable_methods_module)
mod_name = "#{name}::@overridable_methods_module"
Sequel.synchronize{@overridable_methods_module ||= Sequel.set_temp_name(Module.new){mod_name}}
include(@overridable_methods_module)
@overridable_methods_module
end

Expand Down
3 changes: 2 additions & 1 deletion lib/sequel/plugins/subset_static_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def subset_static_cache_module
# it before calling creating this module.
dataset_methods_module

Sequel.synchronize{@subset_static_cache_module ||= Sequel.set_temp_name(Module.new){"#{name}::@subset_static_cache_module"}}
mod_name = "#{name}::@subset_static_cache_module"
Sequel.synchronize{@subset_static_cache_module ||= Sequel.set_temp_name(Module.new){mod_name}}
extend(@subset_static_cache_module)
@subset_static_cache_module
end
Expand Down
10 changes: 10 additions & 0 deletions spec/extensions/subset_static_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
c.singleton_class.ancestors[1].name.must_equal "Sequel::_Model(:items)::@subset_static_cache_module"
end if RUBY_VERSION >= '3.3'

it "should give temporary name to name model-specific module" do
c = Sequel::Model(:items)
c.class_eval do
dataset_module{where :foo, :bar}
plugin :subset_static_cache
cache_subset :foo
end
c.singleton_class.ancestors[1].name.must_equal "Sequel::_Model(:items)::@subset_static_cache_module"
end if RUBY_VERSION >= '3.3'

it "should have .with_pk use the cache without a query" do
@ds.with_pk(1)
@ds.with_pk(1).must_equal @c1
Expand Down
10 changes: 10 additions & 0 deletions spec/model/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
m.send(:overridable_methods_module).name.must_equal "Sequel::_Model(:blah)::@overridable_methods_module"
end if RUBY_VERSION >= '3.3'

it "should provide correct temporary names when Model.name calls Sequel.synchronize" do
m = Sequel::Model(:blah)
m.name.must_equal "Sequel::_Model(:blah)"
def m.name
Sequel.synchronize{super}
end
m.send(:dataset_methods_module).name.must_equal "Sequel::_Model(:blah)::@dataset_methods_module"
m.send(:overridable_methods_module).name.must_equal "Sequel::_Model(:blah)::@overridable_methods_module"
end if RUBY_VERSION >= '3.3'

it "should return a model subclass with the given dataset if given a dataset" do
ds = @db[:blah]
c = Sequel::Model(ds)
Expand Down

0 comments on commit 00d263d

Please sign in to comment.