From: "skhrshin (Shintaro Sakahara) via ruby-core" Date: 2024-05-13T08:25:08+00:00 Subject: [ruby-core:117857] [Ruby master Bug#20485] Simple use of Fiber makes GC leak objects with singleton method Issue #20485 has been updated by skhrshin (Shintaro Sakahara). I asked my co-workers to try this script and some of them gave me their results. The following table includes my results. |Environment|# of people|Reproducibility| |--|--|--| |ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux] on Ubuntu/WSL2|1|Probably 100%| |ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux] on Ubuntu/virtualbox|2|Very high but less than 100%| |ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux] on Docker Desktop/Windows|1|High but less than 100%| |ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux] on Ubuntu/Hyper-V|1|Low (about 10%)| |ruby 3.3.1 (2024-04-23 revision c56cd86388) +YJIT [arm64-darwin23]|1|0%| |ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-darwin22]|1|0%| The person who tried it on `ruby 3.3.1 (2024-04-23 revision c56cd86388) +YJIT [arm64-darwin23]` also gave me the results on several Ruby versions. He said it was reproducible on 3.2.4, but not on 3.2.2. I created a dump log by putting `ObjectSpace.dump_all(output: :stdout)` before `raise "NG"` and uploaded it to GitHub. This log doesn't contain `ObjectSpace.dump(work)` you've suggested because with putting something like `ObjectSpace.dump(work)`, `puts 0` or `sleep 1` between `work.add_method` and `work = nil` the script doesn't reproduce the problem. https://gist.github.com/skhrshin/f639e387578db8faf431adfb7ac06631#file-bugs-ruby-lang-org_issues_20485_dump_all-log As far as I investigated, I couldn't find any OBJECT that prevented `work` from being GCed. The address of `work` looks to be 0x7f2b552d0bb8. I don't have any knowledge about what IMEMO is. I would appreciate it if you could help me. ---------------------------------------- Bug #20485: Simple use of Fiber makes GC leak objects with singleton method https://bugs.ruby-lang.org/issues/20485#change-108266 * Author: skhrshin (Shintaro Sakahara) * Status: Open * ruby -v: ruby 3.2.4 (2024-04-23 revision af471c0e01) [x86_64-linux] * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- I found a possible memory leak which occurs only when several conditions are met. The code to reproduce the problem is below: ``` class Work def add_method singleton_class.define_method(:f) {} end end 1.times { Fiber.new {}.resume } work = Work.new work.add_method work = nil GC.start num_objs = ObjectSpace.each_object.select { |o| o.is_a?(Work) rescue false }.size unless num_objs.zero? raise "NG" end ``` Expected result: The script exits normally. Actual result: RuntimeError "NG" is raised. If I change `1.times { Fiber.new {}.resume }` to just `Fiber.new {}.resume` or remove `work.add_method`, GC works as expected. Is there any problem at the way to use Fiber in this code, or is it a bug due to Ruby? I tested ruby 3.3.1 (2024-04-23 revision c56cd86388) [x86_64-linux] too and the result was a little different. The code above didn't reproduce the problem, but if I changed `1.times` to `Mutex.new.synchronize`, it was able to reproduce. -- 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/postorius/lists/ruby-core.ml.ruby-lang.org/