Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Bug report
Bug description:
http.client.HTTPResponse doesn't handle negative reads the same as other readers, for example the following code will hang for a significant amount of time:
con=http.client.HTTPConnection("httpbin.org")con.request("GET","/get")resp=con.getresponse()# "connection: close" doesn't trigger thisassertresp.headers["connection"]=="keep-alive"whilechunk:=resp.read(-1):print(chunk)
The negative parameter is passed onto the underlying socket which will cause it to try and read to the end-of-stream. Forkeep-alive connections this just blocks until the connection is closed by the server due to inactivity.
I think this is a bug with not checking for negativeamt values in:
Lines 469 to 471 in24216d0
| ifself.lengthisnotNoneandamt>self.length: | |
| # clip the read to the "end of response" | |
| amt=self.length |
Changing the call toread1 causes the above to display the response promptly as I'd expect. This is due to it correctly checking for negative sizes.
Lines 654 to 655 in24216d0
| ifself.lengthisnotNoneand (n<0orn>self.length): | |
| n=self.length |
Note that in earlier Python versions, e.g. 3.9, the above fails withValueError: negative count which seems better than timing out, but I think reading to the end of the response makes more sense.
CPython versions tested on:
3.11, 3.12
Operating systems tested on:
Linux