Skip to content

Commit

Permalink
Replace IO.select with IO#wait_* when checking a single IO (puma#2666)
Browse files Browse the repository at this point in the history
* Replace IO.select with IO#wait_* when checking a single IO

* helpers/integration.rb - fix Darwin UNIXSocket errors
  • Loading branch information
MSP-Greg authored and JuanitoFatas committed Sep 9, 2022
1 parent 1d6f7e2 commit 4a2956d
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 11 deletions.
7 changes: 3 additions & 4 deletions lib/puma/client.rb
Expand Up @@ -143,8 +143,7 @@ def reset(fast_check=true)
return false
else
begin
if fast_check &&
IO.select([@to_io], nil, nil, FAST_TRACK_KA_TIMEOUT)
if fast_check && @to_io.wait_readable(FAST_TRACK_KA_TIMEOUT)
return try_to_finish
end
rescue IOError
Expand Down Expand Up @@ -202,13 +201,13 @@ def try_to_finish

def eagerly_finish
return true if @ready
return false unless IO.select([@to_io], nil, nil, 0)
return false unless @to_io.wait_readable(0)
try_to_finish
end

def finish(timeout)
return if @ready
IO.select([@to_io], nil, nil, timeout) || timeout! until try_to_finish
@to_io.wait_readable(timeout) || timeout! until try_to_finish
end

def timeout!
Expand Down
4 changes: 1 addition & 3 deletions lib/puma/cluster.rb
Expand Up @@ -426,9 +426,7 @@ def run

check_workers

res = IO.select([read], nil, nil, [0, @next_check - Time.now].max)

if res
if read.wait_readable([0, @next_check - Time.now].max)
req = read.read_nonblock(1)

@next_check = Time.now if req == "!"
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/cluster/worker.rb
Expand Up @@ -35,7 +35,7 @@ def run

Thread.new do
Puma.set_thread_name "worker check pipe"
IO.select [@check_pipe]
@check_pipe.wait_readable
log "! Detected parent died, dying"
exit! 1
end
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/minissl.rb
Expand Up @@ -162,7 +162,7 @@ def flush
end

def read_and_drop(timeout = 1)
return :timeout unless IO.select([@socket], nil, nil, timeout)
return :timeout unless @socket.wait_readable(timeout)
case @socket.read_nonblock(1024, exception: false)
when nil
:eof
Expand Down
4 changes: 2 additions & 2 deletions lib/puma/request.rb
Expand Up @@ -201,7 +201,7 @@ def fast_write(io, str)
begin
n = io.syswrite str
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
if !IO.select(nil, [io], nil, WRITE_TIMEOUT)
unless io.wait_writable WRITE_TIMEOUT
raise ConnectionError, "Socket timeout writing data"
end

Expand Down Expand Up @@ -419,7 +419,7 @@ def str_headers(env, status, headers, res_info, lines, requests, client)
# of concurrent connections exceeds the size of the threadpool.
res_info[:keep_alive] &&= requests < @max_fast_inline ||
@thread_pool.busy_threads < @max_threads ||
!IO.select([client.listener], nil, nil, 0)
!client.listener.to_io.wait_readable(0)

res_info[:response_hijack] = nil

Expand Down
1 change: 1 addition & 0 deletions lib/puma/server.rb
Expand Up @@ -14,6 +14,7 @@
require 'puma/request'

require 'socket'
require 'io/wait'
require 'forwardable'

module Puma
Expand Down
1 change: 1 addition & 0 deletions test/helpers/integration.rb
Expand Up @@ -176,6 +176,7 @@ def get_worker_pids(phase = 0, size = workers)
# used to define correct 'refused' errors
def thread_run_refused(unix: false)
if unix
DARWIN ? [Errno::ENOENT, Errno::EPIPE, IOError] :
[Errno::ENOENT, IOError]
else
DARWIN ? [Errno::EBADF, Errno::ECONNREFUSED, Errno::EPIPE, EOFError] :
Expand Down

0 comments on commit 4a2956d

Please sign in to comment.