mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
1e7f99dddf
What's the problem? autoload is thread unsafe. When we define a constant to be autoloaded, we expect the constant construction is invariant. But current autoload implementation allows other threads to access the constant while the first thread is loading a file. What's happening inside? The current implementation uses Qundef as a marker of autoload in Constant table. Once the first thread find Qundef as a value at constant lookup, it starts loading a defined feature. Generally a loaded file overrides the Qundef in Constant table by module/class declaration at very beginning lines of the file, so other threads can see the new Module/Class object before feature loading is finished. It breaks invariant construction. How to solve? To ensure invariant constant construction, we need to override Qundef with defined Object after the feature loading. For keeping Qundef in Constant table, I expanded autoload_data struct in Module to have a slot for keeping the defined object while feature loading. And changed Module's constant lookup/update logic a little so that the slot is only visible from the thread which invokes feature loading. (== the first thread which accessed the autoload constant) Evaluation? All test passes (bootstrap test, test-all and RubySpec) and added 8 tests for threading behavior. Extra logics are executed only when Qundef is found, so no perf drop should happen except autoloading. * variable.c (rb_autoload): Prepare new autoload_data struct. * variable.c (rb_autoload_load): Load feature and update Constant table after feature loading is finished. * variable.c (rb_const_get_0): When the fetched constant is under autoloading, it returns the object only for the thread which starts autoloading. * variable.c (rb_const_defined_0): Ditto. * variable.c (rb_const_set): When the specified constant is under autoloading, it sets the object only for the thread which starts autoloading. Otherwise, simply overrides Qundef with constant override warning. * vm_insnhelper.c (vm_get_ev_const): Apply same change as rb_const_get_0 in variable.c. * test/ruby/test_autoload.rb: Added tests for threading behavior. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33078 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
166 lines
3.9 KiB
Ruby
166 lines
3.9 KiB
Ruby
require 'test/unit'
|
|
require 'tempfile'
|
|
require 'thread'
|
|
require_relative 'envutil'
|
|
|
|
class TestAutoload < Test::Unit::TestCase
|
|
def test_autoload_so
|
|
# Continuation is always available, unless excluded intentionally.
|
|
assert_in_out_err([], <<-INPUT, [], [])
|
|
autoload :Continuation, "continuation"
|
|
begin Continuation; rescue LoadError; end
|
|
INPUT
|
|
end
|
|
|
|
def test_non_realpath_in_loadpath
|
|
require 'tmpdir'
|
|
tmpdir = Dir.mktmpdir('autoload')
|
|
tmpdirs = [tmpdir]
|
|
tmpdirs.unshift(tmpdir + '/foo')
|
|
Dir.mkdir(tmpdirs[0])
|
|
tmpfiles = [tmpdir + '/foo.rb', tmpdir + '/foo/bar.rb']
|
|
open(tmpfiles[0] , 'w') do |f|
|
|
f.puts <<-INPUT
|
|
$:.unshift(File.expand_path('..', __FILE__)+'/./foo')
|
|
module Foo
|
|
autoload :Bar, 'bar'
|
|
end
|
|
p Foo::Bar
|
|
INPUT
|
|
end
|
|
open(tmpfiles[1], 'w') do |f|
|
|
f.puts 'class Foo::Bar; end'
|
|
end
|
|
assert_in_out_err([tmpfiles[0]], "", ["Foo::Bar"], [])
|
|
ensure
|
|
File.unlink(*tmpfiles) rescue nil if tmpfiles
|
|
tmpdirs.each {|dir| Dir.rmdir(dir)}
|
|
end
|
|
|
|
def test_autoload_p
|
|
bug4565 = '[ruby-core:35679]'
|
|
|
|
require 'tmpdir'
|
|
Dir.mktmpdir('autoload') {|tmpdir|
|
|
tmpfile = tmpdir + '/foo.rb'
|
|
a = Module.new do
|
|
autoload :X, tmpfile
|
|
end
|
|
b = Module.new do
|
|
include a
|
|
end
|
|
assert_equal(true, a.const_defined?(:X))
|
|
assert_equal(true, b.const_defined?(:X))
|
|
assert_equal(tmpfile, a.autoload?(:X), bug4565)
|
|
assert_equal(tmpfile, b.autoload?(:X), bug4565)
|
|
}
|
|
end
|
|
|
|
def test_require_explicit
|
|
file = Tempfile.open(['autoload', '.rb'])
|
|
file.puts 'class Object; AutoloadTest = 1; end'
|
|
file.close
|
|
add_autoload(file.path)
|
|
begin
|
|
assert_nothing_raised do
|
|
assert(require file.path)
|
|
assert_equal(1, ::AutoloadTest)
|
|
end
|
|
ensure
|
|
remove_autoload_constant
|
|
end
|
|
end
|
|
|
|
def test_threaded_accessing_constant
|
|
file = Tempfile.open(['autoload', '.rb'])
|
|
file.puts 'sleep 0.5; class AutoloadTest; X = 1; end'
|
|
file.close
|
|
add_autoload(file.path)
|
|
begin
|
|
assert_nothing_raised do
|
|
t1 = Thread.new { ::AutoloadTest::X }
|
|
t2 = Thread.new { ::AutoloadTest::X }
|
|
[t1, t2].each(&:join)
|
|
end
|
|
ensure
|
|
remove_autoload_constant
|
|
end
|
|
end
|
|
|
|
def test_threaded_accessing_inner_constant
|
|
file = Tempfile.open(['autoload', '.rb'])
|
|
file.puts 'class AutoloadTest; sleep 0.5; X = 1; end'
|
|
file.close
|
|
add_autoload(file.path)
|
|
begin
|
|
assert_nothing_raised do
|
|
t1 = Thread.new { ::AutoloadTest::X }
|
|
t2 = Thread.new { ::AutoloadTest::X }
|
|
[t1, t2].each(&:join)
|
|
end
|
|
ensure
|
|
remove_autoload_constant
|
|
end
|
|
end
|
|
|
|
def test_nameerror_when_autoload_did_not_define_the_constant
|
|
file = Tempfile.open(['autoload', '.rb'])
|
|
file.puts ''
|
|
file.close
|
|
add_autoload(file.path)
|
|
begin
|
|
assert_raise(NameError) do
|
|
AutoloadTest
|
|
end
|
|
ensure
|
|
remove_autoload_constant
|
|
end
|
|
end
|
|
|
|
def test_override_autoload
|
|
file = Tempfile.open(['autoload', '.rb'])
|
|
file.puts ''
|
|
file.close
|
|
add_autoload(file.path)
|
|
begin
|
|
eval %q(class AutoloadTest; end)
|
|
assert_equal(Class, AutoloadTest.class)
|
|
ensure
|
|
remove_autoload_constant
|
|
end
|
|
end
|
|
|
|
def test_override_while_autoloading
|
|
file = Tempfile.open(['autoload', '.rb'])
|
|
file.puts 'class AutoloadTest; sleep 0.5; end'
|
|
file.close
|
|
add_autoload(file.path)
|
|
begin
|
|
# while autoloading...
|
|
t = Thread.new { AutoloadTest }
|
|
sleep 0.1
|
|
# override it
|
|
eval %q(AutoloadTest = 1)
|
|
t.join
|
|
assert_equal(1, AutoloadTest)
|
|
ensure
|
|
remove_autoload_constant
|
|
end
|
|
end
|
|
|
|
def add_autoload(path)
|
|
eval <<-END
|
|
class ::Object
|
|
autoload :AutoloadTest, #{path.dump}
|
|
end
|
|
END
|
|
end
|
|
|
|
def remove_autoload_constant
|
|
eval <<-END
|
|
class ::Object
|
|
remove_const(:AutoloadTest)
|
|
end
|
|
END
|
|
end
|
|
end
|