@@ -918,6 +918,56 @@ def func():
918918 self .loop .run_forever ()
919919 self .assertTrue (func .called )
920920
921+ def test_run_until_complete_interrupted_after_done (self ):
922+ # gh-158406: an interrupt after the future is done must not stop
923+ # the next run_until_complete() call early.
924+ self .loop ._process_events = mock .Mock ()
925+ for exc in (KeyboardInterrupt , SystemExit ):
926+ with self .subTest (exc = exc ):
927+ def interrupt ():
928+ raise exc
929+
930+ async def main ():
931+ self .loop .call_soon (interrupt )
932+
933+ with self .assertRaises (exc ):
934+ self .loop .run_until_complete (main ())
935+ result = self .loop .run_until_complete (asyncio .sleep (0 , "ret" ))
936+ self .assertEqual (result , "ret" )
937+
938+ def test_run_until_complete_stopped_after_done (self ):
939+ # gh-158406: stop() in the iteration in which the future is done
940+ # must not stop the next run_until_complete() call early.
941+ async def main ():
942+ self .loop .stop ()
943+ return 1
944+
945+ self .loop ._process_events = mock .Mock ()
946+ self .assertEqual (self .loop .run_until_complete (main ()), 1 )
947+ result = self .loop .run_until_complete (asyncio .sleep (0 , "ret" ))
948+ self .assertEqual (result , "ret" )
949+
950+ def test_run_forever_after_interrupted_run_until_complete (self ):
951+ # gh-158406: an interrupt after the future is done must not stop
952+ # a later run_forever() call early.
953+ def interrupt ():
954+ raise KeyboardInterrupt
955+
956+ async def main ():
957+ self .loop .call_soon (interrupt )
958+
959+ self .loop ._process_events = mock .Mock ()
960+ with self .assertRaises (KeyboardInterrupt ):
961+ self .loop .run_until_complete (main ())
962+
963+ def func ():
964+ self .loop .stop ()
965+ func .called = True
966+ func .called = False
967+ self .loop .call_soon (self .loop .call_soon , func )
968+ self .loop .run_forever ()
969+ self .assertTrue (func .called )
970+
921971 def test_single_selecter_event_callback_after_stopping (self ):
922972 # Python issue #25593: A stopped event loop may cause event callbacks
923973 # to run more than once.
0 commit comments