Skip to content

Commit d24c828

Browse files
authored
bpo-30508: Don't log exceptions if Task/Future "cancel()" method was called. (#2110)
1 parent ea8b348 commit d24c828

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

Lib/asyncio/futures.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ def cancel(self):
240240
change the future's state to cancelled, schedule the callbacks and
241241
return True.
242242
"""
243+
self._log_traceback = False
243244
if self._state != _PENDING:
244245
return False
245246
self._state = _CANCELLED

Lib/asyncio/tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def cancel(self):
208208
terminates with a CancelledError exception (even if cancel()
209209
was not called).
210210
"""
211+
self._log_traceback = False
211212
if self.done():
212213
return False
213214
if self._fut_waiter is not None:

Lib/test/test_asyncio/test_futures.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,14 @@ def test_tb_logger_abandoned(self, m_log):
314314
del fut
315315
self.assertFalse(m_log.error.called)
316316

317+
@mock.patch('asyncio.base_events.logger')
318+
def test_tb_logger_not_called_after_cancel(self, m_log):
319+
fut = asyncio.Future(loop=self.loop)
320+
fut.set_exception(Exception())
321+
fut.cancel()
322+
del fut
323+
self.assertFalse(m_log.error.called)
324+
317325
@mock.patch('asyncio.base_events.logger')
318326
def test_tb_logger_result_unretrieved(self, m_log):
319327
fut = asyncio.Future(loop=self.loop)

Lib/test/test_asyncio/test_tasks.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,25 @@ def kill_me(loop):
18281828
})
18291829
mock_handler.reset_mock()
18301830

1831+
@mock.patch('asyncio.base_events.logger')
1832+
def test_tb_logger_not_called_after_cancel(self, m_log):
1833+
loop = asyncio.new_event_loop()
1834+
self.set_event_loop(loop)
1835+
1836+
@asyncio.coroutine
1837+
def coro():
1838+
raise TypeError
1839+
1840+
@asyncio.coroutine
1841+
def runner():
1842+
task = loop.create_task(coro())
1843+
yield from asyncio.sleep(0.05, loop=loop)
1844+
task.cancel()
1845+
task = None
1846+
1847+
loop.run_until_complete(runner())
1848+
self.assertFalse(m_log.error.called)
1849+
18311850
@mock.patch('asyncio.coroutines.logger')
18321851
def test_coroutine_never_yielded(self, m_log):
18331852
with set_coroutine_debug(True):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Extension Modules
5656
Library
5757
-------
5858

59+
- bpo-30508: Don't log exceptions if Task/Future "cancel()" method was
60+
called.
61+
5962
- bpo-28556: Updates to typing module: Add generic AsyncContextManager, add
6063
support for ContextManager on all versions. Original PRs by Jelle Zijlstra
6164
and Ivan Levkivskyi

0 commit comments

Comments
 (0)