Skip to content

Instantly share code, notes, and snippets.

@dentarg
Created February 13, 2018 16:44
Show Gist options
  • Select an option

  • Save dentarg/8911794c72674d5fd75c0ac6f4dcd696 to your computer and use it in GitHub Desktop.

Select an option

Save dentarg/8911794c72674d5fd75c0ac6f4dcd696 to your computer and use it in GitHub Desktop.
Problematic spec repro
$stdout.sync = true
$stderr.sync = true
require "socket"
require "timeout"
module PortProber
module_function
def random(host)
server = TCPServer.new(host, 0)
port = server.addr[1]
port
ensure
server&.close
end
# rubocop:disable Lint/RescueWithoutErrorClass
def port_open?(ip, port)
Timeout.timeout(0.1) do # <--- Low timeout gets you dangling puma cluster worker
TCPSocket.new(ip, port).close
true
end
rescue
false
end
# rubocop:enable all
def localhost
info = Socket.getaddrinfo("localhost",
80,
Socket::AF_INET,
Socket::SOCK_STREAM)
raise "unable to translate 'localhost' for TCP + IPv4" if info.empty?
info[0][3]
end
end
# puts $PID
# puts %x`ps -p #{$PID}`
ip = PortProber.localhost
port = PortProber.random(ip)
puts "#{ip}:#{port}"
env = { "PORT" => port.to_s }
command = "bundle exec puma -C config/puma.rb"
r_out, w_out = IO.pipe
pid = Process.spawn(env, command, out: w_out, err: "/dev/null")
Process.detach(pid)
def running?(pid)
Process.getpgid(pid)
true
rescue Errno::ESRCH # No such process
false
end
def not_running_or_port_open?(pid, ip, port)
not_running = !running?(pid)
port_open = PortProber.port_open?(ip, port)
not_running || port_open
end
sleep 0.01 until not_running_or_port_open?(pid, ip, port)
# puts pid
# puts %x`ps -p #{pid}`
begin
Process.kill(:INT, pid)
Process.wait(pid)
rescue Errno::ESRCH, Errno::ECHILD
end
[r_out, w_out].each(&:close)
puts
puts %x`ps aux | grep puma ; ps aux | grep ruby`
@dentarg
Copy link
Author

dentarg commented Feb 13, 2018

$ ruby test2.rb
127.0.0.1:49743

dentarg          66846   1.5  0.1  2552568  26564 s006  R+    5:40PM   0:00.03 puma: cluster worker 0: 66840 [seikan]
dentarg          66851   0.0  0.0  2432804   1964 s006  S+    5:40PM   0:00.00 grep puma
dentarg          66849   0.0  0.0  2442612   2316 s006  S+    5:40PM   0:00.00 sh -c ps aux | grep puma ; ps aux | grep ruby
dentarg          66839   1.5  0.0  2468656   9692 s006  S+    5:40PM   0:00.09 ruby test2.rb
dentarg          66849   1.0  0.0  2442612   2316 s006  S+    5:40PM   0:00.00 sh -c ps aux | grep puma ; ps aux | grep ruby
dentarg          66853   0.0  0.0  2432804   1972 s006  S+    5:40PM   0:00.00 grep ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment