mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
Python issue #21645, Tulip issue 192: Rewrite signal handling
Since Python 3.3, the C signal handler writes the signal number into the wakeup file descriptor and then schedules the Python call using Py_AddPendingCall(). asyncio uses the wakeup file descriptor to wake up the event loop, and relies on Py_AddPendingCall() to schedule the final callback with call_soon(). If the C signal handler is called in a thread different than the thread of the event loop, the loop is awaken but Py_AddPendingCall() was not called yet. In this case, the event loop has nothing to do and go to sleep again. Py_AddPendingCall() is called while the event loop is sleeping again and so the final callback is not scheduled immediatly. This patch changes how asyncio handles signals. Instead of relying on Py_AddPendingCall() and the wakeup file descriptor, asyncio now only relies on the wakeup file descriptor. asyncio reads signal numbers from the wakeup file descriptor to call its signal handler.
This commit is contained in:
parent
ddc8c8db1c
commit
fe5649c7b7
5 changed files with 27 additions and 7 deletions
|
@ -435,7 +435,7 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
|
|||
|
||||
def test_write_to_self(self):
|
||||
self.loop._write_to_self()
|
||||
self.csock.send.assert_called_with(b'x')
|
||||
self.csock.send.assert_called_with(b'\0')
|
||||
|
||||
def test_process_events(self):
|
||||
self.loop._process_events([])
|
||||
|
|
|
@ -42,7 +42,7 @@ class SelectorEventLoopSignalTests(test_utils.TestCase):
|
|||
ValueError, self.loop._check_signal, signal.NSIG + 1)
|
||||
|
||||
def test_handle_signal_no_handler(self):
|
||||
self.loop._handle_signal(signal.NSIG + 1, ())
|
||||
self.loop._handle_signal(signal.NSIG + 1)
|
||||
|
||||
def test_handle_signal_cancelled_handler(self):
|
||||
h = asyncio.Handle(mock.Mock(), (),
|
||||
|
@ -50,7 +50,7 @@ class SelectorEventLoopSignalTests(test_utils.TestCase):
|
|||
h.cancel()
|
||||
self.loop._signal_handlers[signal.NSIG + 1] = h
|
||||
self.loop.remove_signal_handler = mock.Mock()
|
||||
self.loop._handle_signal(signal.NSIG + 1, ())
|
||||
self.loop._handle_signal(signal.NSIG + 1)
|
||||
self.loop.remove_signal_handler.assert_called_with(signal.NSIG + 1)
|
||||
|
||||
@mock.patch('asyncio.unix_events.signal')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue