Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b186f1d

Browse files
committed
#11866: Eliminate race condition in the computation of names for new threads.
Original patch by Peter Saveliev.
1 parent e161849 commit b186f1d

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

Lib/threading.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from time import time as _time
1010
from traceback import format_exc as _format_exc
1111
from _weakrefset import WeakSet
12-
from itertools import islice as _islice
12+
from itertools import islice as _islice, count as _count
1313
try:
1414
from _collections import deque as _deque
1515
except ImportError:
@@ -726,11 +726,10 @@ class BrokenBarrierError(RuntimeError):
726726

727727

728728
# Helper to generate new thread names
729-
_counter = 0
729+
_counter = _count().__next__
730+
_counter() # Consume 0 so first non-main thread has id 1.
730731
def _newname(template="Thread-%d"):
731-
global _counter
732-
_counter += 1
733-
return template % _counter
732+
return template % _counter()
734733

735734
# Active thread administration
736735
_active_limbo_lock = _allocate_lock()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Core and Builtins
2222
Library
2323
-------
2424

25+
- Issue #11866: Eliminated race condition in the computation of names
26+
for new threads.
27+
2528
- Issue #21905: Avoid RuntimeError in pickle.whichmodule() when sys.modules
2629
is mutated while iterating. Patch by Olivier Grisel.
2730

0 commit comments

Comments
 (0)