Created
February 13, 2018 16:44
-
-
Save dentarg/8911794c72674d5fd75c0ac6f4dcd696 to your computer and use it in GitHub Desktop.
Problematic spec repro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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` |
Author
dentarg
commented
Feb 13, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment