diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-05-04 18:02:30 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-05-04 18:02:30 (GMT) |
commit | 0d5e52d3469a310001afe50689f77ddba6d554d1 (patch) | |
tree | 0427334f46907890af771153a28f2c39441a8d89 /Lib/test/test_threading.py | |
parent | e548f5a0018fb44ed3a2a365ff0c8c87e397eedd (diff) | |
download | cpython-0d5e52d3469a310001afe50689f77ddba6d554d1.zip cpython-0d5e52d3469a310001afe50689f77ddba6d554d1.tar.gz cpython-0d5e52d3469a310001afe50689f77ddba6d554d1.tar.bz2 |
Issue #1856: Avoid crashes and lockups when daemon threads run while the
interpreter is shutting down; instead, these threads are now killed when
they try to take the GIL.
Diffstat (limited to 'Lib/test/test_threading.py')
-rw-r--r-- | Lib/test/test_threading.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 5f99b2e..1c946da 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -12,6 +12,7 @@ import unittest import weakref import os import subprocess +from test.script_helper import assert_python_ok from test import lock_tests @@ -463,7 +464,6 @@ class ThreadJoinOnShutdown(BaseTestCase): """ self._run_and_join(script) - @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") def test_2_join_in_forked_process(self): # Like the test above, but from a forked interpreter @@ -655,6 +655,49 @@ class ThreadJoinOnShutdown(BaseTestCase): output = "end of worker thread\nend of main thread\n" self.assertScriptHasOutput(script, output) + def test_6_daemon_threads(self): + # Check that a daemon thread cannot crash the interpreter on shutdown + # by manipulating internal structures that are being disposed of in + # the main thread. + script = """if True: + import os + import random + import sys + import time + import threading + + thread_has_run = set() + + def random_io(): + '''Loop for a while sleeping random tiny amounts and doing some I/O.''' + blank = b'x' * 200 + while True: + in_f = open(os.__file__, 'r') + stuff = in_f.read(200) + null_f = open(os.devnull, 'w') + null_f.write(stuff) + time.sleep(random.random() / 1995) + null_f.close() + in_f.close() + thread_has_run.add(threading.current_thread()) + + def main(): + count = 0 + for _ in range(40): + new_thread = threading.Thread(target=random_io) + new_thread.daemon = True + new_thread.start() + count += 1 + while len(thread_has_run) < count: + time.sleep(0.001) + # Trigger process shutdown + sys.exit(0) + + main() + """ + rc, out, err = assert_python_ok('-c', script) + self.assertFalse(err) + class ThreadingExceptionTests(BaseTestCase): # A RuntimeError should be raised if Thread.start() is called |