Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

http.client.HTTPResponse.read(-1) handled incorrectly #112064

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@smason

Description

@smason

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:

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.

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

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp