-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-141938/fix-http-client-state-reset #141941
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
gh-141938/fix-http-client-state-reset #141941
Conversation
Handle exceptions during request transmission and close connection on failure.
Handle TimeoutError specifically when sending requests.
Updated the connection creation method to use a lambda for better testability and changed exception handling from TimeoutError to OSError.
Add tests to verify HTTP connection state resets after a timeout and checks state after a successful request.
Handle specific OSError cases to prevent unnecessary connection closure.
|
I will close this PR until we decide what to do in this case. As you can see the test suite is broken with that change. In addition, the PR summary looks AI-generated so please read our policy about this: https://devguide.python.org/getting-started/generative-ai/. I'm also unsure of this weird change. |
|
@picnixz sir I apologize for the confusion. I actually converted this to a Draft because I realized the code was broken and I was still experimenting. I also understand now that I should have waited for the triage process first. |
|
I never said it was AI and apologize if it was understood otherwise. I said it "looked" AI-generated and invited you to read our policy about this. If the code was generated by you, it's fine! however, please do mention when you use AI in the future. Nonetheless, I am still wondering whether to change the current behavior or not. At least docs could be improved but changing the behavior may break consumer code. |
|
No @picnixz sir no need to apologize! Having your guidance is enough for me. ❤️🙏 I'll make sure to clearly mention any tool usage next time. |
Closes gh-141938
Context:
As reported in gh-141938, when using
http.client, if anOSError(such asTimeoutErrororConnectionRefusedError) occurs during_send_request—specifically after the state has transitioned to_CS_REQ_STARTED—theHTTPConnectionobject remains in a "dirty" state.Because the state is not reset to
_CS_IDLE, the connection object becomes unusable. Any subsequent attempts to use.request(), even after calling.connect(), result in aCannotSendRequesterror.The Fix:
request(): Wrapped_send_requestin atry...except OSErrorblock.OSErroroccurs, we checkerrno.errno.EPIPE(Broken Pipe),self.close()is called explicitly. This resets the internal state machine (self.__state) to_CS_IDLEand closes the socket.errno.EPIPEis excluded to preserve existing behavior where a server closes the connection early (e.g., sending a 413 response), allowing the client to still read the response (prevents regression intest_epipe).__init__(): Changedself._create_connectionto use alambdainstead of capturingsocket.create_connectiondirectly.socket.create_connectionis looked up at call time, allowing unit tests to properly mock it for individual requests without the mock being permanently captured by the instance.Verification:
HTTPConnectionStateTeststoLib/test/test_httplib.py.TimeoutErrorresets state toIdle(previously stuck atRequest-sent) and closes the socket.conn.request()call proceeds without raisingCannotSendRequest.test_epipepasses, ensuring no regression for broken pipe scenarios.