From: "luke-gru (Luke Gruber) via ruby-core" <ruby-core@...>
Date: 2025-02-19T20:42:44+00:00
Subject: [ruby-core:121128] [Ruby master Bug#21149] Strange behavior of ObjectSpace.each_object after Ractor.new

Issue #21149 has been updated by luke-gru (Luke Gruber).


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 did. 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.

----------------------------------------
Bug #21149: Strange behavior of ObjectSpace.each_object after Ractor.new
https://bugs.ruby-lang.org/issues/21149#change-112052

* Author: wanabe (_ wanabe)
* Status: Open
* ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
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]
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/