Skip to content

Commit 114d01b

Browse files
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 #86 Fixes #87 Fixes [Bug #11526] Co-authored-by: Jeremy Stanley <jeremy@instructure.com>
1 parent 21e226c commit 114d01b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/net/http.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,10 @@ def transport_request(req)
23502350
res
23512351
}
23522352
res.reading_body(@socket, req.response_body_permitted?) {
2353-
yield res if block_given?
2353+
if block_given?
2354+
count = max_retries # Don't restart in the middle of a download
2355+
yield res
2356+
end
23542357
}
23552358
rescue Net::OpenTimeout
23562359
raise

test/net/http/test_http.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,16 @@ def test_http_retry_failed
12341234
}
12351235
end
12361236

1237+
def test_http_retry_failed_with_block
1238+
start {|http|
1239+
http.max_retries = 10
1240+
called = 0
1241+
assert_raise(Errno::ECONNRESET){ http.get('/'){called += 1; raise Errno::ECONNRESET} }
1242+
assert_equal 1, called
1243+
}
1244+
@log_tester = nil
1245+
end
1246+
12371247
def test_keep_alive_server_close
12381248
def @server.run(sock)
12391249
sock.close

0 commit comments

Comments
 (0)