annotate Lib/test/test_threading.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 0eb56a8e6262
children 2cdec848b74c
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
57159
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
14 from test import lock_tests
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
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'
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 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
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()
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 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
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
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
59 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
60
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
61 # 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
62 # done.
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
63 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
64 # 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
65 # 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
66 NUMTASKS = 10
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
67
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
68 # 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
69 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
70 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
71 numrunning = Counter()
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 threads = []
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
74
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
75 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
76 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
77 threads.append(t)
48287
1d29e53ce2af change threading.getIdent to a property
Benjamin Peterson <benjamin@python.org>
parents: 47999
diff changeset
78 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
79 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
80 t.start()
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
81
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
82 if verbose:
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
83 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
84 for t in threads:
34363
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
85 t.join(NUMTASKS)
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
86 self.assert_(not t.is_alive())
48287
1d29e53ce2af change threading.getIdent to a property
Benjamin Peterson <benjamin@python.org>
parents: 47999
diff changeset
87 self.failIfEqual(t.ident, 0)
52596
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
88 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
89 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
90 if verbose:
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
91 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
92 self.assertEqual(numrunning.get(), 0)
19405
9de83dda9f09 add a few test cases for threading module.
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
93
52596
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
94 def test_ident_of_no_threading_threads(self):
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
95 # The ident still must work for the main thread and dummy threads.
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
96 self.assertFalse(threading.currentThread().ident is None)
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
97 def f():
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
98 ident.append(threading.currentThread().ident)
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
99 done.set()
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
100 done = threading.Event()
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
101 ident = []
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
102 thread.start_new_thread(f, ())
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
103 done.wait()
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
104 self.assertFalse(ident[0] is None)
57140
3a0e69001215 Merged revisions 76148 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56781
diff changeset
105 # Kill the "immortal" _DummyThread
3a0e69001215 Merged revisions 76148 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56781
diff changeset
106 del threading._active[ident[0]]
52596
21ba0b3fe819 Merged revisions 70897 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49361
diff changeset
107
38802
6e0c0c7cf068 Increase the small thread stack size to get the test
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38794
diff changeset
108 # 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
109 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
110 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
111 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
112 try:
fc101cc354a7 test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38802
diff changeset
113 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
114 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
115 if verbose:
fc101cc354a7 test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38802
diff changeset
116 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
117 return
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
118 self.test_various_ops()
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
119 threading.stack_size(0)
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
120
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
121 # 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
122 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
123 if verbose:
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
124 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
125 try:
fc101cc354a7 test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38802
diff changeset
126 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
127 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
128 if verbose:
fc101cc354a7 test_threading now skips testing alternate thread stack sizes on
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38802
diff changeset
129 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
130 return
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
131 self.test_various_ops()
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
132 threading.stack_size(0)
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
133
34363
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
134 def test_foreign_thread(self):
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
135 # 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
136 def f(mutex):
57159
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
137 # 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
138 # thread to get made in the threading._active map.
57159
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
139 threading.current_thread()
34363
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
140 mutex.release()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
141
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
142 mutex = threading.Lock()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
143 mutex.acquire()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
144 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
145 # Wait for the thread to finish.
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
146 mutex.acquire()
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
147 self.assert_(tid in threading._active)
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
148 self.assert_(isinstance(threading._active[tid],
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
149 threading._DummyThread))
35f31790c6f9 threading._DummyThread.__init__(): document obscure new code.
Tim Peters <tim.peters@gmail.com>
parents: 34362
diff changeset
150 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
151
39431
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
152 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
153 # 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
154 def test_PyThreadState_SetAsyncExc(self):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
155 try:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
156 import ctypes
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
157 except ImportError:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
158 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
159 print "test_PyThreadState_SetAsyncExc can't import ctypes"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
160 return # can't do anything
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 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
163
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
164 class AsyncExc(Exception):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
165 pass
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 exception = ctypes.py_object(AsyncExc)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
168
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
169 # `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
170 # block waiting to catch the asynchronously set AsyncExc exception.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
171 # `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
172 # exception.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
173 worker_started = threading.Event()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
174 worker_saw_exception = threading.Event()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
175
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
176 class Worker(threading.Thread):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
177 def run(self):
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
178 self.id = thread.get_ident()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
179 self.finished = False
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
180
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
181 try:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
182 while True:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
183 worker_started.set()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
184 time.sleep(0.1)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
185 except AsyncExc:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
186 self.finished = True
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
187 worker_saw_exception.set()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
188
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
189 t = Worker()
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
190 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
191 t.start()
39431
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
192 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
193 print " started worker thread"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
194
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
195 # Try a thread id that doesn't make sense.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
196 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
197 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
198 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
199 self.assertEqual(result, 0) # no thread states modified
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
200
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
201 # Now raise an exception in the worker thread.
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
202 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
203 print " waiting for worker thread to get started"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
204 worker_started.wait()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
205 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
206 print " verifying worker hasn't exited"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
207 self.assert_(not t.finished)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
208 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
209 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
210 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
211 self.assertEqual(result, 1) # one thread state modified
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
212 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
213 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
214 worker_saw_exception.wait(timeout=10)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
215 self.assert_(t.finished)
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
216 if verbose:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
217 print " all OK -- joining worker"
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
218 if t.finished:
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
219 t.join()
de9e1a5dca93 Followup to bug #1069160.
Tim Peters <tim.peters@gmail.com>
parents: 39389
diff changeset
220 # 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
221
44063
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
222 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
223 # 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
224 # 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
225 # example.
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
226 try:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
227 import ctypes
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
228 except ImportError:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
229 if verbose:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
230 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
231 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
232
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
233 import subprocess
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
234 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
235 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
236
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
237 # 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
238 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
239 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
240
44063
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
241 # 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
242 # 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
243 class C:
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
244 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
245 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
246 def __del__(self):
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
247 state = self.ensure()
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
248 self.release(state)
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
249
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
250 def waitingThread():
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
251 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
252 ready.release()
44063
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
253 time.sleep(100)
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
254
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
255 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
256 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
257 sys.exit(42)
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
258 """])
4417ab5b351e Issue #1402: PyInterpreterState_Clear() may still invoke user code
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 41960
diff changeset
259 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
260
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
261 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
262 # 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
263 # 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
264 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
265 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
266 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
267
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 # 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
269 # 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
270 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
271 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
272 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
273 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
274 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
275 t = threading.Thread(target=killer)
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
276 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
277 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
278
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 # 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
280 def func(frame, event, arg):
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
281 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
282 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
283
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 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
285 """])
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.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
287 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
288
56781
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
289 def test_join_nondaemon_on_shutdown(self):
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
290 # Issue 1722344
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
291 # Raising SystemExit skipped threading._shutdown
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
292 import subprocess
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
293 p = subprocess.Popen([sys.executable, "-c", """if 1:
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
294 import threading
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
295 from time import sleep
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
296
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
297 def child():
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
298 sleep(1)
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
299 # As a non-daemon thread we SHOULD wake up and nothing
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
300 # should be torn down yet
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
301 print "Woke up, sleep function is:", sleep
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
302
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
303 threading.Thread(target=child).start()
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
304 raise SystemExit
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
305 """],
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
306 stdout=subprocess.PIPE,
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
307 stderr=subprocess.PIPE)
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
308 stdout, stderr = p.communicate()
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
309 self.assertEqual(stdout.strip(),
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
310 "Woke up, sleep function is: <built-in function sleep>")
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
311 stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
2abc3b4bce7c Merged revisions 75570,75574,75624 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 52596
diff changeset
312 self.assertEqual(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
313
44870
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
314 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
315 # 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
316 # 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
317 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
318 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
319 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
320 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
321 # 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
322 # 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
323 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
324 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
325 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
326 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
327 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
328 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
329 "#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
330 finally:
fe7d423474de - Fix Issue #1703448: A joined thread could show up in the
Gregory P. Smith <greg@mad-scientist.com>
parents: 44063
diff changeset
331 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
332
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
333 def test_no_refcycle_through_target(self):
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
334 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
335 def __init__(self, should_raise):
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
336 # The links in this refcycle from Thread back to self
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
337 # 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
338 self.should_raise = should_raise
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
339 self.thread = threading.Thread(target=self._run,
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
340 args=(self,),
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
341 kwargs={'yet_another':self})
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
342 self.thread.start()
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
343
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
344 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
345 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
346 raise SystemExit
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
347
45478
9f183ae3458b Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45477
diff changeset
348 cyclic_object = RunSelfFunction(should_raise=False)
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
349 weak_cyclic_object = weakref.ref(cyclic_object)
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
350 cyclic_object.thread.join()
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
351 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
352 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
353 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
354 sys.getrefcount(weak_cyclic_object())))
45477
241cf81a0508 Prevent classes like:
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44870
diff changeset
355
45478
9f183ae3458b Followup to r61011: Also avoid the reference cycle when the Thread's target
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45477
diff changeset
356 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
357 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
358 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
359 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
360 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
361 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
362 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
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
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
365 class ThreadJoinOnShutdown(unittest.TestCase):
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
366
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
367 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
368 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
369 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
370
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
371 # 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
372 def joiningfunc(mainthread):
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
373 mainthread.join()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
374 print 'end of thread'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
375 \n""" + script
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
376
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
377 import subprocess
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
378 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
379 rc = p.wait()
47999
45233d4c18d2 try to fix test_threading on the Windows bot
Benjamin Peterson <benjamin@python.org>
parents: 47985
diff changeset
380 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
381 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
382 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
383 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
384
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
385 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
386 # 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
387 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
388 import os
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
389 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
390 args=(threading.current_thread(),))
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
391 t.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
392 time.sleep(0.1)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
393 print 'end of main'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
394 """
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
395 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
396
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
397
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
398 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
399 # 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
400 import os
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
401 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
402 return
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 childpid = os.fork()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
405 if childpid != 0:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
406 os.waitpid(childpid, 0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
407 sys.exit(0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
408
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
409 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
410 args=(threading.current_thread(),))
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
411 t.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
412 print 'end of main'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
413 """
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
414 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
415
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
416 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
417 # 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
418 # 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
419 import os
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
420 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
421 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
422 # 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
423 # 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
424 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
425 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
426 ' 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
427 return
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
428 script = """if 1:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
429 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
430 def worker():
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
431 childpid = os.fork()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
432 if childpid != 0:
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
433 os.waitpid(childpid, 0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
434 sys.exit(0)
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
435
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
436 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
437 args=(main_thread,))
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
438 print 'end of main'
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
439 t.start()
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
440 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
441
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
442 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
443 w.start()
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
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
448 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
449 # 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
450 # 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
451 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
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 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
454 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
455
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 def test_joining_current_thread(self):
47598
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
457 current_thread = threading.current_thread()
c349f9fbc704 give the threading API PEP 8 names
Benjamin Peterson <benjamin@python.org>
parents: 47438
diff changeset
458 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
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 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
461 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
462 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
463
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
464 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
465 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
466 thread.start()
48288
e143a8f35241 backport threading property changes
Benjamin Peterson <benjamin@python.org>
parents: 48287
diff changeset
467 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
468
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
469
57159
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
470 class LockTests(lock_tests.LockTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
471 locktype = staticmethod(threading.Lock)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
472
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
473 class RLockTests(lock_tests.RLockTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
474 locktype = staticmethod(threading.RLock)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
475
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
476 class EventTests(lock_tests.EventTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
477 eventtype = staticmethod(threading.Event)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
478
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
479 class ConditionAsRLockTests(lock_tests.RLockTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
480 # An Condition uses an RLock by default and exports its API.
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
481 locktype = staticmethod(threading.Condition)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
482
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
483 class ConditionTests(lock_tests.ConditionTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
484 condtype = staticmethod(threading.Condition)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
485
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
486 class SemaphoreTests(lock_tests.SemaphoreTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
487 semtype = staticmethod(threading.Semaphore)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
488
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
489 class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
490 semtype = staticmethod(threading.BoundedSemaphore)
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
491
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
492
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
493 def test_main():
57159
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
494 test.test_support.run_unittest(LockTests, RLockTests, EventTests,
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
495 ConditionAsRLockTests, ConditionTests,
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
496 SemaphoreTests, BoundedSemaphoreTests,
0eb56a8e6262 Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 57140
diff changeset
497 ThreadTests,
47985
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
498 ThreadJoinOnShutdown,
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
499 ThreadingExceptionTests,
09beb96ba20d Apply patch for 874900: threading module can deadlock after fork
Jesse Noller <jnoller@gmail.com>
parents: 47598
diff changeset
500 )
34362
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
501
cc27b249755d Converted to a unittest. Added checks that the bounded semaphore actually
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
502 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
503 test_main()