gh-113280: Always close socket if SSLSocket creation failed#114659
gh-113280: Always close socket if SSLSocket creation failed#114659serhiy-storchaka merged 5 commits intopython:mainfrom
Conversation
Co-authored-by: Thomas Grainger <[email protected]>
Lib/ssl.py
Outdated
| self = cls.__new__(cls, **kwargs) | ||
| super(SSLSocket, self).__init__(**kwargs) | ||
| sock_timeout = sock.gettimeout() | ||
| sock.detach() | ||
| try: | ||
| sock_timeout = sock.gettimeout() | ||
| sock.detach() |
There was a problem hiding this comment.
I think it would be better to call sock.gettimeout() and sock.detach() outside the try/catch - as if either of those raise you could get a double close
sock_timeout = sock.gettimeout()
kwargs = dict(
family=sock.family, type=sock.type, proto=sock.proto,
fileno=sock.detatch()
)
self = cls.__new__(cls, **kwargs)
super(SSLSocket, self).__init__(**kwargs)
try:
self._context = context
...There was a problem hiding this comment.
But what if __new__ or __init__ raise?
There was a problem hiding this comment.
Then they should be responsible for closing the passed in fileno
There was a problem hiding this comment.
They are not. If you pass a fileno, you are responsible (in this case it is the responsibility of sock until detach() is called).
Co-authored-by: Thomas Grainger <[email protected]>
|
I added tests, although the original case reported in the issue is not covered by tests. But I added tests for two cases in which an explicit |
|
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12. |
…thonGH-114659) (cherry picked from commit 0ea3662) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Thomas Grainger <[email protected]>
|
GH-114995 is a backport of this pull request to the 3.12 branch. |
…thonGH-114659) (cherry picked from commit 0ea3662) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Thomas Grainger <[email protected]>
|
GH-114996 is a backport of this pull request to the 3.11 branch. |
…H-114659) (GH-114995) (cherry picked from commit 0ea3662) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Thomas Grainger <[email protected]>
…H-114659) (GH-114996) (cherry picked from commit 0ea3662) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Thomas Grainger <[email protected]>
…thonGH-114659) Co-authored-by: Thomas Grainger <[email protected]>
…thonGH-114659) Co-authored-by: Thomas Grainger <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.