-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Requests are not retried when received body length is shorter than Content-Length #6512
Comments
I really want to work on this issue, but I can't reproduce the bug. For me, the exception ChunkedEncodingError is not raised if the server sends less bytes than indicated by Content-Length. Here is my test case `import requests def test_chunked_encoding_error():
if name == "main": |
Hi, I'm having the same problem, with urllib v2.0.6, requests v2.31.0. Specifically I am getting this when communicating with a gitlab server: When I pin urllib3 back to v1.26.16, the problem goes away. |
Hi @alain-khalil , thanks for taking a look at this issue. The test case included in my proposed patch in the issue description is sufficient to reproduce this issue: I was originally testing on commit 2ee5b0b but can confirm it still works on main (839a8ed). I think the way you're setting @nerdvegas , there were some changes in urllib3 2.x (urllib3/urllib3#2514) which causes urllib3 to (correctly) raise an error when the read comes up short. The previous behavior was to read the truncated data, but (incorrectly) not raise an error. |
Maybe try annother code part like: import requests retry_strategy = Retry( adapter = HTTPAdapter(max_retries=retry_strategy) s = requests.Session() r = s.get('http://127.0.0.1:5000/test', stream=False) print(r) |
@zweger did you ever find a workaround for this? |
When a server sends less bytes than indicated by Content-Length, we get a ChunkedEncodingError instead of retrying the request.
urllib3 supports retrying requests in this situation by setting
preload_content=True
. When a user specifiesstream=True
, obviously, all bets are off: the response cannot be preloaded and therefore the request cannot be retried. However, even whenstream=False
, the response is still not preloaded and therefore the urllib3 retry mechanism in this situation is bypassed.As a background to this issue, I've been investigating rare failures in my CI builds during
pip install
. I believe this issue to be the proximate cause: pip makes some requests to PyPI, withstream=False
and retries configured but still fails.In the current version of pip (which has an out of date urllib3 package), pip falls victim to #4956 and fails to parse the PyPI metadata with a
JSONDecodeError
. Upgrading pip's urllib3 version results in aChunkedEncodingError
as below.Expected Result
The request is retried according to the specified retry policy.
Actual Result
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(10 bytes read, 26227 more expected)', IncompleteRead(10 bytes read, 26227 more expected))
Because the response is not preloaded, urllib3 cannot retry the request, and requests has no retry functionality of its own.
Reproduction Steps
I'm using an intentionally broken local server for testing. See here for an example.
System Information
Proposed Patch
I have a proposed patch which I believe fixes this problem. Unfortunately, my patch breaks a bunch of the tests (and probably also breaks backwards compatibility, in particular, this patch causes requests to start leaking urllib3 exceptions). On the off chance it's useful in coming up with a proper fix, here it is:
The text was updated successfully, but these errors were encountered: