This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio sock_recv blocks on ssl sockets.
Type: behavior Stage: resolved
Components: asyncio, SSL Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: christian.heimes Nosy List: AkshayTakkar, Ido Michael, asvetlov, christian.heimes, miss-islington, yselivanov
Priority: normal Keywords: easy, newcomer friendly, patch

Created on 2019-06-25 19:36 by AkshayTakkar, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
server_client AkshayTakkar, 2019-06-25 19:36 Server/Client Code to replicate the issue. Note: You'll have to create the certificate.pem and key.pem files
Pull Requests
URL Status Linked Edit
PR 14457 closed AkshayTakkar, 2019-06-28 18:01
PR 16457 merged Ido Michael, 2019-09-28 10:29
PR 17495 merged miss-islington, 2019-12-07 11:52
PR 17496 merged asvetlov, 2019-12-07 11:54
PR 17526 closed christian.heimes, 2019-12-09 13:51
Messages (11)
msg346550 - (view) Author: Akshay Takkar (AkshayTakkar) * Date: 2019-06-25 19:36
I'm working on a server that uses sock_recv from asyncio.
await sock_recv(socket, n) never returns if socket is wrapped in ssl using the wrap_socket function from the ssl module.
I think the problem stated in this stackoverflow question could be the reason: https://stackoverflow.com/questions/40346619/behavior-of-pythons-select-with-partial-recv-on-ssl-socket
msg346552 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2019-06-25 19:42
You can't use sock_recv() with a wrapped SSL socket. A SSLSocket behaves differently because there is user-level buffering. The connection operates on TLS frames and only hands off data after it has been decrypted and verified. asyncio uses wrap_bio().
msg346597 - (view) Author: Akshay Takkar (AkshayTakkar) * Date: 2019-06-26 10:20
Can you please elaborate on how to get around this issue?
msg346598 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2019-06-26 10:35
Use asyncio transports or streams.
sock_recv() is a low-level API with a very limited audience.

wrapped ssl socket provides a synchronous interface only.

Perhaps we should raise an exception if SSLSocket is passed into asyncio functions.
msg346599 - (view) Author: Akshay Takkar (AkshayTakkar) * Date: 2019-06-26 10:48
Ah, I see. Yes, raising an exception would probably be very useful. Thanks for the info.
msg353444 - (view) Author: Ido Michael (Ido Michael) * Date: 2019-09-28 10:34
Fixed this issue since the CLA of the past user wasn't signed.

Added a check for the socket type in each of the methods: 
sock_recv, sock_recv_into, sock_sendall, sock_connect, sock_accept.

PR: GH-16457
msg357971 - (view) Author: miss-islington (miss-islington) Date: 2019-12-07 11:52
New changeset 892f9e0777f262d366d4747a54c33a1c15a49da6 by Miss Islington (bot) (idomic) in branch 'master':
 bpo-37404: Raising value error if an SSLSocket is passed to asyncio functions (GH-16457)
https://github.com/python/cpython/commit/892f9e0777f262d366d4747a54c33a1c15a49da6
msg357972 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2019-12-07 12:44
New changeset 930cef2770b641f49e69b67840daaa53b65cd0e0 by Andrew Svetlov in branch '3.8':
[3.8] bpo-37404: Raising value error if an SSLSocket is passed to asyncio functions (GH-16457) (#17496)
https://github.com/python/cpython/commit/930cef2770b641f49e69b67840daaa53b65cd0e0
msg357973 - (view) Author: miss-islington (miss-islington) Date: 2019-12-07 12:45
New changeset a85066df9423d381e6b233469b00db55563a9f80 by Miss Islington (bot) in branch '3.7':
bpo-37404: Raising value error if an SSLSocket is passed to asyncio functions (GH-16457)
https://github.com/python/cpython/commit/a85066df9423d381e6b233469b00db55563a9f80
msg358086 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2019-12-09 13:52
The patch broke Python builds without ssl support and caused a minor performance regression in hot paths. I changed the code to only perform the check in debug mode.
msg361236 - (view) Author: Ido Michael (Ido Michael) * Date: 2020-02-02 14:21
Sorry it broke the version, what could I have done to avoid this?
Also can we close this issue?
History
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81585
2020-02-03 10:16:45asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-02-02 14:21:34Ido Michaelsetmessages: + msg361236
2019-12-09 13:52:06christian.heimessetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg358086

stage: resolved -> patch review
2019-12-09 13:51:04christian.heimessetpull_requests: + pull_request17004
2019-12-07 12:45:27asvetlovsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.8, Python 3.9
2019-12-07 12:45:10miss-islingtonsetmessages: + msg357973
2019-12-07 12:44:24asvetlovsetmessages: + msg357972
2019-12-07 11:54:04asvetlovsetpull_requests: + pull_request16974
2019-12-07 11:52:50miss-islingtonsetpull_requests: + pull_request16973
2019-12-07 11:52:39miss-islingtonsetnosy: + miss-islington
messages: + msg357971
2019-09-28 10:34:40Ido Michaelsetnosy: + Ido Michael
messages: + msg353444
2019-09-28 10:29:53Ido Michaelsetpull_requests: + pull_request16037
2019-09-13 12:26:00asvetlovsetkeywords: + easy, newcomer friendly
2019-06-28 18:01:56AkshayTakkarsetkeywords: + patch
stage: patch review
pull_requests: + pull_request14274
2019-06-26 10:48:50AkshayTakkarsetmessages: + msg346599
2019-06-26 10:35:47asvetlovsetmessages: + msg346598
2019-06-26 10:20:58AkshayTakkarsetmessages: + msg346597
2019-06-25 19:42:40christian.heimessetmessages: + msg346552
2019-06-25 19:36:21AkshayTakkarcreate