Debian Codegraph @

Lib/test/test_threading.py

raw
1 """
2 Tests for the threading module.
3 """
4
5 import test.support
6 from test.support import threading_helper, requires_subprocess, requires_gil_enabled
7 from test.support import verbose, cpython_only, os_helper
8 from test.support.import_helper import ensure_lazy_imports, import_module
9 from test.support.script_helper import assert_python_ok, assert_python_failure
10 from test.support import force_not_colorized
11
12 import random
13 import sys
14 import _thread
15 import threading
16 import time
17 import unittest
18 import weakref
19 import os
20 import subprocess
21 import signal
22 import textwrap
23 import traceback
24 import warnings
25
26 from unittest import mock
27 from test import lock_tests
28 from test import support
29
30 try:
31 from concurrent import interpreters
32 except ImportError:
33 interpreters = None
34
35 threading_helper.requires_working_threading(module=True)
36
37 # Between fork() and exec(), only async-safe functions are allowed (issues
38 # #12316 and #11870), and fork() from a worker thread is known to trigger
39 # problems with some operating systems (issue #3863): skip problematic tests
40 # on platforms known to behave badly.
41 platforms_to_skip = ('netbsd5', 'hp-ux11')
42
43
44 def skip_unless_reliable_fork(test):
45 if not support.has_fork_support:
46 return unittest.skip("requires working os.fork()")(test)
47 if sys.platform in platforms_to_skip:
48 return unittest.skip("due to known OS bug related to thread+fork")(test)
49 if support.HAVE_ASAN_FORK_BUG:
50 return unittest.skip("libasan has a pthread_create() dead lock related to thread+fork")(test)
51 if support.check_sanitizer(thread=True):
52 return unittest.skip("TSAN doesn't support threads after fork")(test)
53 return test
54
55
56 def requires_subinterpreters(meth):
57 """Decorator to skip a test if subinterpreters are not supported."""
58 return unittest.skipIf(interpreters is None,
59 'subinterpreters required')(meth)
60
61
62 def restore_default_excepthook(testcase):
63 testcase.addCleanup(setattr, threading, 'excepthook', threading.excepthook)
64 threading.excepthook = threading.__excepthook__
65
66
67 # A trivial mutable counter.
68 class Counter(object):
69 def __init__(self):
70 self.value = 0
71 def inc(self):
72 self.value += 1
73 def dec(self):
74 self.value -= 1
75 def get(self):
76 return self.value
77
78 class TestThread(threading.Thread):
79 def __init__(self, name, testcase, sema, mutex, nrunning):
80 threading.Thread.__init__(self, name=name)
81 self.testcase = testcase
82 self.sema = sema
83 self.mutex = mutex
84 self.nrunning = nrunning
85
86 def run(self):
87 delay = random.random() / 10000.0
88 if verbose:
89 print('task %s will run for %.1f usec' %
90 (self.name, delay * 1e6))
91
92 with self.sema:
93 with self.mutex:
94 self.nrunning.inc()
95 if verbose:
96 print(self.nrunning.get(), 'tasks are running')
97 self.testcase.assertLessEqual(self.nrunning.get(), 3)
98
99 time.sleep(delay)
100 if verbose:
101 print('task', self.name, 'done')
102
103 with self.mutex:
104 self.nrunning.dec()
105 self.testcase.assertGreaterEqual(self.nrunning.get(), 0)
106 if verbose:
107 print('%s is finished. %d tasks are running' %
108 (self.name, self.nrunning.get()))
109
110
111 class BaseTestCase(unittest.TestCase):
112 def setUp(self):
113 self._threads = threading_helper.threading_setup()
114
115 def tearDown(self):
116 threading_helper.threading_cleanup(*self._threads)
117 test.support.reap_children()
118
119
120 class ThreadTests(BaseTestCase):
121 maxDiff = 9999
122
123 @cpython_only
124 def test_lazy_import(self):
125 ensure_lazy_imports("threading", {"functools", "warnings"})
126
127 @cpython_only
128 def test_name(self):
129 def func(): pass
130
131 thread = threading.Thread(name="myname1")
132 self.assertEqual(thread.name, "myname1")
133
134 # Convert int name to str
135 thread = threading.Thread(name=123)
136 self.assertEqual(thread.name, "123")
137
138 # target name is ignored if name is specified
139 thread = threading.Thread(target=func, name="myname2")
140 self.assertEqual(thread.name, "myname2")
141
142 with mock.patch.object(threading, '_counter', return_value=2):
143 thread = threading.Thread(name="")
144 self.assertEqual(thread.name, "Thread-2")
145
146 with mock.patch.object(threading, '_counter', return_value=3):
147 thread = threading.Thread()
148 self.assertEqual(thread.name, "Thread-3")
149
150 with mock.patch.object(threading, '_counter', return_value=5):
151 thread = threading.Thread(target=func)
152 self.assertEqual(thread.name, "Thread-5 (func)")
153
154 def test_args_argument(self):
155 # bpo-45735: Using list or tuple as *args* in constructor could
156 # achieve the same effect.
157 num_list = [1]
158 num_tuple = (1,)
159
160 str_list = ["str"]
161 str_tuple = ("str",)
162
163 list_in_tuple = ([1],)
164 tuple_in_list = [(1,)]
165
166 test_cases = (
167 (num_list, lambda arg: self.assertEqual(arg, 1)),
168 (num_tuple, lambda arg: self.assertEqual(arg, 1)),
169 (str_list, lambda arg: self.assertEqual(arg, "str")),
170 (str_tuple, lambda arg: self.assertEqual(arg, "str")),
171 (list_in_tuple, lambda arg: self.assertEqual(arg, [1])),
172 (tuple_in_list, lambda arg: self.assertEqual(arg, (1,)))
173 )
174
175 for args, target in test_cases:
176 with self.subTest(target=target, args=args):
177 t = threading.Thread(target=target, args=args)
178 t.start()
179 t.join()
180
181 def test_lock_no_args(self):
182 threading.Lock() # works
183 self.assertRaises(TypeError, threading.Lock, 1)
184 self.assertRaises(TypeError, threading.Lock, a=1)
185 self.assertRaises(TypeError, threading.Lock, 1, 2, a=1, b=2)
186
187 def test_lock_no_subclass(self):
188 # Intentionally disallow subclasses of threading.Lock because they have
189 # never been allowed, so why start now just because the type is public?
190 with self.assertRaises(TypeError):
191 class MyLock(threading.Lock): pass
192
193 def test_lock_or_none(self):
194 import types
195 self.assertIsInstance(threading.Lock | None, types.UnionType)
196
197 # Create a bunch of threads, let each do some work, wait until all are
198 # done.
199 def test_various_ops(self):
200 # This takes about n/3 seconds to run (about n/3 clumps of tasks,
201 # times about 1 second per clump).
202 NUMTASKS = 10
203
204 # no more than 3 of the 10 can run at once
205 sema = threading.BoundedSemaphore(value=3)
206 mutex = threading.RLock()
207 numrunning = Counter()
208
209 threads = []
210
211 for i in range(NUMTASKS):
212 t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
213 threads.append(t)
214 self.assertIsNone(t.ident)
215 self.assertRegex(repr(t), r'^<TestThread\(.*, initial\)>$')
216 t.start()
217
218 if hasattr(threading, 'get_native_id'):
219 native_ids = set(t.native_id for t in threads) | {threading.get_native_id()}
220 self.assertNotIn(None, native_ids)
221 self.assertEqual(len(native_ids), NUMTASKS + 1)
222
223 if verbose:
224 print('waiting for all tasks to complete')
225 for t in threads:
226 t.join()
227 self.assertFalse(t.is_alive())
228 self.assertNotEqual(t.ident, 0)
229 self.assertIsNotNone(t.ident)
230 self.assertRegex(repr(t), r'^<TestThread\(.*, stopped -?\d+\)>$')
231 if verbose:
232 print('all tasks done')
233 self.assertEqual(numrunning.get(), 0)
234
235 def test_ident_of_no_threading_threads(self):
236 # The ident still must work for the main thread and dummy threads.
237 self.assertIsNotNone(threading.current_thread().ident)
238 def f():
239 ident.append(threading.current_thread().ident)
240 done.set()
241 done = threading.Event()
242 ident = []
243 with threading_helper.wait_threads_exit():
244 tid = _thread.start_new_thread(f, ())
245 done.wait()
246 self.assertEqual(ident[0], tid)
247
248 # run with a small(ish) thread stack size (256 KiB)
249 def test_various_ops_small_stack(self):
250 if verbose:
251 print('with 256 KiB thread stack size...')
252 try:
253 threading.stack_size(262144)
254 except _thread.error:
255 raise unittest.SkipTest(
256 'platform does not support changing thread stack size')
257 self.test_various_ops()
258 threading.stack_size(0)
259
260 # run with a large thread stack size (1 MiB)
261 def test_various_ops_large_stack(self):
262 if verbose:
263 print('with 1 MiB thread stack size...')
264 try:
265 threading.stack_size(0x100000)
266 except _thread.error:
267 raise unittest.SkipTest(
268 'platform does not support changing thread stack size')
269 self.test_various_ops()
270 threading.stack_size(0)
271
272 def test_foreign_thread(self):
273 # Check that a "foreign" thread can use the threading module.
274 dummy_thread = None
275 error = None
276 def f(mutex):
277 try:
278 nonlocal dummy_thread
279 nonlocal error
280 # Calling current_thread() forces an entry for the foreign
281 # thread to get made in the threading._active map.
282 dummy_thread = threading.current_thread()
283 tid = dummy_thread.ident
284 self.assertIn(tid, threading._active)
285 self.assertIsInstance(dummy_thread, threading._DummyThread)
286 self.assertIs(threading._active.get(tid), dummy_thread)
287 # gh-29376
288 self.assertTrue(
289 dummy_thread.is_alive(),
290 'Expected _DummyThread to be considered alive.'
291 )
292 self.assertIn('_DummyThread', repr(dummy_thread))
293 except BaseException as e:
294 error = e
295 finally:
296 mutex.release()
297
298 mutex = threading.Lock()
299 mutex.acquire()
300 with threading_helper.wait_threads_exit():
301 tid = _thread.start_new_thread(f, (mutex,))
302 # Wait for the thread to finish.
303 mutex.acquire()
304 if error is not None:
305 raise error
306 self.assertEqual(tid, dummy_thread.ident)
307 # Issue gh-106236:
308 with self.assertRaises(RuntimeError):
309 dummy_thread.join()
310 dummy_thread._started.clear()
311 with self.assertRaises(RuntimeError):
312 dummy_thread.is_alive()
313 # Busy wait for the following condition: after the thread dies, the
314 # related dummy thread must be removed from threading._active.
315 timeout = 5
316 timeout_at = time.monotonic() + timeout
317 while time.monotonic() < timeout_at:
318 if threading._active.get(dummy_thread.ident) is not dummy_thread:
319 break
320 time.sleep(.1)
321 else:
322 self.fail('It was expected that the created threading._DummyThread was removed from threading._active.')
323
324 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
325 # exposed at the Python level. This test relies on ctypes to get at it.
326 def test_PyThreadState_SetAsyncExc(self):
327 ctypes = import_module("ctypes")
328
329 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
330 set_async_exc.argtypes = (ctypes.c_ulong, ctypes.py_object)
331
332 class AsyncExc(Exception):
333 pass
334
335 exception = ctypes.py_object(AsyncExc)
336
337 # First check it works when setting the exception from the same thread.
338 tid = threading.get_ident()
339 self.assertIsInstance(tid, int)
340 self.assertGreater(tid, 0)
341
342 try:
343 result = set_async_exc(tid, exception)
344 # The exception is async, so we might have to keep the VM busy until
345 # it notices.
346 while True:
347 pass
348 except AsyncExc:
349 pass
350 else:
351 # This code is unreachable but it reflects the intent. If we wanted
352 # to be smarter the above loop wouldn't be infinite.
353 self.fail("AsyncExc not raised")
354 try:
355 self.assertEqual(result, 1) # one thread state modified
356 except UnboundLocalError:
357 # The exception was raised too quickly for us to get the result.
358 pass
359
360 # `worker_started` is set by the thread when it's inside a try/except
361 # block waiting to catch the asynchronously set AsyncExc exception.
362 # `worker_saw_exception` is set by the thread upon catching that
363 # exception.
364 worker_started = threading.Event()
365 worker_saw_exception = threading.Event()
366
367 class Worker(threading.Thread):
368 def run(self):
369 self.id = threading.get_ident()
370 self.finished = False
371
372 try:
373 while True:
374 worker_started.set()
375 time.sleep(0.1)
376 except AsyncExc:
377 self.finished = True
378 worker_saw_exception.set()
379
380 t = Worker()
381 t.daemon = True # so if this fails, we don't hang Python at shutdown
382 t.start()
383 if verbose:
384 print(" started worker thread")
385
386 # Try a thread id that doesn't make sense.
387 if verbose:
388 print(" trying nonsensical thread id")
389 result = set_async_exc(-1, exception)
390 self.assertEqual(result, 0) # no thread states modified
391
392 # Now raise an exception in the worker thread.
393 if verbose:
394 print(" waiting for worker thread to get started")
395 ret = worker_started.wait()
396 self.assertTrue(ret)
397 if verbose:
398 print(" verifying worker hasn't exited")
399 self.assertFalse(t.finished)
400 if verbose:
401 print(" attempting to raise asynch exception in worker")
402 result = set_async_exc(t.id, exception)
403 self.assertEqual(result, 1) # one thread state modified
404 if verbose:
405 print(" waiting for worker to say it caught the exception")
406 worker_saw_exception.wait(timeout=support.SHORT_TIMEOUT)
407 self.assertTrue(t.finished)
408 if verbose:
409 print(" all OK -- joining worker")
410 if t.finished:
411 t.join()
412 # else the thread is still running, and we have no way to kill it
413
414 def test_limbo_cleanup(self):
415 # Issue 7481: Failure to start thread should cleanup the limbo map.
416 def fail_new_thread(*args, **kwargs):
417 raise threading.ThreadError()
418 _start_joinable_thread = threading._start_joinable_thread
419 threading._start_joinable_thread = fail_new_thread
420 try:
421 t = threading.Thread(target=lambda: None)
422 self.assertRaises(threading.ThreadError, t.start)
423 self.assertFalse(
424 t in threading._limbo,
425 "Failed to cleanup _limbo map on failure of Thread.start().")
426 finally:
427 threading._start_joinable_thread = _start_joinable_thread
428
429 def test_finalize_running_thread(self):
430 # Issue 1402: the PyGILState_Ensure / _Release functions may be called
431 # very late on python exit: on deallocation of a running thread for
432 # example.
433 if support.check_sanitizer(thread=True):
434 # the thread running `time.sleep(100)` below will still be alive
435 # at process exit
436 self.skipTest("TSAN would report thread leak")
437 import_module("ctypes")
438
439 rc, out, err = assert_python_failure("-c", """if 1:
440 import ctypes, sys, time, _thread
441
442 # This lock is used as a simple event variable.
443 ready = _thread.allocate_lock()
444 ready.acquire()
445
446 # Module globals are cleared before __del__ is run
447 # So we save the functions in class dict
448 class C:
449 ensure = ctypes.pythonapi.PyGILState_Ensure
450 release = ctypes.pythonapi.PyGILState_Release
451 def __del__(self):
452 state = self.ensure()
453 self.release(state)
454
455 def waitingThread():
456 x = C()
457 ready.release()
458 time.sleep(100)
459
460 _thread.start_new_thread(waitingThread, ())
461 ready.acquire() # Be sure the other thread is waiting.
462 sys.exit(42)
463 """)
464 self.assertEqual(rc, 42)
465
466 def test_finalize_with_trace(self):
467 # Issue1733757
468 # Avoid a deadlock when sys.settrace steps into threading._shutdown
469 if support.check_sanitizer(thread=True):
470 # the thread running `time.sleep(2)` below will still be alive
471 # at process exit
472 self.skipTest("TSAN would report thread leak")
473
474 assert_python_ok("-c", """if 1:
475 import sys, threading
476
477 # A deadlock-killer, to prevent the
478 # testsuite to hang forever
479 def killer():
480 import os, time
481 time.sleep(2)
482 print('program blocked; aborting')
483 os._exit(2)
484 t = threading.Thread(target=killer)
485 t.daemon = True
486 t.start()
487
488 # This is the trace function
489 def func(frame, event, arg):
490 threading.current_thread()
491 return func
492
493 sys.settrace(func)
494 """)
495
496 def test_join_nondaemon_on_shutdown(self):
497 # Issue 1722344
498 # Raising SystemExit skipped threading._shutdown
499 rc, out, err = assert_python_ok("-c", """if 1:
500 import threading
501 from time import sleep
502
503 def child():
504 sleep(1)
505 # As a non-daemon thread we SHOULD wake up and nothing
506 # should be torn down yet
507 print("Woke up, sleep function is:", sleep)
508
509 threading.Thread(target=child).start()
510 raise SystemExit
511 """)
512 self.assertEqual(out.strip(),
513 b"Woke up, sleep function is: <built-in function sleep>")
514 self.assertEqual(err, b"")
515
516 def test_enumerate_after_join(self):
517 # Try hard to trigger #1703448: a thread is still returned in
518 # threading.enumerate() after it has been join()ed.
519 enum = threading.enumerate
520 old_interval = sys.getswitchinterval()
521 try:
522 for i in range(1, 100):
523 support.setswitchinterval(i * 0.0002)
524 t = threading.Thread(target=lambda: None)
525 t.start()
526 t.join()
527 l = enum()
528 self.assertNotIn(t, l,
529 "#1703448 triggered after %d trials: %s" % (i, l))
530 finally:
531 sys.setswitchinterval(old_interval)
532
533 @support.bigmemtest(size=20, memuse=72*2**20, dry_run=False)
534 def test_join_from_multiple_threads(self, size):
535 # Thread.join() should be thread-safe
536 errors = []
537
538 def worker():
539 time.sleep(0.005)
540
541 def joiner(thread):
542 try:
543 thread.join()
544 except Exception as e:
545 errors.append(e)
546
547 for N in range(2, 20):
548 threads = [threading.Thread(target=worker)]
549 for i in range(N):
550 threads.append(threading.Thread(target=joiner,
551 args=(threads[0],)))
552 for t in threads:
553 t.start()
554 time.sleep(0.01)
555 for t in threads:
556 t.join()
557 if errors:
558 raise errors[0]
559
560 def test_join_with_timeout(self):
561 lock = _thread.allocate_lock()
562 lock.acquire()
563
564 def worker():
565 lock.acquire()
566
567 thread = threading.Thread(target=worker)
568 thread.start()
569 thread.join(timeout=0.01)
570 assert thread.is_alive()
571 lock.release()
572 thread.join()
573 assert not thread.is_alive()
574
575 def test_no_refcycle_through_target(self):
576 class RunSelfFunction(object):
577 def __init__(self, should_raise):
578 # The links in this refcycle from Thread back to self
579 # should be cleaned up when the thread completes.
580 self.should_raise = should_raise
581 self.thread = threading.Thread(target=self._run,
582 args=(self,),
583 kwargs={'yet_another':self})
584 self.thread.start()
585
586 def _run(self, other_ref, yet_another):
587 if self.should_raise:
588 raise SystemExit
589
590 restore_default_excepthook(self)
591
592 cyclic_object = RunSelfFunction(should_raise=False)
593 weak_cyclic_object = weakref.ref(cyclic_object)
594 cyclic_object.thread.join()
595 del cyclic_object
596 self.assertIsNone(weak_cyclic_object(),
597 msg=('%d references still around' %
598 sys.getrefcount(weak_cyclic_object())))
599
600 raising_cyclic_object = RunSelfFunction(should_raise=True)
601 weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
602 raising_cyclic_object.thread.join()
603 del raising_cyclic_object
604 self.assertIsNone(weak_raising_cyclic_object(),
605 msg=('%d references still around' %
606 sys.getrefcount(weak_raising_cyclic_object())))
607
608 def test_old_threading_api(self):
609 # Just a quick sanity check to make sure the old method names are
610 # still present
611 t = threading.Thread()
612 with self.assertWarnsRegex(DeprecationWarning,
613 r'get the daemon attribute'):
614 t.isDaemon()
615 with self.assertWarnsRegex(DeprecationWarning,
616 r'set the daemon attribute'):
617 t.setDaemon(True)
618 with self.assertWarnsRegex(DeprecationWarning,
619 r'get the name attribute'):
620 t.getName()
621 with self.assertWarnsRegex(DeprecationWarning,
622 r'set the name attribute'):
623 t.setName("name")
624
625 e = threading.Event()
626 with self.assertWarnsRegex(DeprecationWarning, 'use is_set()'):
627 e.isSet()
628
629 cond = threading.Condition()
630 cond.acquire()
631 with self.assertWarnsRegex(DeprecationWarning, 'use notify_all()'):
632 cond.notifyAll()
633
634 with self.assertWarnsRegex(DeprecationWarning, 'use active_count()'):
635 threading.activeCount()
636 with self.assertWarnsRegex(DeprecationWarning, 'use current_thread()'):
637 threading.currentThread()
638
639 def test_repr_daemon(self):
640 t = threading.Thread()
641 self.assertNotIn('daemon', repr(t))
642 t.daemon = True
643 self.assertIn('daemon', repr(t))
644
645 def test_daemon_param(self):
646 t = threading.Thread()
647 self.assertFalse(t.daemon)
648 t = threading.Thread(daemon=False)
649 self.assertFalse(t.daemon)
650 t = threading.Thread(daemon=True)
651 self.assertTrue(t.daemon)
652
653 @skip_unless_reliable_fork
654 def test_dummy_thread_after_fork(self):
655 # Issue #14308: a dummy thread in the active list doesn't mess up
656 # the after-fork mechanism.
657 code = """if 1:
658 import _thread, threading, os, time, warnings
659
660 def background_thread(evt):
661 # Creates and registers the _DummyThread instance
662 threading.current_thread()
663 evt.set()
664 time.sleep(10)
665
666 evt = threading.Event()
667 _thread.start_new_thread(background_thread, (evt,))
668 evt.wait()
669 assert threading.active_count() == 2, threading.active_count()
670 with warnings.catch_warnings(record=True) as ws:
671 warnings.filterwarnings(
672 "always", category=DeprecationWarning)
673 if os.fork() == 0:
674 assert threading.active_count() == 1, threading.active_count()
675 os._exit(0)
676 else:
677 assert ws[0].category == DeprecationWarning, ws[0]
678 assert 'fork' in str(ws[0].message), ws[0]
679 os.wait()
680 """
681 _, out, err = assert_python_ok("-c", code)
682 self.assertEqual(out, b'')
683 self.assertEqual(err, b'')
684
685 @skip_unless_reliable_fork
686 def test_is_alive_after_fork(self):
687 # Try hard to trigger #18418: is_alive() could sometimes be True on
688 # threads that vanished after a fork.
689 old_interval = sys.getswitchinterval()
690 self.addCleanup(sys.setswitchinterval, old_interval)
691
692 # Make the bug more likely to manifest.
693 test.support.setswitchinterval(1e-6)
694
695 for i in range(20):
696 t = threading.Thread(target=lambda: None)
697 t.start()
698 # Ignore the warning about fork with threads.
699 with warnings.catch_warnings(category=DeprecationWarning,
700 action="ignore"):
701 if (pid := os.fork()) == 0:
702 os._exit(11 if t.is_alive() else 10)
703 else:
704 t.join()
705
706 support.wait_process(pid, exitcode=10)
707
708 def test_main_thread(self):
709 main = threading.main_thread()
710 self.assertEqual(main.name, 'MainThread')
711 self.assertEqual(main.ident, threading.current_thread().ident)
712 self.assertEqual(main.ident, threading.get_ident())
713
714 def f():
715 self.assertNotEqual(threading.main_thread().ident,
716 threading.current_thread().ident)
717 th = threading.Thread(target=f)
718 th.start()
719 th.join()
720
721 @skip_unless_reliable_fork
722 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
723 def test_main_thread_after_fork(self):
724 code = """if 1:
725 import os, threading
726 from test import support
727
728 ident = threading.get_ident()
729 pid = os.fork()
730 if pid == 0:
731 print("current ident", threading.get_ident() == ident)
732 main = threading.main_thread()
733 print("main", main.name)
734 print("main ident", main.ident == ident)
735 print("current is main", threading.current_thread() is main)
736 else:
737 support.wait_process(pid, exitcode=0)
738 """
739 _, out, err = assert_python_ok("-c", code)
740 data = out.decode().replace('\r', '')
741 self.assertEqual(err, b"")
742 self.assertEqual(data,
743 "current ident True\n"
744 "main MainThread\n"
745 "main ident True\n"
746 "current is main True\n")
747
748 @skip_unless_reliable_fork
749 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
750 def test_main_thread_after_fork_from_nonmain_thread(self):
751 code = """if 1:
752 import os, threading, sys, warnings
753 from test import support
754
755 def func():
756 ident = threading.get_ident()
757 with warnings.catch_warnings(record=True) as ws:
758 warnings.filterwarnings(
759 "always", category=DeprecationWarning)
760 pid = os.fork()
761 if pid == 0:
762 print("current ident", threading.get_ident() == ident)
763 main = threading.main_thread()
764 print("main", main.name, type(main).__name__)
765 print("main ident", main.ident == ident)
766 print("current is main", threading.current_thread() is main)
767 # stdout is fully buffered because not a tty,
768 # we have to flush before exit.
769 sys.stdout.flush()
770 else:
771 assert ws[0].category == DeprecationWarning, ws[0]
772 assert 'fork' in str(ws[0].message), ws[0]
773 support.wait_process(pid, exitcode=0)
774
775 th = threading.Thread(target=func)
776 th.start()
777 th.join()
778 """
779 _, out, err = assert_python_ok("-c", code)
780 data = out.decode().replace('\r', '')
781 self.assertEqual(err.decode('utf-8'), "")
782 self.assertEqual(data,
783 "current ident True\n"
784 "main Thread-1 (func) Thread\n"
785 "main ident True\n"
786 "current is main True\n"
787 )
788
789 @skip_unless_reliable_fork
790 @unittest.skipUnless(hasattr(os, 'waitpid'), "test needs os.waitpid()")
791 def test_main_thread_after_fork_from_foreign_thread(self, create_dummy=False):
792 code = """if 1:
793 import os, threading, sys, traceback, _thread
794 from test import support
795
796 def func(lock):
797 ident = threading.get_ident()
798 if %s:
799 # call current_thread() before fork to allocate DummyThread
800 current = threading.current_thread()
801 print("current", current.name, type(current).__name__)
802 print("ident in _active", ident in threading._active)
803 # flush before fork, so child won't flush it again
804 sys.stdout.flush()
805 pid = os.fork()
806 if pid == 0:
807 print("current ident", threading.get_ident() == ident)
808 main = threading.main_thread()
809 print("main", main.name, type(main).__name__)
810 print("main ident", main.ident == ident)
811 print("current is main", threading.current_thread() is main)
812 print("_dangling", [t.name for t in list(threading._dangling)])
813 # stdout is fully buffered because not a tty,
814 # we have to flush before exit.
815 sys.stdout.flush()
816 try:
817 threading._shutdown()
818 os._exit(0)
819 except:
820 traceback.print_exc()
821 sys.stderr.flush()
822 os._exit(1)
823 else:
824 try:
825 support.wait_process(pid, exitcode=0)
826 except Exception:
827 # avoid 'could not acquire lock for
828 # <_io.BufferedWriter name='<stderr>'> at interpreter shutdown,'
829 traceback.print_exc()
830 sys.stderr.flush()
831 finally:
832 lock.release()
833
834 join_lock = _thread.allocate_lock()
835 join_lock.acquire()
836 th = _thread.start_new_thread(func, (join_lock,))
837 join_lock.acquire()
838 """ % create_dummy
839 # "DeprecationWarning: This process is multi-threaded, use of fork()
840 # may lead to deadlocks in the child"
841 _, out, err = assert_python_ok("-W", "ignore::DeprecationWarning", "-c", code)
842 data = out.decode().replace('\r', '')
843 self.assertEqual(err.decode(), "")
844 self.assertEqual(data,
845 ("current Dummy-1 _DummyThread\n" if create_dummy else "") +
846 f"ident in _active {create_dummy!s}\n" +
847 "current ident True\n"
848 "main MainThread _MainThread\n"
849 "main ident True\n"
850 "current is main True\n"
851 "_dangling ['MainThread']\n")
852
853 def test_main_thread_after_fork_from_dummy_thread(self, create_dummy=False):
854 self.test_main_thread_after_fork_from_foreign_thread(create_dummy=True)
855
856 def test_main_thread_during_shutdown(self):
857 # bpo-31516: current_thread() should still point to the main thread
858 # at shutdown
859 code = """if 1:
860 import gc, threading
861
862 main_thread = threading.current_thread()
863 assert main_thread is threading.main_thread() # sanity check
864
865 class RefCycle:
866 def __init__(self):
867 self.cycle = self
868
869 def __del__(self):
870 print("GC:",
871 threading.current_thread() is main_thread,
872 threading.main_thread() is main_thread,
873 threading.enumerate() == [main_thread])
874
875 RefCycle()
876 gc.collect() # sanity check
877 x = RefCycle()
878 """
879 _, out, err = assert_python_ok("-c", code)
880 data = out.decode()
881 self.assertEqual(err, b"")
882 self.assertEqual(data.splitlines(),
883 ["GC: True True True"] * 2)
884
885 def test_finalization_shutdown(self):
886 # bpo-36402: Py_Finalize() calls threading._shutdown() which must wait
887 # until Python thread states of all non-daemon threads get deleted.
888 #
889 # Test similar to SubinterpThreadingTests.test_threads_join_2(), but
890 # test the finalization of the main interpreter.
891 code = """if 1:
892 import os
893 import threading
894 import time
895 import random
896
897 def random_sleep():
898 seconds = random.random() * 0.010
899 time.sleep(seconds)
900
901 class Sleeper:
902 def __del__(self):
903 random_sleep()
904
905 tls = threading.local()
906
907 def f():
908 # Sleep a bit so that the thread is still running when
909 # Py_Finalize() is called.
910 random_sleep()
911 tls.x = Sleeper()
912 random_sleep()
913
914 threading.Thread(target=f).start()
915 random_sleep()
916 """
917 rc, out, err = assert_python_ok("-c", code)
918 self.assertEqual(err, b"")
919
920 def test_repr_stopped(self):
921 # Verify that "stopped" shows up in repr(Thread) appropriately.
922 started = _thread.allocate_lock()
923 finish = _thread.allocate_lock()
924 started.acquire()
925 finish.acquire()
926 def f():
927 started.release()
928 finish.acquire()
929 t = threading.Thread(target=f)
930 t.start()
931 started.acquire()
932 self.assertIn("started", repr(t))
933 finish.release()
934 # "stopped" should appear in the repr in a reasonable amount of time.
935 # Implementation detail: as of this writing, that's trivially true
936 # if .join() is called, and almost trivially true if .is_alive() is
937 # called. The detail we're testing here is that "stopped" shows up
938 # "all on its own".
939 LOOKING_FOR = "stopped"
940 for i in range(500):
941 if LOOKING_FOR in repr(t):
942 break
943 time.sleep(0.01)
944 self.assertIn(LOOKING_FOR, repr(t)) # we waited at least 5 seconds
945 t.join()
946
947 def test_BoundedSemaphore_limit(self):
948 # BoundedSemaphore should raise ValueError if released too often.
949 for limit in range(1, 10):
950 bs = threading.BoundedSemaphore(limit)
951 threads = [threading.Thread(target=bs.acquire)
952 for _ in range(limit)]
953 for t in threads:
954 t.start()
955 for t in threads:
956 t.join()
957 threads = [threading.Thread(target=bs.release)
958 for _ in range(limit)]
959 for t in threads:
960 t.start()
961 for t in threads:
962 t.join()
963 self.assertRaises(ValueError, bs.release)
964
965 @cpython_only
966 def test_frame_tstate_tracing(self):
967 _testcapi = import_module("_testcapi")
968 # Issue #14432: Crash when a generator is created in a C thread that is
969 # destroyed while the generator is still used. The issue was that a
970 # generator contains a frame, and the frame kept a reference to the
971 # Python state of the destroyed C thread. The crash occurs when a trace
972 # function is setup.
973
974 def noop_trace(frame, event, arg):
975 # no operation
976 return noop_trace
977
978 def generator():
979 while 1:
980 yield "generator"
981
982 def callback():
983 if callback.gen is None:
984 callback.gen = generator()
985 return next(callback.gen)
986 callback.gen = None
987
988 old_trace = sys.gettrace()
989 sys.settrace(noop_trace)
990 try:
991 # Install a trace function
992 threading.settrace(noop_trace)
993
994 # Create a generator in a C thread which exits after the call
995 _testcapi.call_in_temporary_c_thread(callback)
996
997 # Call the generator in a different Python thread, check that the
998 # generator didn't keep a reference to the destroyed thread state
999 for test in range(3):
1000 # The trace function is still called here
1001 callback()
1002 finally:
1003 sys.settrace(old_trace)
1004 threading.settrace(old_trace)
1005
1006 def test_gettrace(self):
1007 def noop_trace(frame, event, arg):
1008 # no operation
1009 return noop_trace
1010 old_trace = threading.gettrace()
1011 try:
1012 threading.settrace(noop_trace)
1013 trace_func = threading.gettrace()
1014 self.assertEqual(noop_trace,trace_func)
1015 finally:
1016 threading.settrace(old_trace)
1017
1018 def test_gettrace_all_threads(self):
1019 def fn(*args): pass
1020 old_trace = threading.gettrace()
1021 first_check = threading.Event()
1022 second_check = threading.Event()
1023
1024 trace_funcs = []
1025 def checker():
1026 trace_funcs.append(sys.gettrace())
1027 first_check.set()
1028 second_check.wait()
1029 trace_funcs.append(sys.gettrace())
1030
1031 try:
1032 t = threading.Thread(target=checker)
1033 t.start()
1034 first_check.wait()
1035 threading.settrace_all_threads(fn)
1036 second_check.set()
1037 t.join()
1038 self.assertEqual(trace_funcs, [None, fn])
1039 self.assertEqual(threading.gettrace(), fn)
1040 self.assertEqual(sys.gettrace(), fn)
1041 finally:
1042 threading.settrace_all_threads(old_trace)
1043
1044 self.assertEqual(threading.gettrace(), old_trace)
1045 self.assertEqual(sys.gettrace(), old_trace)
1046
1047 def test_getprofile(self):
1048 def fn(*args): pass
1049 old_profile = threading.getprofile()
1050 try:
1051 threading.setprofile(fn)
1052 self.assertEqual(fn, threading.getprofile())
1053 finally:
1054 threading.setprofile(old_profile)
1055
1056 def test_getprofile_all_threads(self):
1057 def fn(*args): pass
1058 old_profile = threading.getprofile()
1059 first_check = threading.Event()
1060 second_check = threading.Event()
1061
1062 profile_funcs = []
1063 def checker():
1064 profile_funcs.append(sys.getprofile())
1065 first_check.set()
1066 second_check.wait()
1067 profile_funcs.append(sys.getprofile())
1068
1069 try:
1070 t = threading.Thread(target=checker)
1071 t.start()
1072 first_check.wait()
1073 threading.setprofile_all_threads(fn)
1074 second_check.set()
1075 t.join()
1076 self.assertEqual(profile_funcs, [None, fn])
1077 self.assertEqual(threading.getprofile(), fn)
1078 self.assertEqual(sys.getprofile(), fn)
1079 finally:
1080 threading.setprofile_all_threads(old_profile)
1081
1082 self.assertEqual(threading.getprofile(), old_profile)
1083 self.assertEqual(sys.getprofile(), old_profile)
1084
1085 def test_locals_at_exit(self):
1086 # bpo-19466: thread locals must not be deleted before destructors
1087 # are called
1088 rc, out, err = assert_python_ok("-c", """if 1:
1089 import threading
1090
1091 class Atexit:
1092 def __del__(self):
1093 print("thread_dict.atexit = %r" % thread_dict.atexit)
1094
1095 thread_dict = threading.local()
1096 thread_dict.atexit = "value"
1097
1098 atexit = Atexit()
1099 """)
1100 self.assertEqual(out.rstrip(), b"thread_dict.atexit = 'value'")
1101
1102 def test_boolean_target(self):
1103 # bpo-41149: A thread that had a boolean value of False would not
1104 # run, regardless of whether it was callable. The correct behaviour
1105 # is for a thread to do nothing if its target is None, and to call
1106 # the target otherwise.
1107 class BooleanTarget(object):
1108 def __init__(self):
1109 self.ran = False
1110 def __bool__(self):
1111 return False
1112 def __call__(self):
1113 self.ran = True
1114
1115 target = BooleanTarget()
1116 thread = threading.Thread(target=target)
1117 thread.start()
1118 thread.join()
1119 self.assertTrue(target.ran)
1120
1121 def test_leak_without_join(self):
1122 # bpo-37788: Test that a thread which is not joined explicitly
1123 # does not leak. Test written for reference leak checks.
1124 def noop(): pass
1125 with threading_helper.wait_threads_exit():
1126 threading.Thread(target=noop).start()
1127 # Thread.join() is not called
1128
1129 def test_import_from_another_thread(self):
1130 # bpo-1596321: If the threading module is first import from a thread
1131 # different than the main thread, threading._shutdown() must handle
1132 # this case without logging an error at Python exit.
1133 code = textwrap.dedent('''
1134 import _thread
1135 import sys
1136
1137 event = _thread.allocate_lock()
1138 event.acquire()
1139
1140 def import_threading():
1141 import threading
1142 event.release()
1143
1144 if 'threading' in sys.modules:
1145 raise Exception('threading is already imported')
1146
1147 _thread.start_new_thread(import_threading, ())
1148
1149 # wait until the threading module is imported
1150 event.acquire()
1151 event.release()
1152
1153 if 'threading' not in sys.modules:
1154 raise Exception('threading is not imported')
1155
1156 # don't wait until the thread completes
1157 ''')
1158 rc, out, err = assert_python_ok("-c", code)
1159 self.assertEqual(out, b'')
1160 self.assertEqual(err, b'')
1161
1162 def test_start_new_thread_at_finalization(self):
1163 code = """if 1:
1164 import _thread
1165
1166 def f():
1167 print("shouldn't be printed")
1168
1169 class AtFinalization:
1170 def __del__(self):
1171 print("OK")
1172 _thread.start_new_thread(f, ())
1173 at_finalization = AtFinalization()
1174 """
1175 _, out, err = assert_python_ok("-c", code)
1176 self.assertEqual(out.strip(), b"OK")
1177 self.assertIn(b"can't create new thread at interpreter shutdown", err)
1178
1179 def test_join_daemon_thread_in_finalization(self):
1180 # gh-123940: Py_Finalize() prevents other threads from running Python
1181 # code, so join() can not succeed unless the thread is already done.
1182 # (Non-Python threads, that is `threading._DummyThread`, can't be
1183 # joined at all.)
1184 # We raise an exception rather than hang.
1185 for timeout in (None, 10):
1186 with self.subTest(timeout=timeout):
1187 code = textwrap.dedent(f"""
1188 import threading
1189
1190
1191 def loop():
1192 while True:
1193 pass
1194
1195
1196 class Cycle:
1197 def __init__(self):
1198 self.self_ref = self
1199 self.thr = threading.Thread(
1200 target=loop, daemon=True)
1201 self.thr.start()
1202
1203 def __del__(self):
1204 assert self.thr.is_alive()
1205 try:
1206 self.thr.join(timeout={timeout})
1207 except PythonFinalizationError:
1208 assert self.thr.is_alive()
1209 print('got the correct exception!')
1210
1211 # Cycle holds a reference to itself, which ensures it is
1212 # cleaned up during the GC that runs after daemon threads
1213 # have been forced to exit during finalization.
1214 Cycle()
1215 """)
1216 rc, out, err = assert_python_ok("-c", code)
1217 self.assertEqual(err, b"")
1218 self.assertIn(b"got the correct exception", out)
1219
1220 def test_join_finished_daemon_thread_in_finalization(self):
1221 # (see previous test)
1222 # If the thread is already finished, join() succeeds.
1223 code = textwrap.dedent("""
1224 import threading
1225 done = threading.Event()
1226
1227 def set_event():
1228 done.set()
1229
1230 class Cycle:
1231 def __init__(self):
1232 self.self_ref = self
1233 self.thr = threading.Thread(target=set_event, daemon=True)
1234 self.thr.start()
1235 self.thr.join()
1236
1237 def __del__(self):
1238 assert done.is_set()
1239 assert not self.thr.is_alive()
1240 self.thr.join()
1241 assert not self.thr.is_alive()
1242 print('all clear!')
1243
1244 Cycle()
1245 """)
1246 rc, out, err = assert_python_ok("-c", code)
1247 self.assertEqual(err, b"")
1248 self.assertIn(b"all clear", out)
1249
1250 def test_start_new_thread_failed(self):
1251 # gh-109746: if Python fails to start newly created thread
1252 # due to failure of underlying PyThread_start_new_thread() call,
1253 # its state should be removed from interpreter' thread states list
1254 # to avoid its double cleanup
1255 try:
1256 from resource import setrlimit, RLIMIT_NPROC
1257 except ImportError as err:
1258 self.skipTest(err) # RLIMIT_NPROC is specific to Linux and BSD
1259 code = """if 1:
1260 import resource
1261 import _thread
1262
1263 def f():
1264 print("shouldn't be printed")
1265
1266 limits = resource.getrlimit(resource.RLIMIT_NPROC)
1267 [_, hard] = limits
1268 resource.setrlimit(resource.RLIMIT_NPROC, (0, hard))
1269
1270 try:
1271 handle = _thread.start_joinable_thread(f)
1272 except RuntimeError:
1273 print('ok')
1274 else:
1275 print('!skip!')
1276 handle.join()
1277 """
1278 _, out, err = assert_python_ok("-u", "-c", code)
1279 out = out.strip()
1280 if b'!skip!' in out:
1281 self.skipTest('RLIMIT_NPROC had no effect; probably superuser')
1282 self.assertEqual(out, b'ok')
1283 self.assertEqual(err, b'')
1284
1285 @cpython_only
1286 def test_finalize_daemon_thread_hang(self):
1287 if support.check_sanitizer(thread=True, memory=True):
1288 # the thread running `time.sleep(100)` below will still be alive
1289 # at process exit
1290 self.skipTest(
1291 "https://github.com/python/cpython/issues/124878 - Known"
1292 " race condition that TSAN identifies.")
1293 # gh-87135: tests that daemon threads hang during finalization
1294 script = textwrap.dedent('''
1295 import os
1296 import sys
1297 import threading
1298 import time
1299 import _testcapi
1300
1301 lock = threading.Lock()
1302 lock.acquire()
1303 thread_started_event = threading.Event()
1304 def thread_func():
1305 try:
1306 thread_started_event.set()
1307 _testcapi.finalize_thread_hang(lock.acquire)
1308 finally:
1309 # Control must not reach here.
1310 os._exit(2)
1311
1312 t = threading.Thread(target=thread_func)
1313 t.daemon = True
1314 t.start()
1315 thread_started_event.wait()
1316 # Sleep to ensure daemon thread is blocked on `lock.acquire`
1317 #
1318 # Note: This test is designed so that in the unlikely case that
1319 # `0.1` seconds is not sufficient time for the thread to become
1320 # blocked on `lock.acquire`, the test will still pass, it just
1321 # won't be properly testing the thread behavior during
1322 # finalization.
1323 time.sleep(0.1)
1324
1325 def run_during_finalization():
1326 # Wake up daemon thread
1327 lock.release()
1328 # Sleep to give the daemon thread time to crash if it is going
1329 # to.
1330 #
1331 # Note: If due to an exceptionally slow execution this delay is
1332 # insufficient, the test will still pass but will simply be
1333 # ineffective as a test.
1334 time.sleep(0.1)
1335 # If control reaches here, the test succeeded.
1336 os._exit(0)
1337
1338 # Replace sys.stderr.flush as a way to run code during finalization
1339 orig_flush = sys.stderr.flush
1340 def do_flush(*args, **kwargs):
1341 orig_flush(*args, **kwargs)
1342 if not sys.is_finalizing:
1343 return
1344 sys.stderr.flush = orig_flush
1345 run_during_finalization()
1346
1347 sys.stderr.flush = do_flush
1348
1349 # If the follow exit code is retained, `run_during_finalization`
1350 # did not run.
1351 sys.exit(1)
1352 ''')
1353 assert_python_ok("-c", script)
1354
1355 @skip_unless_reliable_fork
1356 @unittest.skipUnless(hasattr(threading, 'get_native_id'), "test needs threading.get_native_id()")
1357 def test_native_id_after_fork(self):
1358 script = """if True:
1359 import threading
1360 import os
1361 from test import support
1362
1363 parent_thread_native_id = threading.current_thread().native_id
1364 print(parent_thread_native_id, flush=True)
1365 assert parent_thread_native_id == threading.get_native_id()
1366 childpid = os.fork()
1367 if childpid == 0:
1368 print(threading.current_thread().native_id, flush=True)
1369 assert threading.current_thread().native_id == threading.get_native_id()
1370 else:
1371 try:
1372 assert parent_thread_native_id == threading.current_thread().native_id
1373 assert parent_thread_native_id == threading.get_native_id()
1374 finally:
1375 support.wait_process(childpid, exitcode=0)
1376 """
1377 rc, out, err = assert_python_ok('-c', script)
1378 self.assertEqual(rc, 0)
1379 self.assertEqual(err, b"")
1380 native_ids = out.strip().splitlines()
1381 self.assertEqual(len(native_ids), 2)
1382 self.assertNotEqual(native_ids[0], native_ids[1])
1383
1384 def test_stop_the_world_during_finalization(self):
1385 # gh-137433: Test functions that trigger a stop-the-world in the free
1386 # threading build concurrent with interpreter finalization.
1387 script = """if True:
1388 import gc
1389 import sys
1390 import threading
1391 NUM_THREADS = 5
1392 b = threading.Barrier(NUM_THREADS + 1)
1393 def run_in_bg():
1394 b.wait()
1395 while True:
1396 sys.setprofile(None)
1397 gc.collect()
1398
1399 for _ in range(NUM_THREADS):
1400 t = threading.Thread(target=run_in_bg, daemon=True)
1401 t.start()
1402
1403 b.wait()
1404 print("Exiting...")
1405 """
1406 rc, out, err = assert_python_ok('-c', script)
1407 self.assertEqual(rc, 0)
1408 self.assertEqual(err, b"")
1409 self.assertEqual(out.strip(), b"Exiting...")
1410
1411 class ThreadJoinOnShutdown(BaseTestCase):
1412
1413 def _run_and_join(self, script):
1414 script = """if 1:
1415 import sys, os, time, threading
1416
1417 # a thread, which waits for the main program to terminate
1418 def joiningfunc(mainthread):
1419 mainthread.join()
1420 print('end of thread')
1421 # stdout is fully buffered because not a tty, we have to flush
1422 # before exit.
1423 sys.stdout.flush()
1424 \n""" + script
1425
1426 rc, out, err = assert_python_ok("-c", script)
1427 data = out.decode().replace('\r', '')
1428 self.assertEqual(data, "end of main\nend of thread\n")
1429
1430 def test_1_join_on_shutdown(self):
1431 # The usual case: on exit, wait for a non-daemon thread
1432 script = """if 1:
1433 import os
1434 t = threading.Thread(target=joiningfunc,
1435 args=(threading.current_thread(),))
1436 t.start()
1437 time.sleep(0.1)
1438 print('end of main')
1439 """
1440 self._run_and_join(script)
1441
1442 @skip_unless_reliable_fork
1443 def test_2_join_in_forked_process(self):
1444 # Like the test above, but from a forked interpreter
1445 script = """if 1:
1446 from test import support
1447
1448 childpid = os.fork()
1449 if childpid != 0:
1450 # parent process
1451 support.wait_process(childpid, exitcode=0)
1452 sys.exit(0)
1453
1454 # child process
1455 t = threading.Thread(target=joiningfunc,
1456 args=(threading.current_thread(),))
1457 t.start()
1458 print('end of main')
1459 """
1460 self._run_and_join(script)
1461
1462 @skip_unless_reliable_fork
1463 def test_3_join_in_forked_from_thread(self):
1464 # Like the test above, but fork() was called from a worker thread
1465 # In the forked process, the main Thread object must be marked as stopped.
1466
1467 script = """if 1:
1468 from test import support
1469
1470 main_thread = threading.current_thread()
1471 def worker():
1472 childpid = os.fork()
1473 if childpid != 0:
1474 # parent process
1475 support.wait_process(childpid, exitcode=0)
1476 sys.exit(0)
1477
1478 # child process
1479 t = threading.Thread(target=joiningfunc,
1480 args=(main_thread,))
1481 print('end of main')
1482 t.start()
1483 t.join() # Should not block: main_thread is already stopped
1484
1485 w = threading.Thread(target=worker)
1486 w.start()
1487 """
1488 self._run_and_join(script)
1489
1490 @unittest.skipIf(sys.platform in platforms_to_skip, "due to known OS bug")
1491 @support.bigmemtest(size=40, memuse=70*2**20, dry_run=False)
1492 def test_4_daemon_threads(self, size):
1493 # Check that a daemon thread cannot crash the interpreter on shutdown
1494 # by manipulating internal structures that are being disposed of in
1495 # the main thread.
1496 if support.check_sanitizer(thread=True):
1497 # some of the threads running `random_io` below will still be alive
1498 # at process exit
1499 self.skipTest("TSAN would report thread leak")
1500
1501 script = """if True:
1502 import os
1503 import random
1504 import sys
1505 import time
1506 import threading
1507
1508 thread_has_run = set()
1509
1510 def random_io():
1511 '''Loop for a while sleeping random tiny amounts and doing some I/O.'''
1512 import test.test_threading as mod
1513 while True:
1514 with open(mod.__file__, 'rb') as in_f:
1515 stuff = in_f.read(200)
1516 with open(os.devnull, 'wb') as null_f:
1517 null_f.write(stuff)
1518 time.sleep(random.random() / 1995)
1519 thread_has_run.add(threading.current_thread())
1520
1521 def main():
1522 count = 0
1523 for _ in range(40):
1524 new_thread = threading.Thread(target=random_io)
1525 new_thread.daemon = True
1526 new_thread.start()
1527 count += 1
1528 while len(thread_has_run) < count:
1529 time.sleep(0.001)
1530 # Trigger process shutdown
1531 sys.exit(0)
1532
1533 main()
1534 """
1535 rc, out, err = assert_python_ok('-c', script)
1536 self.assertFalse(err)
1537
1538 def test_thread_from_thread(self):
1539 script = """if True:
1540 import threading
1541 import time
1542
1543 def thread2():
1544 time.sleep(0.05)
1545 print("OK")
1546
1547 def thread1():
1548 time.sleep(0.05)
1549 t2 = threading.Thread(target=thread2)
1550 t2.start()
1551
1552 t = threading.Thread(target=thread1)
1553 t.start()
1554 # do not join() -- the interpreter waits for non-daemon threads to
1555 # finish.
1556 """
1557 rc, out, err = assert_python_ok('-c', script)
1558 self.assertEqual(err, b"")
1559 self.assertEqual(out.strip(), b"OK")
1560 self.assertEqual(rc, 0)
1561
1562 @skip_unless_reliable_fork
1563 def test_reinit_tls_after_fork(self):
1564 # Issue #13817: fork() would deadlock in a multithreaded program with
1565 # the ad-hoc TLS implementation.
1566
1567 def do_fork_and_wait():
1568 # just fork a child process and wait it
1569 pid = os.fork()
1570 if pid > 0:
1571 support.wait_process(pid, exitcode=50)
1572 else:
1573 os._exit(50)
1574
1575 # Ignore the warning about fork with threads.
1576 with warnings.catch_warnings(category=DeprecationWarning,
1577 action="ignore"):
1578 # start a bunch of threads that will fork() child processes
1579 threads = []
1580 for i in range(16):
1581 t = threading.Thread(target=do_fork_and_wait)
1582 threads.append(t)
1583 t.start()
1584
1585 for t in threads:
1586 t.join()
1587
1588 @skip_unless_reliable_fork
1589 def test_clear_threads_states_after_fork(self):
1590 # Issue #17094: check that threads states are cleared after fork()
1591
1592 # start a bunch of threads
1593 threads = []
1594 for i in range(16):
1595 t = threading.Thread(target=lambda : time.sleep(0.3))
1596 threads.append(t)
1597 t.start()
1598
1599 try:
1600 # Ignore the warning about fork with threads.
1601 with warnings.catch_warnings(category=DeprecationWarning,
1602 action="ignore"):
1603 pid = os.fork()
1604 if pid == 0:
1605 # check that threads states have been cleared
1606 if len(sys._current_frames()) == 1:
1607 os._exit(51)
1608 else:
1609 os._exit(52)
1610 else:
1611 support.wait_process(pid, exitcode=51)
1612 finally:
1613 for t in threads:
1614 t.join()
1615
1616
1617 class SubinterpThreadingTests(BaseTestCase):
1618 def pipe(self):
1619 r, w = os.pipe()
1620 self.addCleanup(os.close, r)
1621 self.addCleanup(os.close, w)
1622 if hasattr(os, 'set_blocking'):
1623 os.set_blocking(r, False)
1624 return (r, w)
1625
1626 def test_threads_join(self):
1627 # Non-daemon threads should be joined at subinterpreter shutdown
1628 # (issue #18808)
1629 r, w = self.pipe()
1630 code = textwrap.dedent(r"""
1631 import os
1632 import random
1633 import threading
1634 import time
1635
1636 def random_sleep():
1637 seconds = random.random() * 0.010
1638 time.sleep(seconds)
1639
1640 def f():
1641 # Sleep a bit so that the thread is still running when
1642 # Py_EndInterpreter is called.
1643 random_sleep()
1644 os.write(%d, b"x")
1645
1646 threading.Thread(target=f).start()
1647 random_sleep()
1648 """ % (w,))
1649 ret = test.support.run_in_subinterp(code)
1650 self.assertEqual(ret, 0)
1651 # The thread was joined properly.
1652 self.assertEqual(os.read(r, 1), b"x")
1653
1654 def test_threads_join_2(self):
1655 # Same as above, but a delay gets introduced after the thread's
1656 # Python code returned but before the thread state is deleted.
1657 # To achieve this, we register a thread-local object which sleeps
1658 # a bit when deallocated.
1659 r, w = self.pipe()
1660 code = textwrap.dedent(r"""
1661 import os
1662 import random
1663 import threading
1664 import time
1665
1666 def random_sleep():
1667 seconds = random.random() * 0.010
1668 time.sleep(seconds)
1669
1670 class Sleeper:
1671 def __del__(self):
1672 random_sleep()
1673
1674 tls = threading.local()
1675
1676 def f():
1677 # Sleep a bit so that the thread is still running when
1678 # Py_EndInterpreter is called.
1679 random_sleep()
1680 tls.x = Sleeper()
1681 os.write(%d, b"x")
1682
1683 threading.Thread(target=f).start()
1684 random_sleep()
1685 """ % (w,))
1686 ret = test.support.run_in_subinterp(code)
1687 self.assertEqual(ret, 0)
1688 # The thread was joined properly.
1689 self.assertEqual(os.read(r, 1), b"x")
1690
1691 @requires_subinterpreters
1692 def test_threads_join_with_no_main(self):
1693 r_interp, w_interp = self.pipe()
1694
1695 INTERP = b'I'
1696 FINI = b'F'
1697 DONE = b'D'
1698
1699 interp = interpreters.create()
1700 interp.exec(f"""if True:
1701 import os
1702 import threading
1703 import time
1704
1705 done = False
1706
1707 def notify_fini():
1708 global done
1709 done = True
1710 os.write({w_interp}, {FINI!r})
1711 t.join()
1712 threading._register_atexit(notify_fini)
1713
1714 def task():
1715 while not done:
1716 time.sleep(0.1)
1717 os.write({w_interp}, {DONE!r})
1718 t = threading.Thread(target=task)
1719 t.start()
1720
1721 os.write({w_interp}, {INTERP!r})
1722 """)
1723 interp.close()
1724
1725 self.assertEqual(os.read(r_interp, 1), INTERP)
1726 self.assertEqual(os.read(r_interp, 1), FINI)
1727 self.assertEqual(os.read(r_interp, 1), DONE)
1728
1729 @cpython_only
1730 @support.skip_if_sanitizer(thread=True, memory=True)
1731 def test_daemon_threads_fatal_error(self):
1732 import_module("_testcapi")
1733 subinterp_code = f"""if 1:
1734 import os
1735 import threading
1736 import time
1737
1738 def f():
1739 # Make sure the daemon thread is still running when
1740 # Py_EndInterpreter is called.
1741 time.sleep({test.support.SHORT_TIMEOUT})
1742 threading.Thread(target=f, daemon=True).start()
1743 """
1744 script = r"""if 1:
1745 import _testcapi
1746
1747 _testcapi.run_in_subinterp(%r)
1748 """ % (subinterp_code,)
1749 assert_python_ok("-c", script)
1750
1751 def _check_allowed(self, before_start='', *,
1752 allowed=True,
1753 daemon_allowed=True,
1754 daemon=False,
1755 ):
1756 import_module("_testinternalcapi")
1757 subinterp_code = textwrap.dedent(f"""
1758 import test.support
1759 import threading
1760 def func():
1761 print('this should not have run!')
1762 t = threading.Thread(target=func, daemon={daemon})
1763 {before_start}
1764 t.start()
1765 """)
1766 check_multi_interp_extensions = bool(support.Py_GIL_DISABLED)
1767 script = textwrap.dedent(f"""
1768 import test.support
1769 test.support.run_in_subinterp_with_config(
1770 {subinterp_code!r},
1771 use_main_obmalloc=True,
1772 allow_fork=True,
1773 allow_exec=True,
1774 allow_threads={allowed},
1775 allow_daemon_threads={daemon_allowed},
1776 check_multi_interp_extensions={check_multi_interp_extensions},
1777 own_gil=False,
1778 )
1779 """)
1780 with test.support.SuppressCrashReport():
1781 _, _, err = assert_python_ok("-c", script)
1782 return err.decode()
1783
1784 @cpython_only
1785 def test_threads_not_allowed(self):
1786 err = self._check_allowed(
1787 allowed=False,
1788 daemon_allowed=False,
1789 daemon=False,
1790 )
1791 self.assertIn('RuntimeError', err)
1792
1793 @cpython_only
1794 def test_daemon_threads_not_allowed(self):
1795 with self.subTest('via Thread()'):
1796 err = self._check_allowed(
1797 allowed=True,
1798 daemon_allowed=False,
1799 daemon=True,
1800 )
1801 self.assertIn('RuntimeError', err)
1802
1803 with self.subTest('via Thread.daemon setter'):
1804 err = self._check_allowed(
1805 't.daemon = True',
1806 allowed=True,
1807 daemon_allowed=False,
1808 daemon=False,
1809 )
1810 self.assertIn('RuntimeError', err)
1811
1812
1813 class ThreadingExceptionTests(BaseTestCase):
1814 # A RuntimeError should be raised if Thread.start() is called
1815 # multiple times.
1816 def test_start_thread_again(self):
1817 thread = threading.Thread()
1818 thread.start()
1819 self.assertRaises(RuntimeError, thread.start)
1820 thread.join()
1821
1822 def test_joining_current_thread(self):
1823 current_thread = threading.current_thread()
1824 self.assertRaises(RuntimeError, current_thread.join);
1825
1826 def test_joining_inactive_thread(self):
1827 thread = threading.Thread()
1828 self.assertRaises(RuntimeError, thread.join)
1829
1830 def test_daemonize_active_thread(self):
1831 thread = threading.Thread()
1832 thread.start()
1833 self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
1834 thread.join()
1835
1836 def test_releasing_unacquired_lock(self):
1837 lock = threading.Lock()
1838 self.assertRaises(RuntimeError, lock.release)
1839
1840 @requires_subprocess()
1841 def test_recursion_limit(self):
1842 # Issue 9670
1843 # test that excessive recursion within a non-main thread causes
1844 # an exception rather than crashing the interpreter on platforms
1845 # like Mac OS X or FreeBSD which have small default stack sizes
1846 # for threads
1847 script = """if True:
1848 import threading
1849
1850 def recurse():
1851 return recurse()
1852
1853 def outer():
1854 try:
1855 recurse()
1856 except RecursionError:
1857 pass
1858
1859 w = threading.Thread(target=outer)
1860 w.start()
1861 w.join()
1862 print('end of main thread')
1863 """
1864 expected_output = "end of main thread\n"
1865 p = subprocess.Popen([sys.executable, "-c", script],
1866 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
1867 stdout, stderr = p.communicate()
1868 data = stdout.decode().replace('\r', '')
1869 self.assertEqual(p.returncode, 0, "Unexpected error: " + stderr.decode())
1870 self.assertEqual(data, expected_output)
1871
1872 def test_print_exception(self):
1873 script = r"""if True:
1874 import threading
1875 import time
1876
1877 running = False
1878 def run():
1879 global running
1880 running = True
1881 while running:
1882 time.sleep(0.01)
1883 1/0
1884 t = threading.Thread(target=run)
1885 t.start()
1886 while not running:
1887 time.sleep(0.01)
1888 running = False
1889 t.join()
1890 """
1891 rc, out, err = assert_python_ok("-c", script)
1892 self.assertEqual(out, b'')
1893 err = err.decode()
1894 self.assertIn("Exception in thread", err)
1895 self.assertIn("Traceback (most recent call last):", err)
1896 self.assertIn("ZeroDivisionError", err)
1897 self.assertNotIn("Unhandled exception", err)
1898
1899 def test_print_exception_stderr_is_none_1(self):
1900 script = r"""if True:
1901 import sys
1902 import threading
1903 import time
1904
1905 running = False
1906 def run():
1907 global running
1908 running = True
1909 while running:
1910 time.sleep(0.01)
1911 1/0
1912 t = threading.Thread(target=run)
1913 t.start()
1914 while not running:
1915 time.sleep(0.01)
1916 sys.stderr = None
1917 running = False
1918 t.join()
1919 """
1920 rc, out, err = assert_python_ok("-c", script)
1921 self.assertEqual(out, b'')
1922 err = err.decode()
1923 self.assertIn("Exception in thread", err)
1924 self.assertIn("Traceback (most recent call last):", err)
1925 self.assertIn("ZeroDivisionError", err)
1926 self.assertNotIn("Unhandled exception", err)
1927
1928 def test_print_exception_stderr_is_none_2(self):
1929 script = r"""if True:
1930 import sys
1931 import threading
1932 import time
1933
1934 running = False
1935 def run():
1936 global running
1937 running = True
1938 while running:
1939 time.sleep(0.01)
1940 1/0
1941 sys.stderr = None
1942 t = threading.Thread(target=run)
1943 t.start()
1944 while not running:
1945 time.sleep(0.01)
1946 running = False
1947 t.join()
1948 """
1949 rc, out, err = assert_python_ok("-c", script)
1950 self.assertEqual(out, b'')
1951 self.assertNotIn("Unhandled exception", err.decode())
1952
1953 def test_print_exception_gh_102056(self):
1954 # This used to crash. See gh-102056.
1955 script = r"""if True:
1956 import time
1957 import threading
1958 import _thread
1959
1960 def f():
1961 try:
1962 f()
1963 except RecursionError:
1964 f()
1965
1966 def g():
1967 try:
1968 raise ValueError()
1969 except* ValueError:
1970 f()
1971
1972 def h():
1973 time.sleep(1)
1974 _thread.interrupt_main()
1975
1976 t = threading.Thread(target=h)
1977 t.start()
1978 g()
1979 t.join()
1980 """
1981
1982 assert_python_failure("-c", script)
1983
1984 def test_bare_raise_in_brand_new_thread(self):
1985 def bare_raise():
1986 raise
1987
1988 class Issue27558(threading.Thread):
1989 exc = None
1990
1991 def run(self):
1992 try:
1993 bare_raise()
1994 except Exception as exc:
1995 self.exc = exc
1996
1997 thread = Issue27558()
1998 thread.start()
1999 thread.join()
2000 self.assertIsNotNone(thread.exc)
2001 self.assertIsInstance(thread.exc, RuntimeError)
2002 # explicitly break the reference cycle to not leak a dangling thread
2003 thread.exc = None
2004
2005 def test_multithread_modify_file_noerror(self):
2006 # See issue25872
2007 def modify_file():
2008 with open(os_helper.TESTFN, 'w', encoding='utf-8') as fp:
2009 fp.write(' ')
2010 traceback.format_stack()
2011
2012 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
2013 threads = [
2014 threading.Thread(target=modify_file)
2015 for i in range(100)
2016 ]
2017 for t in threads:
2018 t.start()
2019 t.join()
2020
2021 def test_dummy_thread_on_interpreter_shutdown(self):
2022 # GH-130522: When `threading` held a reference to itself and then a
2023 # _DummyThread() object was created, destruction of the dummy thread
2024 # would emit an unraisable exception at shutdown, due to a lock being
2025 # destroyed.
2026 code = """if True:
2027 import sys
2028 import threading
2029
2030 threading.x = sys.modules[__name__]
2031 x = threading._DummyThread()
2032 """
2033 rc, out, err = assert_python_ok("-c", code)
2034 self.assertEqual(rc, 0)
2035 self.assertEqual(out, b"")
2036 self.assertEqual(err, b"")
2037
2038
2039 class ThreadRunFail(threading.Thread):
2040 def run(self):
2041 raise ValueError("run failed")
2042
2043
2044 class ExceptHookTests(BaseTestCase):
2045 def setUp(self):
2046 restore_default_excepthook(self)
2047 super().setUp()
2048
2049 @force_not_colorized
2050 def test_excepthook(self):
2051 with support.captured_output("stderr") as stderr:
2052 thread = ThreadRunFail(name="excepthook thread")
2053 thread.start()
2054 thread.join()
2055
2056 stderr = stderr.getvalue().strip()
2057 self.assertIn(f'Exception in thread {thread.name}:\n', stderr)
2058 self.assertIn('Traceback (most recent call last):\n', stderr)
2059 self.assertIn(' raise ValueError("run failed")', stderr)
2060 self.assertIn('ValueError: run failed', stderr)
2061
2062 @support.cpython_only
2063 @force_not_colorized
2064 def test_excepthook_thread_None(self):
2065 # threading.excepthook called with thread=None: log the thread
2066 # identifier in this case.
2067 with support.captured_output("stderr") as stderr:
2068 try:
2069 raise ValueError("bug")
2070 except Exception as exc:
2071 args = threading.ExceptHookArgs([*sys.exc_info(), None])
2072 try:
2073 threading.excepthook(args)
2074 finally:
2075 # Explicitly break a reference cycle
2076 args = None
2077
2078 stderr = stderr.getvalue().strip()
2079 self.assertIn(f'Exception in thread {threading.get_ident()}:\n', stderr)
2080 self.assertIn('Traceback (most recent call last):\n', stderr)
2081 self.assertIn(' raise ValueError("bug")', stderr)
2082 self.assertIn('ValueError: bug', stderr)
2083
2084 def test_system_exit(self):
2085 class ThreadExit(threading.Thread):
2086 def run(self):
2087 sys.exit(1)
2088
2089 # threading.excepthook() silently ignores SystemExit
2090 with support.captured_output("stderr") as stderr:
2091 thread = ThreadExit()
2092 thread.start()
2093 thread.join()
2094
2095 self.assertEqual(stderr.getvalue(), '')
2096
2097 def test_custom_excepthook(self):
2098 args = None
2099
2100 def hook(hook_args):
2101 nonlocal args
2102 args = hook_args
2103
2104 try:
2105 with support.swap_attr(threading, 'excepthook', hook):
2106 thread = ThreadRunFail()
2107 thread.start()
2108 thread.join()
2109
2110 self.assertEqual(args.exc_type, ValueError)
2111 self.assertEqual(str(args.exc_value), 'run failed')
2112 self.assertEqual(args.exc_traceback, args.exc_value.__traceback__)
2113 self.assertIs(args.thread, thread)
2114 finally:
2115 # Break reference cycle
2116 args = None
2117
2118 def test_custom_excepthook_fail(self):
2119 def threading_hook(args):
2120 raise ValueError("threading_hook failed")
2121
2122 err_str = None
2123
2124 def sys_hook(exc_type, exc_value, exc_traceback):
2125 nonlocal err_str
2126 err_str = str(exc_value)
2127
2128 with support.swap_attr(threading, 'excepthook', threading_hook), \
2129 support.swap_attr(sys, 'excepthook', sys_hook), \
2130 support.captured_output('stderr') as stderr:
2131 thread = ThreadRunFail()
2132 thread.start()
2133 thread.join()
2134
2135 self.assertEqual(stderr.getvalue(),
2136 'Exception in threading.excepthook:\n')
2137 self.assertEqual(err_str, 'threading_hook failed')
2138
2139 def test_original_excepthook(self):
2140 def run_thread():
2141 with support.captured_output("stderr") as output:
2142 thread = ThreadRunFail(name="excepthook thread")
2143 thread.start()
2144 thread.join()
2145 return output.getvalue()
2146
2147 def threading_hook(args):
2148 print("Running a thread failed", file=sys.stderr)
2149
2150 default_output = run_thread()
2151 with support.swap_attr(threading, 'excepthook', threading_hook):
2152 custom_hook_output = run_thread()
2153 threading.excepthook = threading.__excepthook__
2154 recovered_output = run_thread()
2155
2156 self.assertEqual(default_output, recovered_output)
2157 self.assertNotEqual(default_output, custom_hook_output)
2158 self.assertEqual(custom_hook_output, "Running a thread failed\n")
2159
2160
2161 class TimerTests(BaseTestCase):
2162
2163 def setUp(self):
2164 BaseTestCase.setUp(self)
2165 self.callback_args = []
2166 self.callback_event = threading.Event()
2167
2168 def test_init_immutable_default_args(self):
2169 # Issue 17435: constructor defaults were mutable objects, they could be
2170 # mutated via the object attributes and affect other Timer objects.
2171 timer1 = threading.Timer(0.01, self._callback_spy)
2172 timer1.start()
2173 self.callback_event.wait()
2174 timer1.args.append("blah")
2175 timer1.kwargs["foo"] = "bar"
2176 self.callback_event.clear()
2177 timer2 = threading.Timer(0.01, self._callback_spy)
2178 timer2.start()
2179 self.callback_event.wait()
2180 self.assertEqual(len(self.callback_args), 2)
2181 self.assertEqual(self.callback_args, [((), {}), ((), {})])
2182 timer1.join()
2183 timer2.join()
2184
2185 def _callback_spy(self, *args, **kwargs):
2186 self.callback_args.append((args[:], kwargs.copy()))
2187 self.callback_event.set()
2188
2189 class LockTests(lock_tests.LockTests):
2190 locktype = staticmethod(threading.Lock)
2191
2192 class PyRLockTests(lock_tests.RLockTests):
2193 locktype = staticmethod(threading._PyRLock)
2194
2195 @unittest.skipIf(threading._CRLock is None, 'RLock not implemented in C')
2196 class CRLockTests(lock_tests.RLockTests):
2197 locktype = staticmethod(threading._CRLock)
2198
2199 def test_signature(self): # gh-102029
2200 with warnings.catch_warnings(record=True) as warnings_log:
2201 threading.RLock()
2202 self.assertEqual(warnings_log, [])
2203
2204 arg_types = [
2205 ((1,), {}),
2206 ((), {'a': 1}),
2207 ((1, 2), {'a': 1}),
2208 ]
2209 for args, kwargs in arg_types:
2210 with self.subTest(args=args, kwargs=kwargs):
2211 with self.assertWarns(DeprecationWarning):
2212 threading.RLock(*args, **kwargs)
2213
2214 # Subtypes with custom `__init__` are allowed (but, not recommended):
2215 class CustomRLock(self.locktype):
2216 def __init__(self, a, *, b) -> None:
2217 super().__init__()
2218
2219 with warnings.catch_warnings(record=True) as warnings_log:
2220 CustomRLock(1, b=2)
2221 self.assertEqual(warnings_log, [])
2222
2223 class EventTests(lock_tests.EventTests):
2224 eventtype = staticmethod(threading.Event)
2225
2226 class ConditionAsRLockTests(lock_tests.RLockTests):
2227 # Condition uses an RLock by default and exports its API.
2228 locktype = staticmethod(threading.Condition)
2229
2230 def test_recursion_count(self):
2231 self.skipTest("Condition does not expose _recursion_count()")
2232
2233 class ConditionTests(lock_tests.ConditionTests):
2234 condtype = staticmethod(threading.Condition)
2235
2236 class SemaphoreTests(lock_tests.SemaphoreTests):
2237 semtype = staticmethod(threading.Semaphore)
2238
2239 class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
2240 semtype = staticmethod(threading.BoundedSemaphore)
2241
2242 class BarrierTests(lock_tests.BarrierTests):
2243 barriertype = staticmethod(threading.Barrier)
2244
2245
2246 class MiscTestCase(unittest.TestCase):
2247 def test__all__(self):
2248 restore_default_excepthook(self)
2249
2250 extra = {"ThreadError"}
2251 not_exported = {'currentThread', 'activeCount'}
2252 support.check__all__(self, threading, ('threading', '_thread'),
2253 extra=extra, not_exported=not_exported)
2254
2255 @unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
2256 @unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
2257 def test_set_name(self):
2258 # Ensure main thread name is restored after test
2259 self.addCleanup(_thread.set_name, _thread._get_name())
2260
2261 # set_name() limit in bytes
2262 truncate = getattr(_thread, "_NAME_MAXLEN", None)
2263 limit = truncate or 100
2264
2265 tests = [
2266 # test short ASCII name
2267 "CustomName",
2268
2269 # test short non-ASCII name
2270 "namé€",
2271
2272 # embedded null character: name is truncated
2273 # at the first null character
2274 "embed\0null",
2275
2276 # Test long ASCII names (not truncated)
2277 "x" * limit,
2278
2279 # Test long ASCII names (truncated)
2280 "x" * (limit + 10),
2281
2282 # Test long non-ASCII name (truncated)
2283 "x" * (limit - 1) + "é€",
2284
2285 # Test long non-BMP names (truncated) creating surrogate pairs
2286 # on Windows
2287 "x" * (limit - 1) + "\U0010FFFF",
2288 "x" * (limit - 2) + "\U0010FFFF" * 2,
2289 "x" + "\U0001f40d" * limit,
2290 "xx" + "\U0001f40d" * limit,
2291 "xxx" + "\U0001f40d" * limit,
2292 "xxxx" + "\U0001f40d" * limit,
2293 ]
2294 if os_helper.FS_NONASCII:
2295 tests.append(f"nonascii:{os_helper.FS_NONASCII}")
2296 if os_helper.TESTFN_UNENCODABLE:
2297 tests.append(os_helper.TESTFN_UNENCODABLE)
2298
2299 if sys.platform.startswith("sunos"):
2300 # Use ASCII encoding on Solaris/Illumos/OpenIndiana
2301 encoding = "ascii"
2302 else:
2303 encoding = sys.getfilesystemencoding()
2304
2305 def work():
2306 nonlocal work_name
2307 work_name = _thread._get_name()
2308
2309 for name in tests:
2310 if not support.MS_WINDOWS:
2311 encoded = name.encode(encoding, "replace")
2312 if b'\0' in encoded:
2313 encoded = encoded.split(b'\0', 1)[0]
2314 if truncate is not None:
2315 encoded = encoded[:truncate]
2316 if sys.platform.startswith("sunos"):
2317 expected = encoded.decode("ascii", "surrogateescape")
2318 else:
2319 expected = os.fsdecode(encoded)
2320 else:
2321 size = 0
2322 chars = []
2323 for ch in name:
2324 if ord(ch) > 0xFFFF:
2325 size += 2
2326 else:
2327 size += 1
2328 if size > truncate:
2329 break
2330 chars.append(ch)
2331 expected = ''.join(chars)
2332
2333 if '\0' in expected:
2334 expected = expected.split('\0', 1)[0]
2335
2336 with self.subTest(name=name, expected=expected, thread="main"):
2337 _thread.set_name(name)
2338 self.assertEqual(_thread._get_name(), expected)
2339
2340 with self.subTest(name=name, expected=expected, thread="worker"):
2341 work_name = None
2342 thread = threading.Thread(target=work, name=name)
2343 thread.start()
2344 thread.join()
2345 self.assertEqual(work_name, expected,
2346 f"{len(work_name)=} and {len(expected)=}")
2347
2348 @unittest.skipUnless(hasattr(_thread, 'set_name'), "missing _thread.set_name")
2349 @unittest.skipUnless(hasattr(_thread, '_get_name'), "missing _thread._get_name")
2350 def test_change_name(self):
2351 # Change the name of a thread while the thread is running
2352
2353 name1 = None
2354 name2 = None
2355 def work():
2356 nonlocal name1, name2
2357 name1 = _thread._get_name()
2358 threading.current_thread().name = "new name"
2359 name2 = _thread._get_name()
2360
2361 thread = threading.Thread(target=work, name="name")
2362 thread.start()
2363 thread.join()
2364 self.assertEqual(name1, "name")
2365 self.assertEqual(name2, "new name")
2366
2367
2368 class InterruptMainTests(unittest.TestCase):
2369 def check_interrupt_main_with_signal_handler(self, signum):
2370 def handler(signum, frame):
2371 1/0
2372
2373 old_handler = signal.signal(signum, handler)
2374 self.addCleanup(signal.signal, signum, old_handler)
2375
2376 with self.assertRaises(ZeroDivisionError):
2377 _thread.interrupt_main()
2378
2379 def check_interrupt_main_noerror(self, signum):
2380 handler = signal.getsignal(signum)
2381 try:
2382 # No exception should arise.
2383 signal.signal(signum, signal.SIG_IGN)
2384 _thread.interrupt_main(signum)
2385
2386 signal.signal(signum, signal.SIG_DFL)
2387 _thread.interrupt_main(signum)
2388 finally:
2389 # Restore original handler
2390 signal.signal(signum, handler)
2391
2392 @requires_gil_enabled("gh-118433: Flaky due to a longstanding bug")
2393 def test_interrupt_main_subthread(self):
2394 # Calling start_new_thread with a function that executes interrupt_main
2395 # should raise KeyboardInterrupt upon completion.
2396 def call_interrupt():
2397 _thread.interrupt_main()
2398 t = threading.Thread(target=call_interrupt)
2399 with self.assertRaises(KeyboardInterrupt):
2400 t.start()
2401 t.join()
2402 t.join()
2403
2404 def test_interrupt_main_mainthread(self):
2405 # Make sure that if interrupt_main is called in main thread that
2406 # KeyboardInterrupt is raised instantly.
2407 with self.assertRaises(KeyboardInterrupt):
2408 _thread.interrupt_main()
2409
2410 def test_interrupt_main_with_signal_handler(self):
2411 self.check_interrupt_main_with_signal_handler(signal.SIGINT)
2412 self.check_interrupt_main_with_signal_handler(signal.SIGTERM)
2413
2414 def test_interrupt_main_noerror(self):
2415 self.check_interrupt_main_noerror(signal.SIGINT)
2416 self.check_interrupt_main_noerror(signal.SIGTERM)
2417
2418 def test_interrupt_main_invalid_signal(self):
2419 self.assertRaises(ValueError, _thread.interrupt_main, -1)
2420 self.assertRaises(ValueError, _thread.interrupt_main, signal.NSIG)
2421 self.assertRaises(ValueError, _thread.interrupt_main, 1000000)
2422
2423 @threading_helper.reap_threads
2424 def test_can_interrupt_tight_loops(self):
2425 cont = [True]
2426 started = [False]
2427 interrupted = [False]
2428
2429 def worker(started, cont, interrupted):
2430 iterations = 100_000_000
2431 started[0] = True
2432 while cont[0]:
2433 if iterations:
2434 iterations -= 1
2435 else:
2436 return
2437 pass
2438 interrupted[0] = True
2439
2440 t = threading.Thread(target=worker,args=(started, cont, interrupted))
2441 t.start()
2442 while not started[0]:
2443 pass
2444 cont[0] = False
2445 t.join()
2446 self.assertTrue(interrupted[0])
2447
2448
2449 class AtexitTests(unittest.TestCase):
2450
2451 def test_atexit_output(self):
2452 rc, out, err = assert_python_ok("-c", """if True:
2453 import threading
2454
2455 def run_last():
2456 print('parrot')
2457
2458 threading._register_atexit(run_last)
2459 """)
2460
2461 self.assertFalse(err)
2462 self.assertEqual(out.strip(), b'parrot')
2463
2464 def test_atexit_called_once(self):
2465 rc, out, err = assert_python_ok("-c", """if True:
2466 import threading
2467 from unittest.mock import Mock
2468
2469 mock = Mock()
2470 threading._register_atexit(mock)
2471 mock.assert_not_called()
2472 # force early shutdown to ensure it was called once
2473 threading._shutdown()
2474 mock.assert_called_once()
2475 """)
2476
2477 self.assertFalse(err)
2478
2479 def test_atexit_after_shutdown(self):
2480 # The only way to do this is by registering an atexit within
2481 # an atexit, which is intended to raise an exception.
2482 rc, out, err = assert_python_ok("-c", """if True:
2483 import threading
2484
2485 def func():
2486 pass
2487
2488 def run_last():
2489 threading._register_atexit(func)
2490
2491 threading._register_atexit(run_last)
2492 """)
2493
2494 self.assertTrue(err)
2495 self.assertIn("RuntimeError: can't register atexit after shutdown",
2496 err.decode())
2497
2498
2499 if __name__ == "__main__":
2500 unittest.main()