Project

General

Profile

Actions

Bug #12688

closed

Thread unsafety in autoload

Added by headius (Charles Nutter) over 7 years ago. Updated over 4 years ago.

Status:
Closed
Target version:
-
[ruby-core:76974]

Description

I need clarification here. I expected, based on Ruby's assertion that autoloads are thread-safe, that the following code would never error. Instead, it gets a couple iterations in and raises NameError:

loop do
  class Foo
    autoload :Bar, 'bar.rb'
  end

  go = false
  threads = (1..50).map {Thread.new { 1 until go; print '.'; Foo.const_get(:Bar) }}
  go = true
  threads.each(&:join)
  puts

  self.class.send :remove_const, :Foo
end

And the output with Ruby 2.3.0:

$ ruby23 -I. autoload_breaker.rb 
..................................................
..................................................autoload_breaker.rb:7:in `const_get': uninitialized constant Foo::Bar (NameError)
Did you mean?  Foo::Bar
	from autoload_breaker.rb:7:in `block (3 levels) in <main>'

Is there something wrong with my script? Is my expectation incorrect?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0