#!/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}"
