Skip to content

Commit

Permalink
Fix timing out requests too early (puma#2606)
Browse files Browse the repository at this point in the history
* Fix timing out when we get a slow request
Fixes puma#2574 (regression in v5.0.3).

Co-authored-by: Daniel Huckstep <daniel@huckstep.ca>
Co-authored-by: Will Jordan <will@code.org>

* Update test_timeout_in_data_phase
Complete request to ensure short timeout is used properly.

Co-authored-by: Daniel Huckstep <daniel@huckstep.ca>
  • Loading branch information
2 people authored and JuanitoFatas committed Sep 9, 2022
1 parent e7dbc56 commit 7dcbc5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/puma/server.rb
Expand Up @@ -295,6 +295,9 @@ def reactor_wakeup(client)
@thread_pool << client
elsif shutdown || client.timeout == 0
client.timeout!
else
client.set_timeout(@first_data_timeout)
false
end
rescue StandardError => e
client_error(e, client)
Expand Down
28 changes: 27 additions & 1 deletion test/test_puma_server.rb
Expand Up @@ -383,11 +383,13 @@ def test_status_hook_fires_when_server_changes_states
end

def test_timeout_in_data_phase
@server.first_data_timeout = 2
@server.first_data_timeout = 1
server_run

sock = send_http "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\n"

sock << "Hello" unless IO.select([sock], nil, nil, 1.15)

data = sock.gets

assert_equal "HTTP/1.1 408 Request Timeout\r\n", data
Expand All @@ -398,6 +400,30 @@ def test_timeout_data_no_queue
test_timeout_in_data_phase
end

# https://github.com/puma/puma/issues/2574
def test_no_timeout_after_data_received
@server.first_data_timeout = 1
server_run

sock = send_http "POST / HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 11\r\n\r\n"
sleep 0.5

sock << "hello"
sleep 0.5
sock << "world"
sleep 0.5
sock << "!"

data = sock.gets

assert_equal "HTTP/1.1 200 OK\r\n", data
end

def test_no_timeout_after_data_received_no_queue
@server = Puma::Server.new @app, @events, queue_requests: false
test_no_timeout_after_data_received
end

def test_http_11_keep_alive_with_body
server_run app: ->(env) { [200, {"Content-Type" => "plain/text"}, ["hello\n"]] }

Expand Down

0 comments on commit 7dcbc5f

Please sign in to comment.