Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Get ractor message passing working with > 1 thread sending and receiving values (WIP) #12633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 107 additions & 1 deletion bootstraptest/test_ractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def test n
}.sort
}

# an exception in a Ractor will be re-raised at Ractor#receive
# an exception in a Ractor main thread will be re-raised at Ractor#receive
assert_equal '[RuntimeError, "ok", true]', %q{
r = Ractor.new do
raise 'ok' # exception will be transferred receiver
Expand All @@ -558,6 +558,18 @@ def test n
end
}

# an exception in a Ractor non-main thread will not be re-raised at Ractor#receive
assert_equal 'ok', %q{
r = Ractor.new do
Thread.new do
raise 'ng'
end
sleep 0.1
'ok'
end
r.take
}

# threads in a ractor will killed
assert_equal '{ok: 3}', %q{
Ractor.new Ractor.current do |main|
Expand Down Expand Up @@ -2276,3 +2288,97 @@ def initialize(a)

'ok'
}

# There are some bugs in Windows with multiple threads in same ractor calling ractor actions
# Ex: https://github.com/ruby/ruby/actions/runs/14998660285/job/42139383905
unless /mswin/ =~ RUBY_PLATFORM
# r.send and r.take from multiple threads
# [Bug #21037]
assert_equal '[true, true]', %q{
class Map
def initialize
@r = Ractor.new {
loop do
key = Ractor.receive
Ractor.yield key
end
}
end

def fetch(key)
@r.send key
@r.take
end
end

tm = Map.new
t1 = Thread.new { 10.times.map { tm.fetch("t1") } }
t2 = Thread.new { 10.times.map { tm.fetch("t2") } }
vals = t1.value + t2.value
[
vals.first(10).all? { |v| v == "t1" },
vals.last(10).all? { |v| v == "t2" }
]
}

# r.send and Ractor.select from multiple threads
assert_equal '[true, true]', %q{
class Map
def initialize
@r = Ractor.new {
loop do
key = Ractor.receive
Ractor.yield key
end
}
end

def fetch(key)
@r.send key
_r, val = Ractor.select(@r)
val
end
end

tm = Map.new
t1 = Thread.new { 10.times.map { tm.fetch("t1") } }
t2 = Thread.new { 10.times.map { tm.fetch("t2") } }
vals = t1.value + t2.value
[
vals.first(10).all? { |v| v == "t1" },
vals.last(10).all? { |v| v == "t2" }
]
}

# Ractor.receive in multiple threads in same ractor
# [Bug #17624]
assert_equal '["T1 received", "T2 received"]', %q{
r1 = Ractor.new do
output = []
m = Mutex.new
# Start two listener threads
t1 = Thread.new do
Ractor.receive
m.synchronize do
output << "T1 received"
end
end
t2 = Thread.new do
Ractor.receive
m.synchronize do
output << "T2 received"
end
end
sleep 0.1 until [t1,t2].all? { |t| t.status == "sleep" }
Ractor.main.send(:both_blocking)

[t1, t2].each(&:join)
output
end

Ractor.receive # wait until both threads have blocked
r1.send(1)
r1.send(2)
r1.take.sort
}
end
Loading
Loading