classification
Title: asyncio loop.add_signal_handler() may not behave as expected
Type: behavior Stage: patch review
Components: asyncio Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, rogerdahl, tiposchi, yselivanov
Priority: normal Keywords: patch

Created on 2020-02-26 20:46 by rogerdahl, last changed 2020-07-05 08:32 by tiposchi.

Pull Requests
URL Status Linked Edit
PR 18664 open rogerdahl, 2020-02-26 20:52
Messages (6)
msg362734 - (view) Author: Roger Dahl (rogerdahl) * Date: 2020-02-26 20:46
This is a ticket to document two ways in which the behavior of loop.set_signal_handler() may not match what the user expects.

First, callbacks to handlers registered with loop.set_signal_handler() may be significantly delayed. I have a program where I've encountered delays of up to maybe a minute or so between hitting Ctrl-C and getting the callback for the SIGINT handler. During this time, the program works through queued aiohttp tasks. Though it's possible to have delays in callbacks for events set with signal.signal(), I haven't personally seen that, and I think that's the case for most users. So I think this point should be included in the docs.

Second, set_signal_handler() silently and implicitly removes corresponding handlers set with signal.signal(). Though perhaps logical, this potentially removes a "fast" handler and replaces it with a "slow" one. I think this should be documented as well.
msg362747 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-02-26 22:17
I'm sorry. but loop.set_signal_handler() method doesn't exist.
msg362748 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-02-26 22:23
Assuming you mean loop.add_signal_handler() method, I would say that a minute-long delay is a sign of long blocking calls in your program.

Typical asyncio program blocks the loop for 1-10 milliseconds at most, and signal handlers are called with this delay.
This is not your case as I see.
msg362750 - (view) Author: Roger Dahl (rogerdahl) * Date: 2020-02-26 22:29
> I'm sorry. but loop.set_signal_handler() method doesn't exist.

Oops, fixed.

> Assuming you mean loop.add_signal_handler() method, I would say that a minute-long delay is a sign of long blocking calls in your program.

During the delay for the callback to occur, I see my program logging work performed on other asyncio tasks and receiving timer events from another thread. So, not long blocking?
msg362830 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-02-27 19:01
Other threads are not related.

Speaking about the main thread that runs an event loop with the signal handler installed -- the handler is executed along with other async code.
If the loop executes some user-provides async task -- it executes the signal handler as well if needed.

This is why I suspect that the discussed program is not completely correct.
msg373018 - (view) Author: LtWorf (tiposchi) Date: 2020-07-05 08:32
This looks related to: https://bugs.python.org/issue32443
History
Date User Action Args
2020-07-05 08:32:00tiposchisetnosy: + tiposchi
messages: + msg373018
2020-02-27 19:01:55asvetlovsetmessages: + msg362830
2020-02-26 22:29:53rogerdahlsetmessages: + msg362750
2020-02-26 22:23:52rogerdahlsettitle: asyncio loop.set_signal_handler() may not behave as expected -> asyncio loop.add_signal_handler() may not behave as expected
2020-02-26 22:23:52asvetlovsetmessages: + msg362748
2020-02-26 22:17:19asvetlovsetmessages: + msg362747
2020-02-26 20:52:05rogerdahlsetkeywords: + patch
stage: patch review
pull_requests: + pull_request18028
2020-02-26 20:46:52rogerdahlcreate