annotate Lib/test/test_threading.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 587fcfbe08b6
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
8 import threading
34363
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
9 import thread
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
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
14 # 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
15 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
16 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
17 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
18 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
19 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
20 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
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 get(self):
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
23 return self.value
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
24
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
25 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
26 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
27 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
28 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
29 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
30 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
31 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
32
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
33 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
34 delay = random.random() / 10000.0
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
35 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
36 print 'task %s will run for %.1f usec' % (
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
37 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
38
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
39 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
40 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
41 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
42 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
43 print self.nrunning.get(), 'tasks are running'
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 self.testcase.assert_(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
45
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
46 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
47 if verbose:
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
48 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
49
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
50 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
51 self.nrunning.dec()
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 self.testcase.assert_(self.nrunning.get() >= 0)
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 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
54 print '%s is finished. %d tasks are running' % (
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
55 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
56
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
57 class ThreadTests(unittest.TestCase):
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
58
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
59 # 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
60 # done.
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
61 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
62 # 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
63 # 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
64 NUMTASKS = 10
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
65
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
66 # 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
67 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
68 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
69 numrunning = Counter()
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
70
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
71 threads = []
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
72
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
73 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
74 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
75 threads.append(t)
48287
1d29e53ce2af change threading.getIdent to a property
Benjamin Peterson <benjamin@python.org>
parents: 47999
diff changeset
76 self.failUnlessEqual(t.ident, None)
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
77 self.assert_(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
78 t.start()
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
79
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
80 if verbose:
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
81 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
82 for t in threads:
34363
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
83 t.join(NUMTASKS)
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
84 self.assert_(not t.is_alive())
48287
1d29e53ce2af change threading.getIdent to a property
Benjamin Peterson <benjamin@python.org>
parents: 47999
diff changeset
85 self.failIfEqual(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
86 self.assertFalse(t.ident is None)
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
87 self.assert_(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
88 if verbose:
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
89 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
90 self.assertEqual(numrunning.get(), 0)
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
91
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
92 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
93 # 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 self.assertFalse(ident[0] 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
103
38802
6e0c0c7cf068 Increase the small thread stack size to get the test
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38794
diff changeset
104 # 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
105 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
106 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
107 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
108 try:
fc101cc354a7 test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38802
diff changeset
109 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
110 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
111 if verbose:
fc101cc354a7 test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38802
diff changeset
112 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
113 return
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
114 self.test_various_ops()
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
115 threading.stack_size(0)
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
116
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
117 # 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
118 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
119 if verbose:
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
120 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
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(0x100000)
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
34363
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
130 def test_foreign_thread(self):
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
131 # 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
132 def f(mutex):
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
133 # Acquiring an RLock forces an entry for the foreign
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
134 # thread to get made in the threading._active map.
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
135 r = threading.RLock()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
136 r.acquire()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
137 r.release()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
138 mutex.release()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
139
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
140 mutex = threading.Lock()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
141 mutex.acquire()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
142 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
143 # Wait for the thread to finish.
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
144 mutex.acquire()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
145 self.assert_(tid in threading._active)
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
146 self.assert_(isinstance(threading._active[tid],
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
147 threading._DummyThread))
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
148 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
149
39431
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
150 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
151 # 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
152 def test_PyThreadState_SetAsyncExc(self):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
153 try:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
154 import ctypes
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
155 except ImportError:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
156 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
157 print "test_PyThreadState_SetAsyncExc can't import ctypes"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
158 return # can't do anything
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
159
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
160 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
161
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
162 class AsyncExc(Exception):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
163 pass
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
164
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
165 exception = ctypes.py_object(AsyncExc)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
166
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
167 # `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
168 # block waiting to catch the asynchronously set AsyncExc exception.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
169 # `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
170 # exception.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
171 worker_started = threading.Event()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
172 worker_saw_exception = threading.Event()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
173
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
174 class Worker(threading.Thread):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
175 def run(self):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
176 self.id = thread.get_ident()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
177 self.finished = False
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
178
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
179 try:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
180 while True:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
181 worker_started.set()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
182 time.sleep(0.1)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
183 except AsyncExc:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
184 self.finished = True
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
185 worker_saw_exception.set()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
186
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
187 t = Worker()
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
188 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
189 t.start()
39431
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
190 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
191 print " started worker thread"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
192
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
193 # Try a thread id that doesn't make sense.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
194 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
195 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
196 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
197 self.assertEqual(result, 0) # no thread states modified
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
198
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
199 # Now raise an exception in the worker thread.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
200 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
201 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
202 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
203 self.assertTrue(ret)
39431
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
204 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
205 print " verifying worker hasn't exited"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
206 self.assert_(not t.finished)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
207 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
208 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
209 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
210 self.assertEqual(result, 1) # one thread state modified
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
211 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
212 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
213 worker_saw_exception.wait(timeout=10)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
214 self.assert_(t.finished)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
215 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
216 print " all OK -- joining worker"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
217 if t.finished:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
218 t.join()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
219 # 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
220
44063
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
221 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
222 # 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
223 # 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
224 # example.
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
225 try:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
226 import ctypes
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
227 except ImportError:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
228 if verbose:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
229 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
230 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
231
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
232 import subprocess
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
233 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
234 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
235
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
236 # 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
237 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
238 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
239
44063
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
240 # 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
241 # 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
242 class C:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
243 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
244 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
245 def __del__(self):
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
246 state = self.ensure()
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
247 self.release(state)
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
248
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
249 def waitingThread():
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
250 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
251 ready.release()
44063
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
252 time.sleep(100)
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
253
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
254 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
255 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
256 sys.exit(42)
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
257 """])
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
258 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
259
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
260 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
261 # 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
262 # 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
263 import subprocess
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
264 rc = subprocess.call([sys.executable, "-c", """if 1:
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
265 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
266
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
267 # 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
268 # 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
269 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
270 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
271 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
272 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
273 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
274 t = threading.Thread(target=killer)
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
275 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
276 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
277
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
278 # 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
279 def func(frame, event, arg):
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
280 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
281 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
282
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
283 sys.settrace(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
284 """])
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
285 self.failIf(rc == 2, "interpreted was blocked")
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
286 self.failUnless(rc == 0, "Unexpected error")
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
287
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
288
44870
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
289 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
290 # 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
291 # 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
292 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
293 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
294 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
295 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
296 # 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
297 # 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
298 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
299 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
300 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
301 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
302 l = enum()
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
303 self.assertFalse(t in l,
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
304 "#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
305 finally:
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
306 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
307
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
308 def test_no_refcycle_through_target(self):
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
309 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
310 def __init__(self, should_raise):
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
311 # The links in this refcycle from Thread back to self
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
312 # 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
313 self.should_raise = should_raise
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
314 self.thread = threading.Thread(target=self._run,
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
315 args=(self,),
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
316 kwargs={'yet_another':self})
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
317 self.thread.start()
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
318
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
319 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
320 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
321 raise SystemExit
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
322
45478
9f183ae3458b Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45477
diff changeset
323 cyclic_object = RunSelfFunction(should_raise=False)
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
324 weak_cyclic_object = weakref.ref(cyclic_object)
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
325 cyclic_object.thread.join()
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
326 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
327 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
328 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
329 sys.getrefcount(weak_cyclic_object())))
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
330
45478
9f183ae3458b Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45477
diff changeset
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338
44870
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
339
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
340 class ThreadJoinOnShutdown(unittest.TestCase):
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
341
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
342 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
343 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
344 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
345
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
346 # 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
347 def joiningfunc(mainthread):
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
348 mainthread.join()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
349 print 'end of thread'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
350 \n""" + script
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
351
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
352 import subprocess
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
353 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
354 rc = p.wait()
47999
45233d4c18d2 try to fix test_threading on the Windows bot
Benjamin Peterson <benjamin@python.org>
parents: 47985
diff changeset
355 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
356 self.assertEqual(data, "end of main\nend of thread\n")
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
357 self.failIf(rc == 2, "interpreter was blocked")
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
358 self.failUnless(rc == 0, "Unexpected error")
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
359
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
360 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
361 # 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
362 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
363 import os
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
364 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
365 args=(threading.current_thread(),))
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
366 t.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
367 time.sleep(0.1)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
368 print 'end of main'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
369 """
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
370 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
371
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
372
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
373 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
374 # 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
375 import os
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
376 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
377 return
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
378 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
379 childpid = os.fork()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
380 if childpid != 0:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
381 os.waitpid(childpid, 0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
382 sys.exit(0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
383
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
384 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
385 args=(threading.current_thread(),))
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
386 t.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
387 print 'end of main'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
388 """
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
389 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
390
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
391 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
392 # 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
393 # 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
394 import os
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
395 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
396 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
397 # 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
398 # 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
399 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
400 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
401 ' 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
402 return
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
403 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
404 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
405 def worker():
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
406 childpid = os.fork()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
407 if childpid != 0:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
408 os.waitpid(childpid, 0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
409 sys.exit(0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
410
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
411 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
412 args=(main_thread,))
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
413 print 'end of main'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
414 t.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
415 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
416
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
417 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
418 w.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
419 """
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
420 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
421
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
422
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
423 class ThreadingExceptionTests(unittest.TestCase):
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
424 # 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
425 # 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
426 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
427 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
428 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
429 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
430
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
431 def test_releasing_unacquired_rlock(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
432 rlock = threading.RLock()
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
433 self.assertRaises(RuntimeError, rlock.release)
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
434
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
435 def test_waiting_on_unacquired_condition(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
436 cond = threading.Condition()
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
437 self.assertRaises(RuntimeError, cond.wait)
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
438
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
439 def test_notify_on_unacquired_condition(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
440 cond = threading.Condition()
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
441 self.assertRaises(RuntimeError, cond.notify)
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
442
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
443 def test_semaphore_with_negative_value(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
444 self.assertRaises(ValueError, threading.Semaphore, value = -1)
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
445 self.assertRaises(ValueError, threading.Semaphore, value = -sys.maxint)
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
446
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
447 def test_joining_current_thread(self):
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
448 current_thread = threading.current_thread()
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
449 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
450
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
451 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
452 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
453 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
454
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
455 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
456 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
457 thread.start()
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
458 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
459
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
460
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
461 def test_main():
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
462 test.test_support.run_unittest(ThreadTests,
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
463 ThreadJoinOnShutdown,
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
464 ThreadingExceptionTests,
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
465 )
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
466
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
467 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
468 test_main()