René's URL Explorer Experiment


Title: Inefficient ssl.SSLWantReadError exception slows down very common use-case · Issue #123954 · python/cpython · GitHub

Open Graph Title: Inefficient ssl.SSLWantReadError exception slows down very common use-case · Issue #123954 · python/cpython

X Title: Inefficient ssl.SSLWantReadError exception slows down very common use-case · Issue #123954 · python/cpython

Description: Bug report Bug description: Event loops like uvloop, asyncio use nonblocking ssl. They typically read data from the socket when epoll returns that it is ready push data to the incoming MemoryBIO read from SSLObject until SSLWantReadError...

Open Graph Description: Bug report Bug description: Event loops like uvloop, asyncio use nonblocking ssl. They typically read data from the socket when epoll returns that it is ready push data to the incoming MemoryBIO re...

X Description: Bug report Bug description: Event loops like uvloop, asyncio use nonblocking ssl. They typically read data from the socket when epoll returns that it is ready push data to the incoming MemoryBIO re...

Opengraph URL: https://github.com/python/cpython/issues/123954

X: @github

direct link

Domain: patch-diff.githubusercontent.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inefficient ssl.SSLWantReadError exception slows down very common use-case","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nEvent loops like uvloop, asyncio use nonblocking ssl. They typically \r\n\r\n1. read data from the socket when epoll returns that it is ready\r\n2. push data to the incoming MemoryBIO\r\n3. read from SSLObject until SSLWantReadError is thrown\r\n4. pass read data to the application protocol\r\n\r\nwhen peers are exchanging relatively small messages, SSLObject.read is typically called 2 times . First call returns data, second - throws SSLWantReadError\r\n\r\nperf shows that the second call is almost as expensive as the first call because of time spent on constructing new exception object. \r\n\r\nIs it possible to optimize exception object creation for the second call?\r\n\r\nI tried to avoid the second call by analyzing MemoryBIO.pending and SSLObject.pending values but they can't always reliably tell that we have to wait for more data.\r\n\r\nFor example, it is possible that incoming MemoryBIO.pending \u003e 0, SSLObject.pending == 0. We call SSLObject.read and it throws because incoming MemoryBIO doesn't have the full ssl frame yet.\r\n\r\nExample echo client that replicates internal logic in asyncio/uvloop:\r\n\r\n```python\r\nimport socket\r\nimport ssl\r\nimport select\r\nfrom typing import Optional\r\n\r\nssl_context = ssl.create_default_context()\r\nssl_context.check_hostname = False\r\nssl_context.verify_mode = ssl.CERT_NONE\r\n\r\nep = select.epoll(2)\r\n\r\nincoming = ssl.MemoryBIO()\r\noutgoing = ssl.MemoryBIO()\r\n\r\nsock: Optional[socket.socket] = None\r\nssl_sock: Optional[ssl.SSLObject] = None\r\n\r\n\r\ndef wait_data():\r\n    ep.poll()\r\n\r\n    try:\r\n        while True:\r\n            chunk = sock.recv(1024)\r\n            incoming.write(chunk)\r\n    except BlockingIOError:\r\n        pass\r\n\r\n\r\ndef wait_data_until_ssl_read_succeed():\r\n    data = bytearray()\r\n    try:\r\n        wait_data()\r\n        # while ssl_sock.pending() \u003e 0 or incoming.pending \u003e 0:\r\n        while True:\r\n            data += ssl_sock.read()\r\n    except ssl.SSLWantReadError:\r\n        pass\r\n\r\n    return data\r\n\r\n\r\nwith socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\r\n    ep.register(sock.fileno(), select.EPOLLIN)\r\n\r\n    ssl_sock = ssl_context.wrap_bio(incoming, outgoing, server_hostname='localhost')\r\n\r\n    sock.connect(('127.0.0.1', 25000))\r\n    sock.setblocking(False)\r\n\r\n    handshake_complete = False\r\n    message_sent = False\r\n\r\n    msg = b\"a\" * 256\r\n\r\n    # do handshake\r\n    while True:\r\n        try:\r\n            ssl_sock.do_handshake()\r\n            break\r\n        except ssl.SSLWantReadError as ex:\r\n            if outgoing.pending \u003e 0:\r\n                chunk = outgoing.read(outgoing.pending)\r\n                sock.send(chunk)\r\n            wait_data()\r\n\r\n    # send message and wait for reply\r\n    while True:\r\n        ssl_sock.write(msg)\r\n        chunk = outgoing.read(outgoing.pending)\r\n        sock.send(chunk)\r\n\r\n        data = wait_data_until_ssl_read_succeed()\r\n        # print(data)\r\n```\r\n\r\nPerf output:\r\n```\r\n   17.41%     0.25%            43  python   _ssl.cpython-314-x86_64-linux-gnu.so     [.] _ssl__SSLSocket_read\r\n            |          \r\n             --17.16%--_ssl__SSLSocket_read\r\n                       |          \r\n                       |--8.09%--SSL_read_ex\r\n                       |          |          \r\n                       |           --7.74%--0x7b1fbef883f9\r\n                       |                     |          \r\n                       |                     |--4.83%--0x7b1fbefadc22\r\n                       |                     |          |          \r\n                       |                     |          |--0.93%--0x7b1fbefa6919\r\n                       |                     |          |          |          \r\n                       |                     |          |           --0.90%--EVP_DecryptUpdate\r\n                       |                     |          |                     |          \r\n                       |                     |          |                      --0.90%--0x7b1fbec90c8b\r\n                       |                     |          |                                |          \r\n                       |                     |          |                                 --0.87%--0x7b1fbec90b45\r\n                       |                     |          |          \r\n                       |                     |          |--0.79%--0x7b1fbefa64a5\r\n                       |                     |          |          |          \r\n                       |                     |          |           --0.61%--EVP_CIPHER_CTX_get_iv_length\r\n                       |                     |          |          \r\n                       |                     |          |--0.72%--0x7b1fbefa6721\r\n                       |                     |          |          |          \r\n                       |                     |          |           --0.60%--EVP_CipherInit_ex\r\n                       |                     |          |          \r\n                       |                     |          |--0.57%--0x7b1fbefa6750\r\n                       |                     |          |          \r\n                       |                     |           --0.52%--0x7b1fbefa68f0\r\n                       |                     |          \r\n                       |                     |--1.13%--0x7b1fbefadf94\r\n                       |                     |          |          \r\n                       |                     |           --0.52%--0x7b1fbefac3c7\r\n                       |                     |          \r\n                       |                      --0.68%--0x7b1fbefad750\r\n                       |          \r\n                       |--6.21%--PySSL_SetError.constprop.0\r\n                       |          |          \r\n                       |           --5.17%--fill_and_set_sslerror\r\n                       |                     |          \r\n                       |                     |--2.83%--PyUnicode_FromFormat\r\n                       |                     |          |          \r\n                       |                     |           --2.54%--unicode_from_format\r\n                       |                     |                     |          \r\n                       |                     |                      --1.23%--__sprintf_chk\r\n                       |                     |                                __vsprintf_internal\r\n                       |                     |                                |          \r\n                       |                     |                                 --0.99%--__vfprintf_internal\r\n                       |                     |          \r\n                       |                      --1.01%--PyObject_SetAttr\r\n                       |                                |          \r\n                       |                                 --0.98%--PyObject_GenericSetAttr\r\n                       |                                           |          \r\n                       |                                            --0.52%--_PyObjectDict_SetItem\r\n                       |          \r\n                        --0.60%--SSL_get_error\r\n```\r\n\r\nTo reproduce you would need some ssl echo server running on localhost 25000 port. After you have started it, run echo client code under perf.\r\n\r\n```\r\n$ perf record -F 999 -g --call-graph lbr --user-callchains -- python echo_client.py\r\n$ perf report -G -n --stdio\r\n```\r\nLet it work for 15 seconds and then press Ctrl-C\r\n\r\n\r\n### CPython versions tested on:\r\n\r\nCPython main branch\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-128391\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/tarasko","@type":"Person","name":"tarasko"},"datePublished":"2024-09-11T14:24:26.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/123954/cpython/issues/123954"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:2d254a62-fb21-767d-34c8-48d533c4271c
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8D44:244BC2:3DB43A:5321E4:696E9456
html-safe-nonce28b40309936204e04f368ec4978985673c321c97d5960c1d01a0f017536f9ca6
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDQ0OjI0NEJDMjozREI0M0E6NTMyMUU0OjY5NkU5NDU2IiwidmlzaXRvcl9pZCI6IjMwMzEwNTkxMDk0NzQzNzQ3NDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmaccbb70378f513436505bbc3eeb0868b8a9891e903ccaac00a2df6287370b23cef
hovercard-subject-tagissue:2519904330
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/123954/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c5bcd4810455b10d6f5deebe857b07c109fbb3b96ea18be70863ec23412d552a/python/cpython/issues/123954
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c5bcd4810455b10d6f5deebe857b07c109fbb3b96ea18be70863ec23412d552a/python/cpython/issues/123954
og:image:altBug report Bug description: Event loops like uvloop, asyncio use nonblocking ssl. They typically read data from the socket when epoll returns that it is ready push data to the incoming MemoryBIO re...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernametarasko
hostnamegithub.com
expected-hostnamegithub.com
Nonefdad15fd2ad43212aa8b8be5f2c2725550f8374ceeeb154a999ad9145b43f3f7
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release27b23bc056eb973d350fc95afc848757edb9e7a9
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/python/cpython/issues/123954#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F123954
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F123954
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://patch-diff.githubusercontent.com/python/cpython/issues/123954
Reloadhttps://patch-diff.githubusercontent.com/python/cpython/issues/123954
Reloadhttps://patch-diff.githubusercontent.com/python/cpython/issues/123954
python https://patch-diff.githubusercontent.com/python
cpythonhttps://patch-diff.githubusercontent.com/python/cpython
Please reload this pagehttps://patch-diff.githubusercontent.com/python/cpython/issues/123954
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://patch-diff.githubusercontent.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://patch-diff.githubusercontent.com/login?return_to=%2Fpython%2Fcpython
Code https://patch-diff.githubusercontent.com/python/cpython
Issues 5k+ https://patch-diff.githubusercontent.com/python/cpython/issues
Pull requests 2.1k https://patch-diff.githubusercontent.com/python/cpython/pulls
Actions https://patch-diff.githubusercontent.com/python/cpython/actions
Projects 31 https://patch-diff.githubusercontent.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://patch-diff.githubusercontent.com/python/cpython/security
Please reload this pagehttps://patch-diff.githubusercontent.com/python/cpython/issues/123954
Insights https://patch-diff.githubusercontent.com/python/cpython/pulse
Code https://patch-diff.githubusercontent.com/python/cpython
Issues https://patch-diff.githubusercontent.com/python/cpython/issues
Pull requests https://patch-diff.githubusercontent.com/python/cpython/pulls
Actions https://patch-diff.githubusercontent.com/python/cpython/actions
Projects https://patch-diff.githubusercontent.com/python/cpython/projects
Security https://patch-diff.githubusercontent.com/python/cpython/security
Insights https://patch-diff.githubusercontent.com/python/cpython/pulse
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/python/cpython/issues/123954
New issuehttps://patch-diff.githubusercontent.com/login?return_to=https://github.com/python/cpython/issues/123954
Inefficient ssl.SSLWantReadError exception slows down very common use-casehttps://patch-diff.githubusercontent.com/python/cpython/issues/123954#top
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-SSLhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-SSL%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/tarasko
https://github.com/tarasko
taraskohttps://github.com/tarasko
on Sep 11, 2024https://github.com/python/cpython/issues/123954#issue-2519904330
gh-123954: proposal for improving logic for setting SSL exceptions #128391https://github.com/python/cpython/pull/128391
extension-modulesC modules in the Modules dirhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22extension-modules%22
performancePerformance or resource usagehttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22performance%22
topic-SSLhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-SSL%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.