Debian Codegraph @

Lib/test/test_thread.py

raw
1 import os
2 import unittest
3 import random
4 from test import support
5 from test.support import threading_helper
6 import _thread as thread
7 import time
8 import warnings
9 import weakref
10
11 from test import lock_tests
12
13 threading_helper.requires_working_threading(module=True)
14
15 NUMTASKS = 10
16 NUMTRIPS = 3
17
18 _print_mutex = thread.allocate_lock()
19
20 def verbose_print(arg):
21 """Helper function for printing out debugging output."""
22 if support.verbose:
23 with _print_mutex:
24 print(arg)
25
26
27 class BasicThreadTest(unittest.TestCase):
28
29 def setUp(self):
30 self.done_mutex = thread.allocate_lock()
31 self.done_mutex.acquire()
32 self.running_mutex = thread.allocate_lock()
33 self.random_mutex = thread.allocate_lock()
34 self.created = 0
35 self.running = 0
36 self.next_ident = 0
37
38 key = threading_helper.threading_setup()
39 self.addCleanup(threading_helper.threading_cleanup, *key)
40
41
42 class ThreadRunningTests(BasicThreadTest):
43
44 def newtask(self):
45 with self.running_mutex:
46 self.next_ident += 1
47 verbose_print("creating task %s" % self.next_ident)
48 thread.start_new_thread(self.task, (self.next_ident,))
49 self.created += 1
50 self.running += 1
51
52 def task(self, ident):
53 with self.random_mutex:
54 delay = random.random() / 10000.0
55 verbose_print("task %s will run for %sus" % (ident, round(delay*1e6)))
56 time.sleep(delay)
57 verbose_print("task %s done" % ident)
58 with self.running_mutex:
59 self.running -= 1
60 if self.created == NUMTASKS and self.running == 0:
61 self.done_mutex.release()
62
63 def test_starting_threads(self):
64 with threading_helper.wait_threads_exit():
65 # Basic test for thread creation.
66 for i in range(NUMTASKS):
67 self.newtask()
68 verbose_print("waiting for tasks to complete...")
69 self.done_mutex.acquire()
70 verbose_print("all tasks done")
71
72 def test_stack_size(self):
73 # Various stack size tests.
74 self.assertEqual(thread.stack_size(), 0, "initial stack size is not 0")
75
76 thread.stack_size(0)
77 self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
78
79 with self.assertRaises(ValueError):
80 # 123 bytes is too small
81 thread.stack_size(123)
82
83 with self.assertRaises(ValueError):
84 # size must be positive
85 thread.stack_size(-4096)
86
87 @unittest.skipIf(os.name not in ("nt", "posix"), 'test meant for nt and posix')
88 def test_nt_and_posix_stack_size(self):
89 try:
90 thread.stack_size(4096)
91 except ValueError:
92 verbose_print("caught expected ValueError setting "
93 "stack_size(4096)")
94 except thread.error:
95 self.skipTest("platform does not support changing thread stack "
96 "size")
97
98 fail_msg = "stack_size(%d) failed - should succeed"
99 for tss in (262144, 0x100000, 0):
100 thread.stack_size(tss)
101 self.assertEqual(thread.stack_size(), tss, fail_msg % tss)
102 verbose_print("successfully set stack_size(%d)" % tss)
103
104 for tss in (262144, 0x100000):
105 verbose_print("trying stack_size = (%d)" % tss)
106 self.next_ident = 0
107 self.created = 0
108 with threading_helper.wait_threads_exit():
109 for i in range(NUMTASKS):
110 self.newtask()
111
112 verbose_print("waiting for all tasks to complete")
113 self.done_mutex.acquire()
114 verbose_print("all tasks done")
115
116 thread.stack_size(0)
117
118 def test__count(self):
119 # Test the _count() function.
120 orig = thread._count()
121 mut = thread.allocate_lock()
122 mut.acquire()
123 started = []
124
125 def task():
126 started.append(None)
127 mut.acquire()
128 mut.release()
129
130 with threading_helper.wait_threads_exit():
131 thread.start_new_thread(task, ())
132 for _ in support.sleeping_retry(support.LONG_TIMEOUT):
133 if started:
134 break
135 self.assertEqual(thread._count(), orig + 1)
136
137 # Allow the task to finish.
138 mut.release()
139
140 # The only reliable way to be sure that the thread ended from the
141 # interpreter's point of view is to wait for the function object to
142 # be destroyed.
143 done = []
144 wr = weakref.ref(task, lambda _: done.append(None))
145 del task
146
147 for _ in support.sleeping_retry(support.LONG_TIMEOUT):
148 if done:
149 break
150 support.gc_collect() # For PyPy or other GCs.
151 self.assertEqual(thread._count(), orig)
152
153 def test_unraisable_exception(self):
154 def task():
155 started.release()
156 raise ValueError("task failed")
157
158 started = thread.allocate_lock()
159 with support.catch_unraisable_exception() as cm:
160 with threading_helper.wait_threads_exit():
161 started.acquire()
162 thread.start_new_thread(task, ())
163 started.acquire()
164
165 self.assertEqual(str(cm.unraisable.exc_value), "task failed")
166 self.assertIsNone(cm.unraisable.object)
167 self.assertEqual(cm.unraisable.err_msg,
168 f"Exception ignored in thread started by {task!r}")
169 self.assertIsNotNone(cm.unraisable.exc_traceback)
170
171 def test_join_thread(self):
172 finished = []
173
174 def task():
175 time.sleep(0.05)
176 finished.append(thread.get_ident())
177
178 with threading_helper.wait_threads_exit():
179 handle = thread.start_joinable_thread(task)
180 handle.join()
181 self.assertEqual(len(finished), 1)
182 self.assertEqual(handle.ident, finished[0])
183
184 def test_join_thread_already_exited(self):
185 def task():
186 pass
187
188 with threading_helper.wait_threads_exit():
189 handle = thread.start_joinable_thread(task)
190 time.sleep(0.05)
191 handle.join()
192
193 def test_join_several_times(self):
194 def task():
195 pass
196
197 with threading_helper.wait_threads_exit():
198 handle = thread.start_joinable_thread(task)
199 handle.join()
200 # Subsequent join() calls should succeed
201 handle.join()
202
203 def test_joinable_not_joined(self):
204 handle_destroyed = thread.allocate_lock()
205 handle_destroyed.acquire()
206
207 def task():
208 handle_destroyed.acquire()
209
210 with threading_helper.wait_threads_exit():
211 handle = thread.start_joinable_thread(task)
212 del handle
213 handle_destroyed.release()
214
215 def test_join_from_self(self):
216 errors = []
217 handles = []
218 start_joinable_thread_returned = thread.allocate_lock()
219 start_joinable_thread_returned.acquire()
220 task_tried_to_join = thread.allocate_lock()
221 task_tried_to_join.acquire()
222
223 def task():
224 start_joinable_thread_returned.acquire()
225 try:
226 handles[0].join()
227 except Exception as e:
228 errors.append(e)
229 finally:
230 task_tried_to_join.release()
231
232 with threading_helper.wait_threads_exit():
233 handle = thread.start_joinable_thread(task)
234 handles.append(handle)
235 start_joinable_thread_returned.release()
236 # Can still join after joining failed in other thread
237 task_tried_to_join.acquire()
238 handle.join()
239
240 assert len(errors) == 1
241 with self.assertRaisesRegex(RuntimeError, "Cannot join current thread"):
242 raise errors[0]
243
244 def test_join_then_self_join(self):
245 # make sure we can't deadlock in the following scenario with
246 # threads t0 and t1 (see comment in `ThreadHandle_join()` for more
247 # details):
248 #
249 # - t0 joins t1
250 # - t1 self joins
251 def make_lock():
252 lock = thread.allocate_lock()
253 lock.acquire()
254 return lock
255
256 error = None
257 self_joiner_handle = None
258 self_joiner_started = make_lock()
259 self_joiner_barrier = make_lock()
260 def self_joiner():
261 nonlocal error
262
263 self_joiner_started.release()
264 self_joiner_barrier.acquire()
265
266 try:
267 self_joiner_handle.join()
268 except Exception as e:
269 error = e
270
271 joiner_started = make_lock()
272 def joiner():
273 joiner_started.release()
274 self_joiner_handle.join()
275
276 with threading_helper.wait_threads_exit():
277 self_joiner_handle = thread.start_joinable_thread(self_joiner)
278 # Wait for the self-joining thread to start
279 self_joiner_started.acquire()
280
281 # Start the thread that joins the self-joiner
282 joiner_handle = thread.start_joinable_thread(joiner)
283
284 # Wait for the joiner to start
285 joiner_started.acquire()
286
287 # Not great, but I don't think there's a deterministic way to make
288 # sure that the self-joining thread has been joined.
289 time.sleep(0.1)
290
291 # Unblock the self-joiner
292 self_joiner_barrier.release()
293
294 self_joiner_handle.join()
295 joiner_handle.join()
296
297 with self.assertRaisesRegex(RuntimeError, "Cannot join current thread"):
298 raise error
299
300 def test_join_with_timeout(self):
301 lock = thread.allocate_lock()
302 lock.acquire()
303
304 def thr():
305 lock.acquire()
306
307 with threading_helper.wait_threads_exit():
308 handle = thread.start_joinable_thread(thr)
309 handle.join(0.1)
310 self.assertFalse(handle.is_done())
311 lock.release()
312 handle.join()
313 self.assertTrue(handle.is_done())
314
315 def test_join_unstarted(self):
316 handle = thread._ThreadHandle()
317 with self.assertRaisesRegex(RuntimeError, "thread not started"):
318 handle.join()
319
320 def test_set_done_unstarted(self):
321 handle = thread._ThreadHandle()
322 with self.assertRaisesRegex(RuntimeError, "thread not started"):
323 handle._set_done()
324
325 def test_start_duplicate_handle(self):
326 lock = thread.allocate_lock()
327 lock.acquire()
328
329 def func():
330 lock.acquire()
331
332 handle = thread._ThreadHandle()
333 with threading_helper.wait_threads_exit():
334 thread.start_joinable_thread(func, handle=handle)
335 with self.assertRaisesRegex(RuntimeError, "thread already started"):
336 thread.start_joinable_thread(func, handle=handle)
337 lock.release()
338 handle.join()
339
340 def test_start_with_none_handle(self):
341 def func():
342 pass
343
344 with threading_helper.wait_threads_exit():
345 handle = thread.start_joinable_thread(func, handle=None)
346 handle.join()
347
348
349 class Barrier:
350 def __init__(self, num_threads):
351 self.num_threads = num_threads
352 self.waiting = 0
353 self.checkin_mutex = thread.allocate_lock()
354 self.checkout_mutex = thread.allocate_lock()
355 self.checkout_mutex.acquire()
356
357 def enter(self):
358 self.checkin_mutex.acquire()
359 self.waiting = self.waiting + 1
360 if self.waiting == self.num_threads:
361 self.waiting = self.num_threads - 1
362 self.checkout_mutex.release()
363 return
364 self.checkin_mutex.release()
365
366 self.checkout_mutex.acquire()
367 self.waiting = self.waiting - 1
368 if self.waiting == 0:
369 self.checkin_mutex.release()
370 return
371 self.checkout_mutex.release()
372
373
374 class BarrierTest(BasicThreadTest):
375
376 def test_barrier(self):
377 with threading_helper.wait_threads_exit():
378 self.bar = Barrier(NUMTASKS)
379 self.running = NUMTASKS
380 for i in range(NUMTASKS):
381 thread.start_new_thread(self.task2, (i,))
382 verbose_print("waiting for tasks to end")
383 self.done_mutex.acquire()
384 verbose_print("tasks done")
385
386 def task2(self, ident):
387 for i in range(NUMTRIPS):
388 if ident == 0:
389 # give it a good chance to enter the next
390 # barrier before the others are all out
391 # of the current one
392 delay = 0
393 else:
394 with self.random_mutex:
395 delay = random.random() / 10000.0
396 verbose_print("task %s will run for %sus" %
397 (ident, round(delay * 1e6)))
398 time.sleep(delay)
399 verbose_print("task %s entering %s" % (ident, i))
400 self.bar.enter()
401 verbose_print("task %s leaving barrier" % ident)
402 with self.running_mutex:
403 self.running -= 1
404 # Must release mutex before releasing done, else the main thread can
405 # exit and set mutex to None as part of global teardown; then
406 # mutex.release() raises AttributeError.
407 finished = self.running == 0
408 if finished:
409 self.done_mutex.release()
410
411 class LockTests(lock_tests.LockTests):
412 locktype = thread.allocate_lock
413
414
415 class TestForkInThread(unittest.TestCase):
416 def setUp(self):
417 self.read_fd, self.write_fd = os.pipe()
418
419 @support.requires_fork()
420 @threading_helper.reap_threads
421 def test_forkinthread(self):
422 pid = None
423
424 def fork_thread(read_fd, write_fd):
425 nonlocal pid
426
427 # Ignore the warning about fork with threads.
428 with warnings.catch_warnings(category=DeprecationWarning,
429 action="ignore"):
430 # fork in a thread (DANGER, undefined per POSIX)
431 if (pid := os.fork()):
432 # parent process
433 return
434
435 # child process
436 try:
437 os.close(read_fd)
438 os.write(write_fd, b"OK")
439 finally:
440 os._exit(0)
441
442 with threading_helper.wait_threads_exit():
443 thread.start_new_thread(fork_thread, (self.read_fd, self.write_fd))
444 self.assertEqual(os.read(self.read_fd, 2), b"OK")
445 os.close(self.write_fd)
446
447 self.assertIsNotNone(pid)
448 support.wait_process(pid, exitcode=0)
449
450 def tearDown(self):
451 try:
452 os.close(self.read_fd)
453 except OSError:
454 pass
455
456 try:
457 os.close(self.write_fd)
458 except OSError:
459 pass
460
461
462 if __name__ == "__main__":
463 unittest.main()