]> granicus.if.org Git - python/commitdiff
bpo-31061: fix crash in asyncio speedup module (GH-2984)
authorINADA Naoki <methane@users.noreply.github.com>
Wed, 2 Aug 2017 07:50:39 +0000 (16:50 +0900)
committerGitHub <noreply@github.com>
Wed, 2 Aug 2017 07:50:39 +0000 (16:50 +0900)
(cherry picked from commit de34cbe9cdaaf7b85fed86f99c2fd071e1a7b1d2)

Lib/test/test_asyncio/test_futures.py
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Library/2017-08-01-09-32-58.bpo-31061.husAYX.rst [new file with mode: 0644]
Modules/_asynciomodule.c

index ce657fc1b6a058e321deb77a69dfbb437f3403a4..ebedfec7fa3f5f9162597ce7a83beef42b753715 100644 (file)
@@ -1,6 +1,7 @@
 """Tests for futures.py."""
 
 import concurrent.futures
+import gc
 import re
 import sys
 import threading
@@ -19,9 +20,11 @@ except ImportError:
 def _fakefunc(f):
     return f
 
+
 def first_cb():
     pass
 
+
 def last_cb():
     pass
 
@@ -483,6 +486,15 @@ class BaseFutureTests:
                           Exception("elephant"), Exception("elephant"))
         self.assertRaises(TypeError, fi.throw, list)
 
+    def test_future_del_collect(self):
+        class Evil:
+            def __del__(self):
+                gc.collect()
+
+        for i in range(100):
+            fut = self._new_future(loop=self.loop)
+            fut.set_result(Evil())
+
 
 @unittest.skipUnless(hasattr(futures, '_CFuture'),
                      'requires the C _asyncio module')
index 195a1ed40770ddaf94eb7584a61fd031cb5b3113..243faf6b04995875f2a76ca8040635c06a086b44 100644 (file)
@@ -3,6 +3,7 @@
 import collections
 import contextlib
 import functools
+import gc
 import io
 import os
 import re
@@ -92,6 +93,20 @@ class BaseTaskTests:
         self.loop.set_task_factory(self.new_task)
         self.loop.create_future = lambda: self.new_future(self.loop)
 
+    def test_task_del_collect(self):
+        class Evil:
+            def __del__(self):
+                gc.collect()
+
+        @asyncio.coroutine
+        def run():
+            return Evil()
+
+        self.loop.run_until_complete(
+            asyncio.gather(*[
+                self.new_task(self.loop, run()) for _ in range(100)
+            ], loop=self.loop))
+
     def test_other_loop_future(self):
         other_loop = asyncio.new_event_loop()
         fut = self.new_future(other_loop)
diff --git a/Misc/NEWS.d/next/Library/2017-08-01-09-32-58.bpo-31061.husAYX.rst b/Misc/NEWS.d/next/Library/2017-08-01-09-32-58.bpo-31061.husAYX.rst
new file mode 100644 (file)
index 0000000..650e5f9
--- /dev/null
@@ -0,0 +1 @@
+Fixed a crash when using asyncio and threads.
index 8fbd565dd9c26c0126d7969c651381401b6b721f..c48cbbc263dddaaa760fe3e97fffde4fe2ec4678 100644 (file)
@@ -971,6 +971,8 @@ FutureObj_dealloc(PyObject *self)
         }
     }
 
+    PyObject_GC_UnTrack(self);
+
     if (fut->fut_weakreflist != NULL) {
         PyObject_ClearWeakRefs(self);
     }
@@ -1845,6 +1847,8 @@ TaskObj_dealloc(PyObject *self)
         }
     }
 
+    PyObject_GC_UnTrack(self);
+
     if (task->task_weakreflist != NULL) {
         PyObject_ClearWeakRefs(self);
     }