Bug #21149
closedStrange behavior of ObjectSpace.each_object after Ractor.new
Description
I found the strange behavior of ObjectSpace.each_object.
It misses some objects after Ractor.new.
$ ruby -ve 'class Foo; end; foo = Foo.new; Ractor.new do buz = Foo.new end.take; bar = Foo.new; p [ObjectSpace.each_object(Foo).to_a, foo.class, bar.class]'
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
-e:1: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
[[], Foo, Foo]
Of course it works well without Ractor.new.
$ ruby -ve 'class Foo; end; foo = Foo.new; bar = Foo.new; p [ObjectSpace.each_object(Foo).to_a, foo.class, bar.class]'
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
[[#<Foo:0x00007fa84f4b26d0>, #<Foo:0x00007fa84f4b27c0>], Foo, Foo]
Updated by nobu (Nobuyoshi Nakada) 10 months ago
Interesting.
It seems irrelevant what is done in the new Ractor.
Even if the block is empty, the result is same.
Updated by luke-gru (Luke Gruber) 10 months ago
ยท Edited
Yes, this is a known bug. Currently, objects don't record which ractor they belong to. Therefore, after you start new Ractor Ruby can't know if it should yield an unshareable object to ObjectSpace.each_object, because it would violate Ractor safety if it yielded it and that object didn't belong to the ractor that called each_object. You can see that shareable objects (like if you froze it) are yielded.
You can see the code here: https://github.com/ruby/ruby/blob/master/gc.c#L1526-L1528
From what I remember, this is documented somewhere but I could be wrong. If it's not documented, it should be.
Updated by wanabe (_ wanabe) 10 months ago
- Status changed from Open to Rejected
Thank you. I forgot to search existing issues before opening the ticket. Now I can see many related tickets:
- [Feature #17270] ObjectSpace.each_object should be restricted on multi-Ractors
- [Bug #17360] Objects disappear from ObjectSpace after using Ractor
- [Bug #19387] Issue with ObjectSpace.each_objects not returning objects after starting a ractor
I will reject this #21149 as duplicated.
Updated by wanabe (_ wanabe) 10 months ago
- Related to Feature #17270: ObjectSpace.each_object should be restricted on multi-Ractors added
Updated by wanabe (_ wanabe) 10 months ago
- Is duplicate of Bug #17360: Objects disappear from ObjectSpace after using Ractor added
- Is duplicate of Bug #19387: ObjectSpace.each_objects only returns shareable objects after starting a Ractor added