Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_asyncio/test_futures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for futures.py."""

import concurrent.futures
import gc
import re
import sys
import threading
Expand All @@ -19,9 +20,11 @@
def _fakefunc(f):
return f


def first_cb():
pass


def last_cb():
pass

Expand Down Expand Up @@ -483,6 +486,15 @@ def test_future_iter_throw(self):
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')
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import contextlib
import functools
import io
import gc
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another nit: 'gc' import should be before 'functools' -- we sort imports alphabetically in CPython.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be worth running a linter on CI to catch these

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, good idea. BTW help is always welcome by python/core-workflow. You're an awesome contributor, we'd love you to continue! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're too kind :) Will keep that in mind if we run into any other cpython issues.

import os
import re
import sys
Expand Down Expand Up @@ -91,6 +92,17 @@ def setUp(self):
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()

yield from asyncio.gather(*[asyncio.Task(run()) for _ in range(100)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test won't run. yield from makes test_task_del_collect() a generator and test suite doesn't know how to run them.

Please change to

self.loop.run_until_complete(
    asyncio.gather(*[asyncio.Task(run()) for _ in range(100)]))


def test_other_loop_future(self):
other_loop = asyncio.new_event_loop()
fut = self.new_future(other_loop)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a crash when using asyncio and threads.
4 changes: 4 additions & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ FutureObj_dealloc(PyObject *self)
}
}

PyObject_GC_UnTrack(self);

if (fut->fut_weakreflist != NULL) {
PyObject_ClearWeakRefs(self);
}
Expand Down Expand Up @@ -1846,6 +1848,8 @@ TaskObj_dealloc(PyObject *self)
}
}

PyObject_GC_UnTrack(self);

if (task->task_weakreflist != NULL) {
PyObject_ClearWeakRefs(self);
}
Expand Down