11"""Tests for tasks.py."""
22
3+ import _thread
34import collections
45import contextlib
56import contextvars
1920from asyncio import tasks
2021from test .test_asyncio import utils as test_utils
2122from test import support
23+ from test .support import threading_helper
2224from test .support .script_helper import assert_python_ok
2325from test .support .warnings_helper import ignore_warnings
2426
@@ -1250,6 +1252,37 @@ async def coro():
12501252
12511253 self .loop .run_until_complete (self .new_task (self .loop , coro ()))
12521254
1255+ def test_gather_discards_awaited_by_for_cancelled_sibling (self ):
1256+ # gh-157213: same, when gather() is ended by a cancelled child
1257+ async def survivor ():
1258+ await asyncio .Future ()
1259+
1260+ async def coro ():
1261+ t = self .new_task (self .loop , survivor ())
1262+ victim = self .new_task (self .loop , asyncio .sleep (10 ))
1263+ victim .cancel ()
1264+ with self .assertRaises (asyncio .CancelledError ):
1265+ await asyncio .gather (t , victim )
1266+ self .assertFalse (t ._asyncio_awaited_by )
1267+ t .cancel ()
1268+ with self .assertRaises (asyncio .CancelledError ):
1269+ await t
1270+
1271+ self .loop .run_until_complete (self .new_task (self .loop , coro ()))
1272+
1273+ def test_gather_does_not_add_callback_to_outer (self ):
1274+ # gh-158239: gather() must not add an internal done callback to
1275+ # the outer future just to maintain the await graph.
1276+ async def child ():
1277+ await asyncio .sleep (0 )
1278+
1279+ async def coro ():
1280+ outer = asyncio .gather (child (), child ())
1281+ self .assertFalse (outer ._callbacks )
1282+ await outer
1283+
1284+ self .loop .run_until_complete (self .new_task (self .loop , coro ()))
1285+
12531286 def test_wait_really_done (self ):
12541287 # there is possibility that some tasks in the pending list
12551288 # became done but their callbacks haven't all been called yet
@@ -2188,7 +2221,7 @@ def test_shield_cancel_outer(self):
21882221 self .assertTrue (outer .cancelled ())
21892222 self .assertEqual (0 , 0 if outer ._callbacks is None else len (outer ._callbacks ))
21902223 self .assertFalse (inner ._asyncio_awaited_by )
2191- self .assertTrue ({ f for f , _ctx in inner ._callbacks or []} <= { asyncio . tasks . _log_on_exception } )
2224+ self .assertFalse ( inner ._callbacks )
21922225
21932226 def test_shield_cancel_outer_result (self ):
21942227 mock_handler = mock .Mock ()
@@ -2203,6 +2236,8 @@ def test_shield_cancel_outer_result(self):
22032236 mock_handler .assert_not_called ()
22042237
22052238 def test_shield_cancel_outer_exception (self ):
2239+ # gh-156321: an exception in the inner future must not be reported
2240+ # eagerly, as it may still be retrieved later.
22062241 mock_handler = mock .Mock ()
22072242 self .loop .set_exception_handler (mock_handler )
22082243 inner = self .new_future (self .loop )
@@ -2212,7 +2247,29 @@ def test_shield_cancel_outer_exception(self):
22122247 test_utils .run_briefly (self .loop )
22132248 inner .set_exception (Exception ('foo' ))
22142249 test_utils .run_briefly (self .loop )
2250+ mock_handler .assert_not_called ()
2251+ self .assertIsInstance (inner .exception (), Exception )
2252+
2253+ def test_shield_cancel_outer_exception_never_retrieved (self ):
2254+ # gh-156321: an exception nobody retrieves is reported by the inner
2255+ # future itself when it is garbage collected, like any other future.
2256+ mock_handler = mock .Mock ()
2257+ self .loop .set_exception_handler (mock_handler )
2258+ inner = self .new_future (self .loop )
2259+ outer = asyncio .shield (inner )
2260+ test_utils .run_briefly (self .loop )
2261+ outer .cancel ()
2262+ test_utils .run_briefly (self .loop )
2263+ inner .set_exception (Exception ('foo' ))
2264+ test_utils .run_briefly (self .loop )
2265+ mock_handler .assert_not_called ()
2266+ inner = None
2267+ outer = None
2268+ support .gc_collect ()
22152269 mock_handler .assert_called_once ()
2270+ context = mock_handler .call_args [0 ][1 ]
2271+ self .assertEndsWith (context ['message' ], 'exception was never retrieved' )
2272+ self .assertIsInstance (context ['exception' ], Exception )
22162273
22172274 def test_shield_cancel_outer_in_task (self ):
22182275 inner = self .new_future (self .loop )
@@ -2227,9 +2284,9 @@ async def coro():
22272284 task = self .new_task (self .loop , coro ())
22282285 self .loop .run_until_complete (task )
22292286 self .assertFalse (inner ._asyncio_awaited_by )
2230- self .assertTrue ({ f for f , _ctx in inner ._callbacks or []} <= { asyncio . tasks . _log_on_exception } )
2287+ self .assertFalse ( inner ._callbacks )
22312288
2232- def test_shield_duplicate_log_once (self ):
2289+ def test_shield_cancel_outer_twice_exception (self ):
22332290 mock_handler = mock .Mock ()
22342291 self .loop .set_exception_handler (mock_handler )
22352292 inner = self .new_future (self .loop )
@@ -2241,9 +2298,11 @@ def test_shield_duplicate_log_once(self):
22412298 test_utils .run_briefly (self .loop )
22422299 outer .cancel ()
22432300 test_utils .run_briefly (self .loop )
2301+ self .assertFalse (inner ._callbacks )
22442302 inner .set_exception (Exception ('foo' ))
22452303 test_utils .run_briefly (self .loop )
2246- mock_handler .assert_called_once ()
2304+ mock_handler .assert_not_called ()
2305+ self .assertIsInstance (inner .exception (), Exception )
22472306
22482307 def test_shield_shortcut (self ):
22492308 fut = self .new_future (self .loop )
0 commit comments