def accept_and_process_next_client(server_socket)
original_pid = Process.pid
client = server_socket.accept
client.binmode
begin
command = client.readline
rescue EOFError
return nil
end
if command !~ /\n\Z/
STDERR.puts "Command must end with a newline"
elsif command == "spawn\n"
while client.readline != "\n"
end
GC.start
pid = fork
if pid.nil?
$0 = "#{$0} (forking...)"
client.puts "OK"
client.puts Process.pid
client.flush
client.sync = true
return [:forked, client]
elsif defined?(NativeSupport)
NativeSupport.detach_process(pid)
else
Process.detach(pid)
end
else
STDERR.puts "Unknown command '#{command.inspect}'"
end
return nil
ensure
if client && Process.pid == original_pid
begin
client.close
rescue Errno::EINVAL
end
end
end