This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncoghlan
Recipients gregory.p.smith, ncoghlan, njs
Date 2017-09-07.20:03:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1504814605.12.0.216214618224.issue31388@psf.upfronthosting.co.za>
In-reply-to
Content
As discussed in issue 29988, it's currently difficult to write completely robust cleanup code in Python, as the default SIGINT handler may lead to KeyboardInterrupt being raised as soon as *any* Python code starts executing in the main thread, even when that Python code is part of:

- a finally block
- an __exit__ method
- a __del__ method
- a weakref callback
- a trace hook
- a profile hook
- a pending call callback

Issue 29988 proposes a way to adjust with statements to ensure that __exit__ at least starts executing if __enter__ returned successfully, but that's only sufficient to ensure robust resource cleanup if the __exit__ method is implemented in C and never calls back in to any operation that starts executing Python code.

Currently, the "best" option for ensuring cleanup code isn't interrupted is to outright ignore SIGINT while the cleanup code is running:

    old_handler = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, signal.SIG_IGN)
    try:
        ... # Cleanup code
    finally:
        signal.signal(signal.SIGINT, old_handler)

Fortunately, most folks aren't willing to do this, but it does suggest a potential pattern for temporarily *deferring* SIGINT handling: adding a "deferred" attribute to the entries in the array that tracks incoming signals, and providing some C level decorators and context managers for manipulating that state.
History
Date User Action Args
2017-09-07 20:03:25ncoghlansetrecipients: + ncoghlan, gregory.p.smith, njs
2017-09-07 20:03:25ncoghlansetmessageid: <1504814605.12.0.216214618224.issue31388@psf.upfronthosting.co.za>
2017-09-07 20:03:25ncoghlanlinkissue31388 messages
2017-09-07 20:03:24ncoghlancreate