Skip to content

Commit

Permalink
Test adding connection to Reactor after shutdown
Browse files Browse the repository at this point in the history
Modifies `TestPumaServer#shutdown_requests` to pause `Reactor#add` until after
shutdown begins, to ensure requests are handled correctly for this edge case.
Adds unit-test coverage for the fix introduced in puma#2377 and updated in puma#2279.
  • Loading branch information
wjordan committed Oct 7, 2020
1 parent 9ab7a3f commit 147175e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion test/test_puma_server.rb
Expand Up @@ -972,6 +972,22 @@ def shutdown_requests(s1_complete: true, s1_response: nil, post: false, s2_respo
[204, {}, []]
}

pool = @server.instance_variable_get(:@thread_pool)

# Trigger potential race condition by pausing Reactor#add until shutdown begins.
if options.fetch(:queue_requests, true)
reactor = @server.instance_variable_get(:@reactor)
reactor.instance_variable_set(:@pool, pool)
reactor.extend(Module.new do
def add(client)
if client.env['REQUEST_PATH'] == '/s2'
Thread.pass until @pool.instance_variable_get(:@shutdown)
end
super
end
end)
end

s1 = nil
s2 = send_http post ?
"POST /s2 HTTP/1.1\r\nHost: test.com\r\nContent-Type: text/plain\r\nContent-Length: 5\r\n\r\nhi!" :
Expand All @@ -982,7 +998,7 @@ def shutdown_requests(s1_complete: true, s1_response: nil, post: false, s2_respo
app_finished.signal if s1_complete
end
@server.stop
Thread.pass until @server.instance_variable_get(:@thread_pool).instance_variable_get(:@shutdown)
Thread.pass until pool.instance_variable_get(:@shutdown)

assert_match(s1_response, s1.gets) if s1_response

Expand Down

0 comments on commit 147175e

Please sign in to comment.