Issue #11683 has been reported by Hiroshi Shirosaki.

----------------------------------------
Bug #11683: multi-threaded autoload and defined? sometimes fails
https://bugs.ruby-lang.org/issues/11683

* Author: Hiroshi Shirosaki
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.0dev (2015-11-13 trunk 52552) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
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--------------------------------
0001-Add-waiting-for-autoload-in-rb_const_defined_0.patch (2.63 KB)


-- 
https://bugs.ruby-lang.org/