Closes #21886, #21447: Fix a race condition in asyncio when setting the result

of a Future with call_soon(). Add an helper, a private method, to set the
result only if the future was not cancelled.
This commit is contained in:
Victor Stinner 2014-07-05 15:29:41 +02:00
parent 5021cb553c
commit a9acbe82e7
9 changed files with 31 additions and 7 deletions

View file

@ -343,6 +343,12 @@ class FutureTests(test_utils.TestCase):
message = m_log.error.call_args[0][0]
self.assertRegex(message, re.compile(regex, re.DOTALL))
def test_set_result_unless_cancelled(self):
fut = asyncio.Future(loop=self.loop)
fut.cancel()
fut._set_result_unless_cancelled(2)
self.assertTrue(fut.cancelled())
class FutureDoneCallbackTests(test_utils.TestCase):

View file

@ -211,6 +211,10 @@ class TaskTests(test_utils.TestCase):
coro = ('%s() at %s:%s'
% (coro_qualname, code.co_filename, code.co_firstlineno))
# test repr(CoroWrapper)
if coroutines._DEBUG:
self.assertEqual(repr(gen), '<CoroWrapper %s>' % coro)
# test pending Task
t = asyncio.Task(gen, loop=self.loop)
t.add_done_callback(Dummy())