Project

General

Profile

Actions

Misc #18662

closed

Fiber scheduling and Module#autoload

Added by fxn (Xavier Noria) about 2 years ago. Updated almost 2 years ago.

Status:
Closed
Assignee:
-
[ruby-core:108072]

Description

Looks like Fiber context-switching does not synchronize constant reference access. This script using the async gem demonstrates the issue:

require 'tempfile'
require 'async'

Tempfile.create(['foo', '.rb']) do |file|
  file.write(<<~RUBY)
    sleep 0.01
    class C
    end
  RUBY
  file.close

  autoload :C, file.path

  Async do |task|
    3.times do |i|
      task.async { p C }
    end
  end.wait
end

As you'll, it raises NameErrors.

The sleep call in c.rb is just an artificial way to trigger a context switch, but we learned that the debug gem does trigger it due to a TracePoint on script_compiled it installs. Check this other script that also reproduces the error:

require 'tempfile'
require 'async'
require 'debug'

Tempfile.create(['foo', '.rb']) do |file|
  file.write(<<~RUBY)
    class C
      def self.call_me
      end
    end
  RUBY
  file.close

  autoload :C, file.path

  Async do |task|
    5.times do |i|
      task.async do
        p C.call_me
      end
    end
  end.wait
end

See https://github.com/ruby/debug/issues/580 for details.

I didn't tag this as "bug" because I do not know how you'd consider this. On one hand, blocking fibers are switched by the programmer, but non-blocking ones are by a contract interface + scheduler.

What do you think? Could something be done?


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #18663: Autoload doesn't work with fiber context switch.ClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0