Skip to content
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-118950: Fix SSLProtocol.connection_lost not being called when OSError is thrown on asyncio.write. #118960

Merged
merged 8 commits into from
Oct 24, 2024

Conversation

cjavad
Copy link
Contributor

@cjavad cjavad commented May 12, 2024

Let _SSLProtocolTransport.is_closing reflect SSLProtocol internal transport closing state so that StreamWriter.drain will invoke sleep(0) which calls connection_lost and correctly notifies waiters of connection lost. (#118950)

In an existing comment in asyncio/streams.py there is some logic that correctly ensures that the connection_lost function is always called for users that keep writing and draining in some other context.

The issue here is that this code only runs when the transport is marked as closing, which in the case of the _SSLTransportProtocol only happened after connection_lost has been called.

My proposed fix is for _SSLTransportProtocol.is_closing to also return true when its inner transport is marked as closing, when connection_lost is called the inner transport is removed and instead the _closed flag is set.

No additional tests have been added (yet).

Copy link

cpython-cla-bot bot commented May 12, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented May 12, 2024

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 skip news label instead.

…ocol internal transport closing state so that StreamWriter.drain will invoke sleep(0) which calls connection_lost and correctly notifies waiters of connection lost. (python#118950)
@cjavad cjavad force-pushed the fix-issue-118950 branch from d8c3743 to ca05114 Compare May 12, 2024 01:11
@cjavad
Copy link
Contributor Author

cjavad commented May 12, 2024

As requested @gvanrossum i've opened up a PR for #118950

cjavad added a commit to SimplyPrint/printer-ws-client that referenced this pull request May 18, 2024
…e connection and cancellation and handle exceptions more gracefully. This addresses the 1006 issue.
@gvanrossum
Copy link
Member

@kumaraditya303 Do you have time to review this? I know nothing about SSL. Who else could we ask?

@kumaraditya303
Copy link
Contributor

Please add some tests, I'll try to find time to review in following week.

@Dreamsorcerer
Copy link
Contributor

I'm not familiar with the SSL code, but the change looks reasonable to me and certainly seems to fix the aiohttp issue (aio-libs/aiohttp#3122).

@cjavad
Copy link
Contributor Author

cjavad commented Aug 14, 2024

I will get around to adding some test cases in a few weeks today, but anyone is more than welcome to chip in, you can see my original standalone tests (especially the first one) in the issue.

Note: All tests are passing, the final check never ran due to GitHub going down.

@gvanrossum gvanrossum removed their request for review August 14, 2024 22:48
@cjavad
Copy link
Contributor Author

cjavad commented Oct 20, 2024

Is it realistic to expect this bug fix will be backported to 3.12? Or should we expect it in 3.13 or 3.14?

Copy link
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look good to me.

@1st1 Double-checking with you that backporting to 3.13 and 3.12 makes sense for this PR.

@willingc willingc added the needs backport to 3.12 bug and security fixes label Oct 21, 2024
@1st1
Copy link
Member

1st1 commented Oct 23, 2024

@1st1 Double-checking with you that backporting to 3.13 and 3.12 makes sense for this PR.

I did a cursory review of this and read the sslproto.py code. I think this change is correct is should be ok to port to 3.13/12.

@willingc
Copy link
Contributor

As a courtesy, I will keep this PR open a few more days before merging until @kumaraditya303 can take a look at this again.

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I tweaked the test a bit and the news entry.

@kumaraditya303 kumaraditya303 merged commit 3f24bde into python:main Oct 24, 2024
35 checks passed
@miss-islington-app
Copy link

Thanks @cjavad for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 24, 2024
…n OSError is thrown (pythonGH-118960)

(cherry picked from commit 3f24bde)

Co-authored-by: Javad Shafique <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 24, 2024
…n OSError is thrown (pythonGH-118960)

(cherry picked from commit 3f24bde)

Co-authored-by: Javad Shafique <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 24, 2024

GH-125931 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 24, 2024
@bedevere-app
Copy link

bedevere-app bot commented Oct 24, 2024

GH-125932 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Oct 24, 2024
kumaraditya303 added a commit that referenced this pull request Oct 26, 2024
…en OSError is thrown (GH-118960) (#125931)

gh-118950: Fix SSLProtocol.connection_lost not being called when OSError is thrown (GH-118960)

(cherry picked from commit 3f24bde)

Co-authored-by: Javad Shafique <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303 added a commit that referenced this pull request Oct 26, 2024
…en OSError is thrown (GH-118960) (#125932)

gh-118950: Fix SSLProtocol.connection_lost not being called when OSError is thrown (GH-118960)

(cherry picked from commit 3f24bde)

Co-authored-by: Javad Shafique <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
bdraco added a commit to home-assistant/core that referenced this pull request Nov 1, 2024
python/cpython#118960 has been fixed which means
cleanup closed should no longer be needed
bdraco added a commit to aio-libs/aiohttp that referenced this pull request Nov 9, 2024
python/cpython#118960 has been fixed an
asyncio no longer leaks SSL connections
timo-reymann added a commit to timo-reymann/fork-truststore that referenced this pull request Dec 2, 2024
timo-reymann added a commit to timo-reymann/fork-truststore that referenced this pull request Dec 2, 2024
timo-reymann added a commit to timo-reymann/fork-truststore that referenced this pull request Dec 2, 2024
timo-reymann added a commit to timo-reymann/fork-truststore that referenced this pull request Dec 2, 2024
timo-reymann added a commit to timo-reymann/fork-truststore that referenced this pull request Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants