Issue 31387: asyncio should make it easy to enable cooperative SIGINT handling (original) (raw)
Messages (4)
Author: Alyssa Coghlan (ncoghlan) *
Date: 2017-09-07 19:37
Issue 29988 covers the fact that with the default SIGINT handler installed, a poorly timed Ctrl-C can lead to context managers failing to even start running their (a)exit methods, let alone complete them.
For the asynchronous case, the problem is even worse, as the event loop may be interrupted at arbitrary points if the default SIGINT handler is left in place.
To handle this robustly, it's desirable to make it easy to switch event-driven programs over to cooperative Ctrl-C handling by installing an asyncio SIGINT handler while the event loop is running, rather than leaving the default SIGINT handler in place.
(Note: while installing a cooperative SIGINT handler will enable more robust event-loop managed resource cleanup, it will have the downside that Ctrl-C won't be able to interrupt a coroutine that has incorrectly blocked the main thread)
Author: Nathaniel Smith (njs) *
Date: 2017-09-07 21:22
Some prior discussion on the old asyncio tracker: https://github.com/python/asyncio/pull/305#issuecomment-168714572 https://github.com/python/asyncio/issues/341
Author: Alyssa Coghlan (ncoghlan) *
Date: 2017-09-08 01:04
Issue 31388 is also potentially relevant here, as registering a signal handler for SIGINT isn't sufficient to cover all potential cases were Py_AddPendingCall gets called. In particular, the tests for issue 29988 use Py_AddPendingCall to emulate Ctrl-C, rather than relying on SIGINT actually being raised.
Author: Alyssa Coghlan (ncoghlan) *
Date: 2017-09-08 02:28
As per Nathaniel's comments on issue 29988 and 31388, doing this robustly relies on:
- the event loop being able to reliably guard itself and aexit method implementations against interrupts (issue 31388)
- "async with" statements ensuring that if the frame resumes after calling aenter, then it will also call aexit (as was done for synchronous with statements in issue 29988)
History
Date
User
Action
Args
2022-04-11 14:58:52
admin
set
github: 75568
2019-10-14 23:11:16
aeros
set
nosy: + aeros
2019-09-07 12:32:21
ryanhiebert
set
nosy: + ryanhiebert
2017-09-08 02:28:12
ncoghlan
set
dependencies: + with statements are not ensuring that __exit__ is called if __enter__ succeeds, Provide a way to defer SIGINT handling in the current thread
messages: +
2017-09-08 01:04:35
ncoghlan
set
messages: +
2017-09-07 21:22:41
njs
set
messages: +
2017-09-07 19:37:30
ncoghlan
create