Skip to content

Commit c5d85d5

Browse files
committed
Improve error message failing to copy proc
Previously, when passing an unshareable proc to a Ractor we would get the message: 'Ractor.new': allocator undefined for Proc (TypeError) With this change we get: 'Ractor.new': can not copy unshareable object #<Proc:0x00007f1b31713600 test.rb:2> (Ractor::Error) Which should hopefully be a little clearer about the problem and make it simpler to debug.
1 parent f1c2faf commit c5d85d5

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

bootstraptest/test_ractor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,14 @@ def test n
507507
}
508508

509509
# To copy the object, now Marshal#dump is used
510-
assert_equal "allocator undefined for Thread", %q{
510+
assert_match /can not copy unshareable object/, %q{
511511
obj = Thread.new{}
512512
begin
513513
r = Ractor.new obj do |msg|
514514
msg
515515
end
516-
rescue TypeError => e
517-
e.message #=> no _dump_data is defined for class Thread
516+
rescue Ractor::Error => e
517+
e.message
518518
else
519519
'ng'
520520
end

ractor.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,9 @@ copy_enter(VALUE obj, struct obj_traverse_replace_data *data)
20752075
return traverse_skip;
20762076
}
20772077
else {
2078+
if (!rb_get_alloc_func(rb_obj_class(obj))) {
2079+
rb_raise(rb_eRactorError, "can not copy unshareable object %+"PRIsVALUE, obj);
2080+
}
20782081
data->replacement = rb_obj_clone(obj);
20792082
return traverse_cont;
20802083
}

test/ruby/test_ractor.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ def test_ifunc_proc_not_shareable
213213
assert_unshareable(pr, /not supported yet/, exception: RuntimeError)
214214
end
215215

216+
def test_copy_unshareable_object_error_message
217+
assert_ractor(<<~'RUBY')
218+
pr = proc {}
219+
err = assert_raise(Ractor::Error) do
220+
Ractor.new(pr) {}.join
221+
end
222+
assert_match(/can not copy unshareable object/, err.message)
223+
RUBY
224+
end
225+
216226
def test_ractor_new_raises_isolation_error_if_outer_variables_are_accessed
217227
assert_raise(Ractor::IsolationError) do
218228
channel = Ractor::Port.new

0 commit comments

Comments
 (0)