-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Open
Labels
Description
Bug report
Bug description:
Consider this code
import sys
import logging
import asyncio
import signal
from time import*
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
async def f():
if 1:
main_task = asyncio.current_task()
def on_sigint() -> None:
logger.error("Received SIGINT")
main_task.cancel(KeyboardInterrupt)
asyncio.get_running_loop().add_signal_handler(signal.SIGINT, on_sigint)
try:
while True:
await asyncio.sleep(0.1)
print("*")
except asyncio.CancelledError as e:
print(e)
try:
while True:
await asyncio.sleep(0.1)
print("#")
except asyncio.CancelledError:
try:
while True:
await asyncio.sleep(0.1)
print("!")
except asyncio.CancelledError:
while True:
await asyncio.sleep(0.1)
print("@")
asyncio.run(f())Intention: each time I press Ctrl-C, it repeatedly print *, #, !, @ respectively.
As is, it does do that correctly.
However, if I don't manually add the SIGINT signal handler, it will print *, {first Ctrl-C} #, {second Ctrl-C} !, {third Ctrl-C} then forcefully stop.
I don't think this behavior is desirable. Or at least it probably shouldn't log a scary error Task was destroyed but it is pending!?
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Projects
Status
Todo