Skip to content

Commit 2a2e7ce

Browse files
committed
pythongh-148292: Update SSLSocket.read() for OpenSSL 4
Add _got_eof attribute to avoid calling SSL_read_ex() again after SSL_ERROR_EOF.
1 parent d14e31e commit 2a2e7ce

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎Lib/ssl.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@ def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
10371037
self.server_hostname = context._encode_hostname(server_hostname)
10381038
self.do_handshake_on_connect = do_handshake_on_connect
10391039
self.suppress_ragged_eofs = suppress_ragged_eofs
1040+
self._got_eof = False
10401041

10411042
# See if we are connected
10421043
try:
@@ -1149,12 +1150,23 @@ def read(self, len=1024, buffer=None):
11491150
self._checkClosed()
11501151
if self._sslobj is None:
11511152
raise ValueError("Read on closed or unwrapped SSL socket.")
1153+
if self._got_eof:
1154+
# gh-148292: On OpenSSL 4, calling SSL_read_ex() again after
1155+
# SSL_ERROR_EOF fails with "A failure in the SSL library occurred"
1156+
if buffer is not None:
1157+
return 0
1158+
else:
1159+
return b''
1160+
11521161
try:
11531162
if buffer is not None:
11541163
return self._sslobj.read(len, buffer)
11551164
else:
11561165
return self._sslobj.read(len)
11571166
except SSLError as x:
1167+
if x.args[0] == SSL_ERROR_EOF:
1168+
self._got_eof = True
1169+
11581170
if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
11591171
if buffer is not None:
11601172
return 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :meth:`ssl.SSLSocket.read` for OpenSSL 4: no longer call
2+
``SSL_read_ex()`` after ``SSL_ERROR_EOF``. Patch by Victor Stinner.

0 commit comments

Comments
 (0)