Project

General

Profile

Actions

Bug #22122

open

A Ractor/Ractor::Port memory leak (or so it would seem)

Bug #22122: A Ractor/Ractor::Port memory leak (or so it would seem)

Added by miles-georgi (Miles Georgi) 6 days ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.0.5 (2026-05-20 revision 64336ffd0e) +PRISM [x86_64-linux]
[ruby-core:125798]

Description

Hi!

If I do something like this:

#!/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) ractor-and-port-leak-script miles-georgi (Miles Georgi), 06/19/2026 07:24 PM

No data to display

Actions

Also available in: PDF Atom