Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_threading.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 6bd276f5d1bc |
| children | 7025278c5a41 |
| rev | line source |
|---|---|
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
1 # Very rudimentary test of threading module |
|
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
2 |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
3 import test.test_support |
|
24364
aa0d2d51fbde
Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents:
19413
diff
changeset
|
4 from test.test_support import verbose |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
5 import random |
|
47438
7eaaad09b6cd
Adds a Thread.getIdent() method to provide the _get_ident() value for
Gregory P. Smith <greg@mad-scientist.com>
parents:
46280
diff
changeset
|
6 import re |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
7 import sys |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59219
diff
changeset
|
8 thread = test.test_support.import_module('thread') |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59219
diff
changeset
|
9 threading = test.test_support.import_module('threading') |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
10 import time |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
11 import unittest |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
12 import weakref |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
13 |
|
57129
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
14 from test import lock_tests |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
15 |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
16 # A trivial mutable counter. |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
17 class Counter(object): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
18 def __init__(self): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
19 self.value = 0 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
20 def inc(self): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
21 self.value += 1 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
22 def dec(self): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
23 self.value -= 1 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
24 def get(self): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
25 return self.value |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
26 |
|
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
27 class TestThread(threading.Thread): |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
28 def __init__(self, name, testcase, sema, mutex, nrunning): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
29 threading.Thread.__init__(self, name=name) |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
30 self.testcase = testcase |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
31 self.sema = sema |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
32 self.mutex = mutex |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
33 self.nrunning = nrunning |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
34 |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
35 def run(self): |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
36 delay = random.random() / 10000.0 |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
37 if verbose: |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
38 print 'task %s will run for %.1f usec' % ( |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
39 self.name, delay * 1e6) |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
40 |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
41 with self.sema: |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
42 with self.mutex: |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
43 self.nrunning.inc() |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
44 if verbose: |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
45 print self.nrunning.get(), 'tasks are running' |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
46 self.testcase.assertTrue(self.nrunning.get() <= 3) |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
47 |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
48 time.sleep(delay) |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
49 if verbose: |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
50 print 'task', self.name, 'done' |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
51 |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
52 with self.mutex: |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
53 self.nrunning.dec() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
54 self.testcase.assertTrue(self.nrunning.get() >= 0) |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
55 if verbose: |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
56 print '%s is finished. %d tasks are running' % ( |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
57 self.name, self.nrunning.get()) |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
58 |
|
56882
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
59 class BaseTestCase(unittest.TestCase): |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
60 def setUp(self): |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
61 self._threads = test.test_support.threading_setup() |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
62 |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
63 def tearDown(self): |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
64 test.test_support.threading_cleanup(*self._threads) |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
65 test.test_support.reap_children() |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
66 |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
67 |
|
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
68 class ThreadTests(BaseTestCase): |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
69 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
70 # Create a bunch of threads, let each do some work, wait until all are |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
71 # done. |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
72 def test_various_ops(self): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
73 # This takes about n/3 seconds to run (about n/3 clumps of tasks, |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
74 # times about 1 second per clump). |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
75 NUMTASKS = 10 |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
76 |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
77 # no more than 3 of the 10 can run at once |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
78 sema = threading.BoundedSemaphore(value=3) |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
79 mutex = threading.RLock() |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
80 numrunning = Counter() |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
81 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
82 threads = [] |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
83 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
84 for i in range(NUMTASKS): |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
85 t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning) |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
86 threads.append(t) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
87 self.assertEqual(t.ident, None) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
88 self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t))) |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
89 t.start() |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
90 |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
91 if verbose: |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
92 print 'waiting for all tasks to complete' |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
93 for t in threads: |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
94 t.join(NUMTASKS) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
95 self.assertTrue(not t.is_alive()) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
96 self.assertNotEqual(t.ident, 0) |
|
52593
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
97 self.assertFalse(t.ident is None) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
98 self.assertTrue(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t))) |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
99 if verbose: |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
100 print 'all tasks done' |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
101 self.assertEqual(numrunning.get(), 0) |
|
19405
9de83dda9f09
add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff
changeset
|
102 |
|
52593
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
103 def test_ident_of_no_threading_threads(self): |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
104 # The ident still must work for the main thread and dummy threads. |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
105 self.assertFalse(threading.currentThread().ident is None) |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
106 def f(): |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
107 ident.append(threading.currentThread().ident) |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
108 done.set() |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
109 done = threading.Event() |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
110 ident = [] |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
111 thread.start_new_thread(f, ()) |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
112 done.wait() |
|
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
113 self.assertFalse(ident[0] is None) |
|
57138
6f5f481d619c
Kill a small potential leak in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57129
diff
changeset
|
114 # Kill the "immortal" _DummyThread |
|
6f5f481d619c
Kill a small potential leak in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
57129
diff
changeset
|
115 del threading._active[ident[0]] |
|
52593
587fcfbe08b6
fix Thread.ident when it is the main thread or a dummy thread #5632
Benjamin Peterson <benjamin@python.org>
parents:
52579
diff
changeset
|
116 |
|
38802
6e0c0c7cf068
Increase the small thread stack size to get the test
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38794
diff
changeset
|
117 # run with a small(ish) thread stack size (256kB) |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
118 def test_various_ops_small_stack(self): |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
119 if verbose: |
|
38802
6e0c0c7cf068
Increase the small thread stack size to get the test
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38794
diff
changeset
|
120 print 'with 256kB thread stack size...' |
|
39389
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
121 try: |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
122 threading.stack_size(262144) |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
123 except thread.error: |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
124 if verbose: |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
125 print 'platform does not support changing thread stack size' |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
126 return |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
127 self.test_various_ops() |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
128 threading.stack_size(0) |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
129 |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
130 # run with a large thread stack size (1MB) |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
131 def test_various_ops_large_stack(self): |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
132 if verbose: |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
133 print 'with 1MB thread stack size...' |
|
39389
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
134 try: |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
135 threading.stack_size(0x100000) |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
136 except thread.error: |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
137 if verbose: |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
138 print 'platform does not support changing thread stack size' |
|
fc101cc354a7
test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38802
diff
changeset
|
139 return |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
140 self.test_various_ops() |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
141 threading.stack_size(0) |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
142 |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
143 def test_foreign_thread(self): |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
144 # Check that a "foreign" thread can use the threading module. |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
145 def f(mutex): |
|
57157
fbf41b1124d8
Issue #7282: Fix a memory leak when an RLock was used in a thread other
Antoine Pitrou <solipsis@pitrou.net>
parents:
57138
diff
changeset
|
146 # Calling current_thread() forces an entry for the foreign |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
147 # thread to get made in the threading._active map. |
|
57157
fbf41b1124d8
Issue #7282: Fix a memory leak when an RLock was used in a thread other
Antoine Pitrou <solipsis@pitrou.net>
parents:
57138
diff
changeset
|
148 threading.current_thread() |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
149 mutex.release() |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
150 |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
151 mutex = threading.Lock() |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
152 mutex.acquire() |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
153 tid = thread.start_new_thread(f, (mutex,)) |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
154 # Wait for the thread to finish. |
|
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
155 mutex.acquire() |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57157
diff
changeset
|
156 self.assertIn(tid, threading._active) |
|
58472
6ffcdafa0118
use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
157 self.assertIsInstance(threading._active[tid], threading._DummyThread) |
|
34363
35f31790c6f9
threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents:
34362
diff
changeset
|
158 del threading._active[tid] |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
159 |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
160 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently) |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
161 # exposed at the Python level. This test relies on ctypes to get at it. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
162 def test_PyThreadState_SetAsyncExc(self): |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
163 try: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
164 import ctypes |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
165 except ImportError: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
166 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
167 print "test_PyThreadState_SetAsyncExc can't import ctypes" |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
168 return # can't do anything |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
169 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
170 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
171 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
172 class AsyncExc(Exception): |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
173 pass |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
174 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
175 exception = ctypes.py_object(AsyncExc) |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
176 |
|
56598
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
177 # First check it works when setting the exception from the same thread. |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
178 tid = thread.get_ident() |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
179 |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
180 try: |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
181 result = set_async_exc(ctypes.c_long(tid), exception) |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
182 # The exception is async, so we might have to keep the VM busy until |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
183 # it notices. |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
184 while True: |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
185 pass |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
186 except AsyncExc: |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
187 pass |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
188 else: |
|
56600
25ef6f4818a0
Add a comment about unreachable code, and fix a typo
Antoine Pitrou <solipsis@pitrou.net>
parents:
56598
diff
changeset
|
189 # This code is unreachable but it reflects the intent. If we wanted |
|
25ef6f4818a0
Add a comment about unreachable code, and fix a typo
Antoine Pitrou <solipsis@pitrou.net>
parents:
56598
diff
changeset
|
190 # to be smarter the above loop wouldn't be infinite. |
|
56598
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
191 self.fail("AsyncExc not raised") |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
192 try: |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
193 self.assertEqual(result, 1) # one thread state modified |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
194 except UnboundLocalError: |
|
56600
25ef6f4818a0
Add a comment about unreachable code, and fix a typo
Antoine Pitrou <solipsis@pitrou.net>
parents:
56598
diff
changeset
|
195 # The exception was raised too quickly for us to get the result. |
|
56598
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
196 pass |
|
a0e41dfb972b
Add a test for same-thread asynchronous exceptions (see #1779233).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
197 |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
198 # `worker_started` is set by the thread when it's inside a try/except |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
199 # block waiting to catch the asynchronously set AsyncExc exception. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
200 # `worker_saw_exception` is set by the thread upon catching that |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
201 # exception. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
202 worker_started = threading.Event() |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
203 worker_saw_exception = threading.Event() |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
204 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
205 class Worker(threading.Thread): |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
206 def run(self): |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
207 self.id = thread.get_ident() |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
208 self.finished = False |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
209 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
210 try: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
211 while True: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
212 worker_started.set() |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
213 time.sleep(0.1) |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
214 except AsyncExc: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
215 self.finished = True |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
216 worker_saw_exception.set() |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
217 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
218 t = Worker() |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
219 t.daemon = True # so if this fails, we don't hang Python at shutdown |
|
39434
be107b04c252
test_PyThreadState_SetAsyncExc(): This is failing on some
Tim Peters <tim.peters@gmail.com>
parents:
39431
diff
changeset
|
220 t.start() |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
221 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
222 print " started worker thread" |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
223 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
224 # Try a thread id that doesn't make sense. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
225 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
226 print " trying nonsensical thread id" |
|
39434
be107b04c252
test_PyThreadState_SetAsyncExc(): This is failing on some
Tim Peters <tim.peters@gmail.com>
parents:
39431
diff
changeset
|
227 result = set_async_exc(ctypes.c_long(-1), exception) |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
228 self.assertEqual(result, 0) # no thread states modified |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
229 |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
230 # Now raise an exception in the worker thread. |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
231 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
232 print " waiting for worker thread to get started" |
|
52579
b5aa8aa78c0f
#1674032: return value of flag from Event.wait(). OKed by Guido.
Georg Brandl <georg@python.org>
parents:
49361
diff
changeset
|
233 ret = worker_started.wait() |
|
b5aa8aa78c0f
#1674032: return value of flag from Event.wait(). OKed by Guido.
Georg Brandl <georg@python.org>
parents:
49361
diff
changeset
|
234 self.assertTrue(ret) |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
235 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
236 print " verifying worker hasn't exited" |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
237 self.assertTrue(not t.finished) |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
238 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
239 print " attempting to raise asynch exception in worker" |
|
39434
be107b04c252
test_PyThreadState_SetAsyncExc(): This is failing on some
Tim Peters <tim.peters@gmail.com>
parents:
39431
diff
changeset
|
240 result = set_async_exc(ctypes.c_long(t.id), exception) |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
241 self.assertEqual(result, 1) # one thread state modified |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
242 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
243 print " waiting for worker to say it caught the exception" |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
244 worker_saw_exception.wait(timeout=10) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
245 self.assertTrue(t.finished) |
|
39431
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
246 if verbose: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
247 print " all OK -- joining worker" |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
248 if t.finished: |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
249 t.join() |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
250 # else the thread is still running, and we have no way to kill it |
|
de9e1a5dca93
Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents:
39389
diff
changeset
|
251 |
|
59200
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
252 def test_limbo_cleanup(self): |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
253 # Issue 7481: Failure to start thread should cleanup the limbo map. |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
254 def fail_new_thread(*args): |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
255 raise thread.error() |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
256 _start_new_thread = threading._start_new_thread |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
257 threading._start_new_thread = fail_new_thread |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
258 try: |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
259 t = threading.Thread(target=lambda: None) |
|
59219
c45805ba323f
Cleanup the test added in r78517 based on Ezio Melotti's feedback.
Gregory P. Smith <greg@mad-scientist.com>
parents:
59200
diff
changeset
|
260 self.assertRaises(thread.error, t.start) |
|
c45805ba323f
Cleanup the test added in r78517 based on Ezio Melotti's feedback.
Gregory P. Smith <greg@mad-scientist.com>
parents:
59200
diff
changeset
|
261 self.assertFalse( |
|
c45805ba323f
Cleanup the test added in r78517 based on Ezio Melotti's feedback.
Gregory P. Smith <greg@mad-scientist.com>
parents:
59200
diff
changeset
|
262 t in threading._limbo, |
|
c45805ba323f
Cleanup the test added in r78517 based on Ezio Melotti's feedback.
Gregory P. Smith <greg@mad-scientist.com>
parents:
59200
diff
changeset
|
263 "Failed to cleanup _limbo map on failure of Thread.start().") |
|
59200
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
264 finally: |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
265 threading._start_new_thread = _start_new_thread |
|
eabfa778b076
Issue #7481: When a threading.Thread failed to start it would leave the
Gregory P. Smith <greg@mad-scientist.com>
parents:
58472
diff
changeset
|
266 |
|
44063
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
267 def test_finalize_runnning_thread(self): |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
268 # Issue 1402: the PyGILState_Ensure / _Release functions may be called |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
269 # very late on python exit: on deallocation of a running thread for |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
270 # example. |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
271 try: |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
272 import ctypes |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
273 except ImportError: |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
274 if verbose: |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
275 print("test_finalize_with_runnning_thread can't import ctypes") |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
276 return # can't do anything |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
277 |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
278 import subprocess |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
279 rc = subprocess.call([sys.executable, "-c", """if 1: |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
280 import ctypes, sys, time, thread |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
281 |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
282 # This lock is used as a simple event variable. |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
283 ready = thread.allocate_lock() |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
284 ready.acquire() |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
285 |
|
44063
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
286 # Module globals are cleared before __del__ is run |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
287 # So we save the functions in class dict |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
288 class C: |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
289 ensure = ctypes.pythonapi.PyGILState_Ensure |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
290 release = ctypes.pythonapi.PyGILState_Release |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
291 def __del__(self): |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
292 state = self.ensure() |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
293 self.release(state) |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
294 |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
295 def waitingThread(): |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
296 x = C() |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
297 ready.release() |
|
44063
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
298 time.sleep(100) |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
299 |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
300 thread.start_new_thread(waitingThread, ()) |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
301 ready.acquire() # Be sure the other thread is waiting. |
|
44063
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
302 sys.exit(42) |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
303 """]) |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
304 self.assertEqual(rc, 42) |
|
4417ab5b351e
Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41960
diff
changeset
|
305 |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
306 def test_finalize_with_trace(self): |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
307 # Issue1733757 |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
308 # Avoid a deadlock when sys.settrace steps into threading._shutdown |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
309 import subprocess |
|
64854
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
310 p = subprocess.Popen([sys.executable, "-c", """if 1: |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
311 import sys, threading |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
312 |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
313 # A deadlock-killer, to prevent the |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
314 # testsuite to hang forever |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
315 def killer(): |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
316 import os, time |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
317 time.sleep(2) |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
318 print 'program blocked; aborting' |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
319 os._exit(2) |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
320 t = threading.Thread(target=killer) |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
321 t.daemon = True |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
322 t.start() |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
323 |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
324 # This is the trace function |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
325 def func(frame, event, arg): |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
326 threading.current_thread() |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
327 return func |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
328 |
|
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
329 sys.settrace(func) |
|
64854
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
330 """], |
|
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
331 stdout=subprocess.PIPE, |
|
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
332 stderr=subprocess.PIPE) |
|
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
333 stdout, stderr = p.communicate() |
|
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
334 rc = p.returncode |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
335 self.assertFalse(rc == 2, "interpreted was blocked") |
|
64854
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
336 self.assertTrue(rc == 0, |
|
6bd276f5d1bc
Merged revisions 84909-84913 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
61046
diff
changeset
|
337 "Unexpected error: " + repr(stderr)) |
|
46280
76f577a9ec03
#1733757: the interpreter would hang on shutdown, if the function set by sys.settrace
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46146
diff
changeset
|
338 |
|
56645
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
339 def test_join_nondaemon_on_shutdown(self): |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
340 # Issue 1722344 |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
341 # Raising SystemExit skipped threading._shutdown |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
342 import subprocess |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
343 p = subprocess.Popen([sys.executable, "-c", """if 1: |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
344 import threading |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
345 from time import sleep |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
346 |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
347 def child(): |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
348 sleep(1) |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
349 # As a non-daemon thread we SHOULD wake up and nothing |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
350 # should be torn down yet |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
351 print "Woke up, sleep function is:", sleep |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
352 |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
353 threading.Thread(target=child).start() |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
354 raise SystemExit |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
355 """], |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
356 stdout=subprocess.PIPE, |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
357 stderr=subprocess.PIPE) |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
358 stdout, stderr = p.communicate() |
|
56686
92dde282153d
Fix Windows buildbot failure
Antoine Pitrou <solipsis@pitrou.net>
parents:
56649
diff
changeset
|
359 self.assertEqual(stdout.strip(), |
|
92dde282153d
Fix Windows buildbot failure
Antoine Pitrou <solipsis@pitrou.net>
parents:
56649
diff
changeset
|
360 "Woke up, sleep function is: <built-in function sleep>") |
|
56649
3cfd20cd6459
Test wouldn't work in debug mode.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56645
diff
changeset
|
361 stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip() |
|
56645
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
362 self.assertEqual(stderr, "") |
|
12079c3d4b77
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
Antoine Pitrou <solipsis@pitrou.net>
parents:
56600
diff
changeset
|
363 |
|
44870
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
364 def test_enumerate_after_join(self): |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
365 # Try hard to trigger #1703448: a thread is still returned in |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
366 # threading.enumerate() after it has been join()ed. |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
367 enum = threading.enumerate |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
368 old_interval = sys.getcheckinterval() |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
369 try: |
|
45975
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
370 for i in xrange(1, 100): |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
371 # Try a couple times at each thread-switching interval |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
372 # to get more interleavings. |
|
8ef05aceef2b
Speed test_threading up from 14s to .5s, and avoid a deadlock on certain
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45478
diff
changeset
|
373 sys.setcheckinterval(i // 5) |
|
44870
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
374 t = threading.Thread(target=lambda: None) |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
375 t.start() |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
376 t.join() |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
377 l = enum() |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57157
diff
changeset
|
378 self.assertNotIn(t, l, |
|
44870
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
379 "#1703448 triggered after %d trials: %s" % (i, l)) |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
380 finally: |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
381 sys.setcheckinterval(old_interval) |
|
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
382 |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
383 def test_no_refcycle_through_target(self): |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
384 class RunSelfFunction(object): |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
385 def __init__(self, should_raise): |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
386 # The links in this refcycle from Thread back to self |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
387 # should be cleaned up when the thread completes. |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
388 self.should_raise = should_raise |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
389 self.thread = threading.Thread(target=self._run, |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
390 args=(self,), |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
391 kwargs={'yet_another':self}) |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
392 self.thread.start() |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
393 |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
394 def _run(self, other_ref, yet_another): |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
395 if self.should_raise: |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
396 raise SystemExit |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
397 |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
398 cyclic_object = RunSelfFunction(should_raise=False) |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
399 weak_cyclic_object = weakref.ref(cyclic_object) |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
400 cyclic_object.thread.join() |
|
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
401 del cyclic_object |
|
46146
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45975
diff
changeset
|
402 self.assertEquals(None, weak_cyclic_object(), |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45975
diff
changeset
|
403 msg=('%d references still around' % |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45975
diff
changeset
|
404 sys.getrefcount(weak_cyclic_object()))) |
|
45477
241cf81a0508
Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44870
diff
changeset
|
405 |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
406 raising_cyclic_object = RunSelfFunction(should_raise=True) |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
407 weak_raising_cyclic_object = weakref.ref(raising_cyclic_object) |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
408 raising_cyclic_object.thread.join() |
|
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
409 del raising_cyclic_object |
|
46146
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45975
diff
changeset
|
410 self.assertEquals(None, weak_raising_cyclic_object(), |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45975
diff
changeset
|
411 msg=('%d references still around' % |
|
10a39a662e69
Kill a race in test_threading in which the exception info in a thread finishing
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45975
diff
changeset
|
412 sys.getrefcount(weak_raising_cyclic_object()))) |
|
45478
9f183ae3458b
Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45477
diff
changeset
|
413 |
|
44870
fe7d423474de
- Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents:
44063
diff
changeset
|
414 |
|
56882
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
415 class ThreadJoinOnShutdown(BaseTestCase): |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
416 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
417 def _run_and_join(self, script): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
418 script = """if 1: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
419 import sys, os, time, threading |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
420 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
421 # a thread, which waits for the main program to terminate |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
422 def joiningfunc(mainthread): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
423 mainthread.join() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
424 print 'end of thread' |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
425 \n""" + script |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
426 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
427 import subprocess |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
428 p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
429 rc = p.wait() |
|
47999
45233d4c18d2
try to fix test_threading on the Windows bot
Benjamin Peterson <benjamin@python.org>
parents:
47985
diff
changeset
|
430 data = p.stdout.read().replace('\r', '') |
|
45233d4c18d2
try to fix test_threading on the Windows bot
Benjamin Peterson <benjamin@python.org>
parents:
47985
diff
changeset
|
431 self.assertEqual(data, "end of main\nend of thread\n") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
432 self.assertFalse(rc == 2, "interpreter was blocked") |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52593
diff
changeset
|
433 self.assertTrue(rc == 0, "Unexpected error") |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
434 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
435 def test_1_join_on_shutdown(self): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
436 # The usual case: on exit, wait for a non-daemon thread |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
437 script = """if 1: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
438 import os |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
439 t = threading.Thread(target=joiningfunc, |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
440 args=(threading.current_thread(),)) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
441 t.start() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
442 time.sleep(0.1) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
443 print 'end of main' |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
444 """ |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
445 self._run_and_join(script) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
446 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
447 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
448 def test_2_join_in_forked_process(self): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
449 # Like the test above, but from a forked interpreter |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
450 import os |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
451 if not hasattr(os, 'fork'): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
452 return |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
453 script = """if 1: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
454 childpid = os.fork() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
455 if childpid != 0: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
456 os.waitpid(childpid, 0) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
457 sys.exit(0) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
458 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
459 t = threading.Thread(target=joiningfunc, |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
460 args=(threading.current_thread(),)) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
461 t.start() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
462 print 'end of main' |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
463 """ |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
464 self._run_and_join(script) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
465 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
466 def test_3_join_in_forked_from_thread(self): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
467 # Like the test above, but fork() was called from a worker thread |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
468 # In the forked process, the main Thread object must be marked as stopped. |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
469 import os |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
470 if not hasattr(os, 'fork'): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
471 return |
|
49361
eed4d533332f
Works around issue3863: freebsd4/5/6 and os2emx are known to have OS bugs when
Gregory P. Smith <greg@mad-scientist.com>
parents:
48288
diff
changeset
|
472 # Skip platforms with known problems forking from a worker thread. |
|
eed4d533332f
Works around issue3863: freebsd4/5/6 and os2emx are known to have OS bugs when
Gregory P. Smith <greg@mad-scientist.com>
parents:
48288
diff
changeset
|
473 # See http://bugs.python.org/issue3863. |
|
eed4d533332f
Works around issue3863: freebsd4/5/6 and os2emx are known to have OS bugs when
Gregory P. Smith <greg@mad-scientist.com>
parents:
48288
diff
changeset
|
474 if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'): |
|
eed4d533332f
Works around issue3863: freebsd4/5/6 and os2emx are known to have OS bugs when
Gregory P. Smith <greg@mad-scientist.com>
parents:
48288
diff
changeset
|
475 print >>sys.stderr, ('Skipping test_3_join_in_forked_from_thread' |
|
eed4d533332f
Works around issue3863: freebsd4/5/6 and os2emx are known to have OS bugs when
Gregory P. Smith <greg@mad-scientist.com>
parents:
48288
diff
changeset
|
476 ' due to known OS bugs on'), sys.platform |
|
eed4d533332f
Works around issue3863: freebsd4/5/6 and os2emx are known to have OS bugs when
Gregory P. Smith <greg@mad-scientist.com>
parents:
48288
diff
changeset
|
477 return |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
478 script = """if 1: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
479 main_thread = threading.current_thread() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
480 def worker(): |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
481 childpid = os.fork() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
482 if childpid != 0: |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
483 os.waitpid(childpid, 0) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
484 sys.exit(0) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
485 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
486 t = threading.Thread(target=joiningfunc, |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
487 args=(main_thread,)) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
488 print 'end of main' |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
489 t.start() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
490 t.join() # Should not block: main_thread is already stopped |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
491 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
492 w = threading.Thread(target=worker) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
493 w.start() |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
494 """ |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
495 self._run_and_join(script) |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
496 |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
497 |
|
56882
5f61bd3aa427
Suppress transient refleaks in test_threading.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56686
diff
changeset
|
498 class ThreadingExceptionTests(BaseTestCase): |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
499 # A RuntimeError should be raised if Thread.start() is called |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
500 # multiple times. |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
501 def test_start_thread_again(self): |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
502 thread = threading.Thread() |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
503 thread.start() |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
504 self.assertRaises(RuntimeError, thread.start) |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
505 |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
506 def test_joining_current_thread(self): |
|
47598
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
507 current_thread = threading.current_thread() |
|
c349f9fbc704
give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents:
47438
diff
changeset
|
508 self.assertRaises(RuntimeError, current_thread.join); |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
509 |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
510 def test_joining_inactive_thread(self): |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
511 thread = threading.Thread() |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
512 self.assertRaises(RuntimeError, thread.join) |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
513 |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
514 def test_daemonize_active_thread(self): |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
515 thread = threading.Thread() |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
516 thread.start() |
|
48288
e143a8f35241
backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents:
48287
diff
changeset
|
517 self.assertRaises(RuntimeError, setattr, thread, "daemon", True) |
|
41960
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
518 |
|
588686d4c2cf
Patch #1731049: make threading.py use a proper "raise" when checking internal state, rather than assert statements (which get stripped out by -O).
Collin Winter <collinw@gmail.com>
parents:
39434
diff
changeset
|
519 |
|
57129
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
520 class LockTests(lock_tests.LockTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
521 locktype = staticmethod(threading.Lock) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
522 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
523 class RLockTests(lock_tests.RLockTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
524 locktype = staticmethod(threading.RLock) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
525 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
526 class EventTests(lock_tests.EventTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
527 eventtype = staticmethod(threading.Event) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
528 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
529 class ConditionAsRLockTests(lock_tests.RLockTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
530 # An Condition uses an RLock by default and exports its API. |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
531 locktype = staticmethod(threading.Condition) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
532 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
533 class ConditionTests(lock_tests.ConditionTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
534 condtype = staticmethod(threading.Condition) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
535 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
536 class SemaphoreTests(lock_tests.SemaphoreTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
537 semtype = staticmethod(threading.Semaphore) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
538 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
539 class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
540 semtype = staticmethod(threading.BoundedSemaphore) |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
541 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
542 |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
543 def test_main(): |
|
57129
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
544 test.test_support.run_unittest(LockTests, RLockTests, EventTests, |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
545 ConditionAsRLockTests, ConditionTests, |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
546 SemaphoreTests, BoundedSemaphoreTests, |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56882
diff
changeset
|
547 ThreadTests, |
|
47985
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
548 ThreadJoinOnShutdown, |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
549 ThreadingExceptionTests, |
|
09beb96ba20d
Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents:
47598
diff
changeset
|
550 ) |
|
34362
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
551 |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
552 if __name__ == "__main__": |
|
cc27b249755d
Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
553 test_main() |
