From b186f1df41da42d774ac9278588acc2bd11a59a8 Mon Sep 17 00:00:00 2001 From: R David Murray Date: Sat, 4 Oct 2014 17:43:54 -0400 Subject: [PATCH] #11866: Eliminate race condition in the computation of names for new threads. Original patch by Peter Saveliev. --- Lib/threading.py | 9 ++++----- Misc/NEWS | 3 +++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index 7a5a4408ce..37aa3b8ddc 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -9,7 +9,7 @@ except ImportError: from time import time as _time from traceback import format_exc as _format_exc from _weakrefset import WeakSet -from itertools import islice as _islice +from itertools import islice as _islice, count as _count try: from _collections import deque as _deque except ImportError: @@ -726,11 +726,10 @@ class BrokenBarrierError(RuntimeError): # Helper to generate new thread names -_counter = 0 +_counter = _count().__next__ +_counter() # Consume 0 so first non-main thread has id 1. def _newname(template="Thread-%d"): - global _counter - _counter += 1 - return template % _counter + return template % _counter() # Active thread administration _active_limbo_lock = _allocate_lock() diff --git a/Misc/NEWS b/Misc/NEWS index 6112a008e9..d0593c24f9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,9 @@ Core and Builtins Library ------- +- Issue #11866: Eliminated race condition in the computation of names + for new threads. + - Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules is mutated while iterating. Patch by Olivier Grisel. -- 2.40.0