[ruby-core:125798] [Ruby Bug#22122] A Ractor/Ractor::Port memory leak (or so it would seem)
Issue #22122 has been reported by miles-georgi (Miles Georgi). ---------------------------------------- Bug #22122: A Ractor/Ractor::Port memory leak (or so it would seem) https://bugs.ruby-lang.org/issues/22122 * Author: miles-georgi (Miles Georgi) * Status: Open * ruby -v: ruby 4.0.5 (2026-05-20 revision 64336ffd0e) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- Hi! If I do something like this: ```ruby #!/usr/bin/env ruby 1000.times do return_port = Ractor::Port.new ractor = Ractor.new(return_port) do |return_port| return_port << Ractor::Port.new while receive end 10_000.times { ractor << true } # If you uncomment this then the leak goes away: # 10_000.times { return_port.receive } ractor << nil ractor.join end GC.start ractor_count = 0 port_count = 0 ObjectSpace.each_object do ractor_count += 1 if Ractor === it port_count += 1 if Ractor::Port === it end puts "ractors: #{ractor_count} ports: #{port_count}" ``` Then I see: ``` ractors: 1001 ports: 10001001 ``` Note that you can also just make the number of loops super big or infinite and just watch the memory grow. But a possible hint: if I uncomment that line that pulls stuff out of the ports it's creating, then I get: ``` ractors: 1 ports: 1 ``` Which is what I would expect since nothing references any of the ractors or ports created by the script and the ractors have all terminated. I can reproduce in both: ruby 4.1.0dev (2026-06-19T18:04:54Z master 1c1dafa769) +PRISM [x86_64-linux] and ruby 4.0.5 (2026-05-20 revision 64336ffd0e) +PRISM [x86_64-linux] Attached is the script. Thanks! ---Files-------------------------------- ractor-and-port-leak-script (542 Bytes) -- https://bugs.ruby-lang.org/
participants (1)
-
miles-georgi (Miles Georgi)