-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-130902 Add optional socket shutdown to close method of HTTPConnection class in http/client.py #130919
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
Conversation
Similar to pythongh-130862 but for http library
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small triage corrections.
Misc/NEWS.d/next/Library/2025-03-06-16-31-08.gh-issue-130902.mHAtcq.rst
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests should be added for this.
…HAtcq.rst Co-authored-by: Kanishk Pachauri <itskanishkp.py@gmail.com>
Adding to test_httplib.py as a TestCase |
@Mr-Sunglasses The change you requested to the news entry that specified the close method of the httpconnection class resulted in an error because it could not find ":func:
|
@Mr-Sunglasses I also finished adding the tests to test_httplib.py |
…ews entry to avoid docs build error
Misc/NEWS.d/next/Library/2025-03-06-16-31-08.gh-issue-130902.mHAtcq.rst
Outdated
Show resolved
Hide resolved
…HAtcq.rst Co-authored-by: Kanishk Pachauri <itskanishkp.py@gmail.com>
@Mr-Sunglasses done used your new fix ready for review (again) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Please update the documentation of
socket.close()
and mention the use of optionalshutdown
TiA.
|
||
def close(self): | ||
"""Close the connection to the HTTP server.""" | ||
def close(self, shutdown=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add it in the Doc/library/socket.rst
also, about the shutdown
.
Ref.
cpython/Doc/library/socket.rst
Line 1488 in b1b4f96
.. method:: socket.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, shouldn't take too long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mr-Sunglasses Finally ready for review/ Added/updated docs for the close method of HTTPConnection class as well as added back a modifed docstring in http/client.py!
Updated the docstring of the close method of the HTTPConnection class to reflect optional shutdown parameter in http/client.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some small changes in calling of terms.
Par suggestion Co-authored-by: Kanishk Pachauri <itskanishkp.py@gmail.com>
@Mr-Sunglasses applied your small changes for like the billionth time ready for review again :) |
Sorry for being nitty, Thanks a lot 🥇 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @sharktide
nah don't worry. Just been having a rough day |
I had accidentally clicked the close with comment button, just ignore it anyone who sees this pr. Still needs core review |
@picnixz Could you please give a core review for this PR? No one else has done so yet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect. All exceptions are now swallowed but they should be forwarded so that the caller can handle them how they see fit.
There is no practical need to shutdown the socket. The HTTP server may want to keep the connection alive, possibly to make some cleanup on their side. It's just that we don't need it anymore on the client side.
Also, there are other places where .close()
is being called but then without a specific shutdown
(e.g., _tunnel()
). Sorry but I'm rejecting this. Also, it's not socket.close()
that is being changed but http.client.close()
which is entirely different.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Closing per #130902 (comment). |
Similar to gh-130862 but for http library
This pr will close #130902
Overview:
Based on #130902, I added an optional socket shutdown to HTTPConnection.close. The modified function, HTTPConnection.close has a new optional shutdown parameter defaulted to false.
Original function:
Refactored function:
This new change has some benefits such as:
View #130902 for more information
I tested it against a custom test script:
And it passed successfully:
http.client
Possible Improvement: Using shutdown() Before close() inHTTPConnection.close()
#130902