Skip to content
7 changes: 4 additions & 3 deletions Doc/library/asyncio-queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ Queue
raise :exc:`QueueShutDown`.

If *immediate* is true, the queue is terminated immediately.
The queue is drained to be completely empty. All callers of
:meth:`~Queue.join` are unblocked regardless of the number
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
The queue is drained to be completely empty and the count
of unfinished tasks is reduced by the number of tasks drained.
If unfinished tasks is zero, callers of :meth:`~Queue.join`
are unblocked. Also, blocked callers of :meth:`~Queue.get`
are unblocked and will raise :exc:`QueueShutDown` because the
queue is empty.

Expand Down
2 changes: 1 addition & 1 deletion Doc/library/concurrent.interpreters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ makes them similar to processes, but they still enjoy in-process
efficiency, like threads.

All that said, interpreters do naturally support certain flavors of
concurrency, as a powerful side effect of that isolation.
concurrency.
There's a powerful side effect of that isolation. It enables a
different approach to concurrency than you can take with async or
threads. It's a similar concurrency model to CSP or the actor model,
Expand Down
7 changes: 4 additions & 3 deletions Doc/library/queue.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ until empty or terminated immediately with a hard shutdown.
raise :exc:`ShutDown`.

If *immediate* is true, the queue is terminated immediately.
The queue is drained to be completely empty. All callers of
:meth:`~Queue.join` are unblocked regardless of the number
of unfinished tasks. Blocked callers of :meth:`~Queue.get`
The queue is drained to be completely empty and the count
of unfinished tasks is reduced by the number of tasks drained.
If unfinished tasks is zero, callers of :meth:`~Queue.join`
are unblocked. Also, blocked callers of :meth:`~Queue.get`
are unblocked and will raise :exc:`ShutDown` because the
queue is empty.

Expand Down
11 changes: 11 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ difflib
(Contributed by Jiahao Li in :gh:`134580`.)


hashlib
-------

* Ensure that hash functions guaranteed to be always *available* exist as
attributes of :mod:`hashlib` even if they will not work at runtime due to
missing backend implementations. For instance, ``hashlib.md5`` will no
longer raise :exc:`AttributeError` if OpenSSL is not available and Python
has been built without MD5 support.
(Contributed by Bénédikt Tran in :gh:`136929`.)


http.client
-----------

Expand Down
8 changes: 5 additions & 3 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@ def shutdown(self, immediate=False):
By default, gets will only raise once the queue is empty. Set
'immediate' to True to make gets raise immediately instead.

All blocked callers of put() and get() will be unblocked. If
'immediate', unblock callers of join() regardless of the
number of unfinished tasks.
All blocked callers of put() and get() will be unblocked.

If 'immediate', the queue is drained and unfinished tasks
is reduced by the number of drained tasks. If unfinished tasks
is reduced to zero, callers of Queue.join are unblocked.
"""
self._is_shutdown = True
if immediate:
Expand Down
31 changes: 27 additions & 4 deletions Lib/hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,39 @@ def file_digest(fileobj, digest, /, *, _bufsize=2**18):
return digestobj


__logging = None
for __func_name in __always_supported:
# try them all, some may not work due to the OpenSSL
# version not supporting that algorithm.
try:
globals()[__func_name] = __get_hash(__func_name)
except ValueError:
import logging
logging.exception('code for hash %s was not found.', __func_name)

except ValueError as __exc:
import logging as __logging
__logging.error('hash algorithm %s will not be supported at runtime '
'[reason: %s]', __func_name, __exc)
# The following code can be simplified in Python 3.19
# once "string" is removed from the signature.
__code = f'''\
def {__func_name}(data=__UNSET, *, usedforsecurity=True, string=__UNSET):
if data is __UNSET and string is not __UNSET:
import warnings
warnings.warn(
"the 'string' keyword parameter is deprecated since "
"Python 3.15 and slated for removal in Python 3.19; "
"use the 'data' keyword parameter or pass the data "
"to hash as a positional argument instead",
DeprecationWarning, stacklevel=2)
if data is not __UNSET and string is not __UNSET:
raise TypeError("'data' and 'string' are mutually exclusive "
"and support for 'string' keyword parameter "
"is slated for removal in a future version.")
raise ValueError("unsupported hash algorithm {__func_name}")
'''
exec(__code, {"__UNSET": object()}, __locals := {})
globals()[__func_name] = __locals[__func_name]
del __exc, __code, __locals

# Cleanup locals()
del __always_supported, __func_name, __get_hash
del __py_new, __hash_new, __get_openssl_constructor
del __logging
8 changes: 5 additions & 3 deletions Lib/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ def shutdown(self, immediate=False):
By default, gets will only raise once the queue is empty. Set
'immediate' to True to make gets raise immediately instead.

All blocked callers of put() and get() will be unblocked. If
'immediate', callers of join() are unblocked regardless of
the number of unfinished tasks.
All blocked callers of put() and get() will be unblocked.

If 'immediate', the queue is drained and unfinished tasks
is reduced by the number of drained tasks. If unfinished tasks
is reduced to zero, callers of Queue.join are unblocked.
'''
with self.mutex:
self.is_shutdown = True
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,25 @@ def getpass(self):
# Make sure the password function isn't called if it isn't needed
ctx.load_cert_chain(CERTFILE, password=getpass_exception)

@threading_helper.requires_working_threading()
def test_load_cert_chain_thread_safety(self):
# gh-134698: _ssl detaches the thread state (and as such,
# releases the GIL and critical sections) around expensive
# OpenSSL calls. Unfortunately, OpenSSL structures aren't
# thread-safe, so executing these calls concurrently led
# to crashes.
ctx = ssl.create_default_context()

def race():
ctx.load_cert_chain(CERTFILE)

threads = [threading.Thread(target=race) for _ in range(8)]
with threading_helper.catch_threading_exception() as cm:
with threading_helper.start_threads(threads):
pass

self.assertIsNone(cm.exc_value)

def test_load_verify_locations(self):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_verify_locations(CERTFILE)
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,23 @@ def modify_file():
t.start()
t.join()

def test_dummy_thread_on_interpreter_shutdown(self):
# GH-130522: When `threading` held a reference to itself and then a
# _DummyThread() object was created, destruction of the dummy thread
# would emit an unraisable exception at shutdown, due to a lock being
# destroyed.
code = """if True:
import sys
import threading

threading.x = sys.modules[__name__]
x = threading._DummyThread()
"""
rc, out, err = assert_python_ok("-c", code)
self.assertEqual(rc, 0)
self.assertEqual(out, b"")
self.assertEqual(err, b"")


class ThreadRunFail(threading.Thread):
def run(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ def __init__(self, dummy_thread):
# the related _DummyThread will be kept forever!
_thread_local_info._track_dummy_thread_ref = self

def __del__(self):
def __del__(self, _active_limbo_lock=_active_limbo_lock, _active=_active):
with _active_limbo_lock:
if _active.get(self._tident) is self._dummy_thread:
_active.pop(self._tident, None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when calling methods of :class:`ssl.SSLContext` or
:class:`ssl.SSLSocket` across multiple threads.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ensure that hash functions guaranteed to be always *available* exist as
attributes of :mod:`hashlib` even if they will not work at runtime due to
missing backend implementations. For instance, ``hashlib.md5`` will no
longer raise :exc:`AttributeError` if OpenSSL is not available and Python
has been built without MD5 support. Patch by Bénédikt Tran.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix unraisable :exc:`TypeError` raised during :term:`interpreter shutdown`
in the :mod:`threading` module.
Loading
Loading