[ruby/net-http] Don't invoke response block more than once due to retry

If a socket error occurs while performing a streaming download via
the response block provided to transport_request, avoid calling
the response block again as this would result in duplicate data
received by the client.

Fixes https://github.com/ruby/net-http/pull/86
Fixes https://github.com/ruby/net-http/pull/87

Fixes [Bug #11526]

https://github.com/ruby/net-http/commit/114d01b092

Co-authored-by: Jeremy Stanley <jeremy@instructure.com>
This commit is contained in:
Jeremy Evans 2024-01-05 08:51:32 -08:00 committed by git
parent 37657c79b6
commit 4d03140009
2 changed files with 14 additions and 1 deletions

View File

@ -2350,7 +2350,10 @@ module Net #:nodoc:
res
}
res.reading_body(@socket, req.response_body_permitted?) {
yield res if block_given?
if block_given?
count = max_retries # Don't restart in the middle of a download
yield res
end
}
rescue Net::OpenTimeout
raise

View File

@ -1234,6 +1234,16 @@ class TestNetHTTPKeepAlive < Test::Unit::TestCase
}
end
def test_http_retry_failed_with_block
start {|http|
http.max_retries = 10
called = 0
assert_raise(Errno::ECONNRESET){ http.get('/'){called += 1; raise Errno::ECONNRESET} }
assert_equal 1, called
}
@log_tester = nil
end
def test_keep_alive_server_close
def @server.run(sock)
sock.close