]> granicus.if.org Git - python/commitdiff
bpo-31516: current_thread() should not return a dummy thread at shutdown (#3673)
authorAntoine Pitrou <pitrou@free.fr>
Mon, 2 Oct 2017 14:42:15 +0000 (16:42 +0200)
committerGitHub <noreply@github.com>
Mon, 2 Oct 2017 14:42:15 +0000 (16:42 +0200)
bpo-31516: current_thread() should not return a dummy thread at shutdown

Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst [new file with mode: 0644]

index f7c3680bda379b5ab17fe7d5a16f99ed64649857..6e1ae06f7927e4f930aac9ad23efa1846dceaf74 100644 (file)
@@ -547,6 +547,35 @@ class ThreadTests(BaseTestCase):
         self.assertEqual(err, b"")
         self.assertEqual(data, "Thread-1\nTrue\nTrue\n")
 
+    def test_main_thread_during_shutdown(self):
+        # bpo-31516: current_thread() should still point to the main thread
+        # at shutdown
+        code = """if 1:
+            import gc, threading
+
+            main_thread = threading.current_thread()
+            assert main_thread is threading.main_thread()  # sanity check
+
+            class RefCycle:
+                def __init__(self):
+                    self.cycle = self
+
+                def __del__(self):
+                    print("GC:",
+                          threading.current_thread() is main_thread,
+                          threading.main_thread() is main_thread,
+                          threading.enumerate() == [main_thread])
+
+            RefCycle()
+            gc.collect()  # sanity check
+            x = RefCycle()
+        """
+        _, out, err = assert_python_ok("-c", code)
+        data = out.decode()
+        self.assertEqual(err, b"")
+        self.assertEqual(data.splitlines(),
+                         ["GC: True True True"] * 2)
+
     def test_tstate_lock(self):
         # Test an implementation detail of Thread objects.
         started = _thread.allocate_lock()
index e4bf974495b39269aafffcfad39886106b77bd93..418116faceb755b7a670fdbed99d352ee2c45351 100644 (file)
@@ -1158,8 +1158,8 @@ class Timer(Thread):
             self.function(*self.args, **self.kwargs)
         self.finished.set()
 
+
 # Special thread class to represent the main thread
-# This is garbage collected through an exit handler
 
 class _MainThread(Thread):
 
@@ -1272,7 +1272,6 @@ def _shutdown():
     while t:
         t.join()
         t = _pickSomeNonDaemonThread()
-    _main_thread._delete()
 
 def _pickSomeNonDaemonThread():
     for t in enumerate():
diff --git a/Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst b/Misc/NEWS.d/next/Library/2017-09-20-18-43-01.bpo-31516.23Yuq3.rst
new file mode 100644 (file)
index 0000000..af48d15
--- /dev/null
@@ -0,0 +1 @@
+``threading.current_thread()`` should not return a dummy thread at shutdown.