Project

General

Profile

Bug #11683

multi-threaded autoload and defined? sometimes fails

Added by h.shirosaki (Hiroshi Shirosaki) about 4 years ago. Updated 5 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0dev (2015-11-13 trunk 52552) [x86_64-linux]
[ruby-core:71471]

Description

I get the following failure. This is a multi-threaded autoload and defined? method test.
If I add wait queue code introduced at r52332 to rb_const_defined_0 function, the test is not failed.
I attached the patch.

diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb
index a672e0b..c71668e 100644
--- a/test/ruby/test_autoload.rb
+++ b/test/ruby/test_autoload.rb
@@ -234,6 +234,28 @@ p Foo::Bar
     end
   end

+  def test_defined_with_autoload
+    ruby_impl_require do |called_with|
+      Tempfile.create(%w(autoload .rb)) do |file|
+        file.puts "class AutoloadTest; module B; end; end"
+        file.flush
+        add_autoload(file.path)
+        begin
+          thrs = []
+          2.times do
+            thrs << Thread.new do
+              Thread.pass; assert_equal("constant", defined? Object::AutoloadTest::B)
+            end
+          end
+          thrs.each(&:join)
+        ensure
+          remove_autoload_constant
+        end
+        assert_equal [file.path], called_with.uniq
+      end
+    end
+  end
+
   def add_autoload(path)
     (@autoload_paths ||= []) << path
     ::Object.class_eval {autoload(:AutoloadTest, path)}
$ while [ $? -eq 0 ]; do make test-all TESTS="-v ruby/test_autoload.rb -n test_defined_with_autoload"; done

(snip)

  1) Failure:
TestAutoload#test_defined_with_autoload [/home/shirosaki/src/ruby/test/ruby/test_autoload.rb:247]:
<"constant"> expected but was
<nil>.

Files


Related issues

Related to Ruby master - Bug #11384: multi-threaded autoload sometimes failsClosedActions

History

Updated by nobu (Nobuyoshi Nakada) about 4 years ago

  • Description updated (diff)

I could reproduce it by iterating 10 times from add_autoload through ensure block.

Updated by h.shirosaki (Hiroshi Shirosaki) about 4 years ago

If I add sleep 0.01 in threads, the test more likely to fail on my Ubuntu 15.10 machine.

require 'tempfile'
Tempfile.create(%w(autoload .rb)) do |file|
  file.puts "class AutoloadTest; module B; end; end"
  file.flush
  autoload(:AutoloadTest, file.path)
  begin
    thrs = []
    2.times do
      thrs << Thread.new do
        Thread.pass
        sleep 0.01
        unless "constant" == defined?(Object::AutoloadTest::B)
          raise "defined? Object::AutoloadTest::B is not constant!"
        end
      end
    end
    thrs.each(&:join)
  end
end
#3

Updated by jeremyevans0 (Jeremy Evans) 5 months ago

  • Related to Bug #11384: multi-threaded autoload sometimes fails added
#4

Updated by jeremyevans0 (Jeremy Evans) 5 months ago

  • Status changed from Open to Closed

Also available in: Atom PDF