]> granicus.if.org Git - python/commitdiff
#11866: Eliminate race condition in the computation of names for new threads.
authorR David Murray <rdmurray@bitdance.com>
Sat, 4 Oct 2014 21:40:43 +0000 (17:40 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 4 Oct 2014 21:40:43 +0000 (17:40 -0400)
Original patch by Peter Saveliev.

Lib/threading.py
Misc/NEWS

index 0438e1f5209de9d0708a253d70c35935caa74fec..27a5511ddc290001d7e7c9d105df98ee68a388af 100644 (file)
@@ -11,6 +11,7 @@ except ImportError:
 import warnings
 
 from collections import deque as _deque
+from itertools import count as _count
 from time import time as _time, sleep as _sleep
 from traceback import format_exc as _format_exc
 
@@ -623,11 +624,10 @@ class _Event(_Verbose):
             self.__cond.release()
 
 # 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 = _counter + 1
-    return template % _counter
+    return template % _counter()
 
 # Active thread administration
 _active_limbo_lock = _allocate_lock()
index 6dc092e069a142e6c82b081c1b4c6708e8a21641..5144b5082d08952159c9ce29908bbaf744619254 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #11866: Eliminated race condition in the computation of names
+  for new threads.
+
 - Issue #22219: The zipfile module CLI now adds entries for directories
   (including empty directories) in ZIP file.