I see "InvalidStateError: CANCELLED: Future<CANCELLED>” exception in my 
production system.
It doesn’t really affect application logic, just annoying. also it is very hard 
to understand where exception from.
so i propose to add safe_set_result() method to Future that will check status 
before setting result.
code review is here https://codereview.appspot.com/69870048/

here is test case:

    @mock.patch('asyncio.base_events.logger')
    def test_ctor_with_cancelled_waiter(self,m_log):
        fut = asyncio.Future(loop=self.loop)

        @asyncio.coroutine
        def foo():
            _SelectorSocketTransport(
                self.loop, self.sock, self.protocol, fut)
            yield from fut

        task = asyncio.async(foo(), loop=self.loop)
        test_utils.run_once(self.loop)
        task.cancel()
        test_utils.run_briefly(self.loop)
        self.assertTrue(fut.cancelled())

        # exception should not be raised
        self.assertFalse(m_log.error.called)

Reply via email to