[Python-checkins] cpython (2.7): #11866: Eliminate race condition in the computation of names for new threads.

r.david.murray python-checkins at python.org
Sat Oct 4 23:47:06 CEST 2014


https://hg.python.org/cpython/rev/a6b4add67168
changeset:   92812:a6b4add67168
branch:      2.7
parent:      92798:a3e18dd8f267
user:        R David Murray <rdmurray at bitdance.com>
date:        Sat Oct 04 17:40:43 2014 -0400
summary:
  #11866: Eliminate race condition in the computation of names for new threads.

Original patch by Peter Saveliev.

files:
  Lib/threading.py |  8 ++++----
  Misc/NEWS        |  3 +++
  2 files changed, 7 insertions(+), 4 deletions(-)


diff --git a/Lib/threading.py b/Lib/threading.py
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -11,6 +11,7 @@
 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 @@
             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()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list