Skip to content

Commit 71c5360

Browse files
committed
Completely rewrite signal handling.
Instead of using libuv signal handlers, we now simply use signal.set_wakeup_fd function (exactly the same approach as in asyncio). As the result, the new code is *much* simpler, and uvloop programs don't mess with low-level signal processing APIs.
1 parent d260c31 commit 71c5360

File tree

11 files changed

+136
-338
lines changed

11 files changed

+136
-338
lines changed

uvloop/_noop.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def noop():
2+
"""Empty function to invoke CPython ceval loop."""
3+
return

uvloop/cbhandles.pyx

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ cdef class Handle:
3030
cdef:
3131
int cb_type
3232
object callback
33-
bint old_exec_py_code
3433

3534
if self.cancelled:
3635
return
@@ -40,12 +39,6 @@ cdef class Handle:
4039
Py_INCREF(self) # Since _run is a cdef and there's no BoundMethod,
4140
# we guard 'self' manually (since the callback
4241
# might cause GC of the handle.)
43-
old_exec_py_code = self.loop._executing_py_code
44-
# old_exec_py_code might be 0 -- that means that this
45-
# handle is run by libuv callback or something
46-
# and it can be 1 -- that means that we're calling it
47-
# from a UVHandle (for instance UVIdle) callback.
48-
self.loop._executing_py_code = 1
4942
try:
5043
if cb_type == 1:
5144
callback = self.arg1
@@ -91,7 +84,6 @@ cdef class Handle:
9184
self.loop.call_exception_handler(context)
9285

9386
finally:
94-
self.loop._executing_py_code = old_exec_py_code
9587
Py_DECREF(self)
9688

9789
cdef _cancel(self):
@@ -177,9 +169,6 @@ cdef class TimerHandle:
177169
self.timer = None # let it die asap
178170

179171
cdef _run(self):
180-
cdef:
181-
bint old_exec_py_code
182-
183172
if self.closed == 1:
184173
return
185174

@@ -189,11 +178,6 @@ cdef class TimerHandle:
189178

190179
Py_INCREF(self) # Since _run is a cdef and there's no BoundMethod,
191180
# we guard 'self' manually.
192-
old_exec_py_code = self.loop._executing_py_code
193-
IF DEBUG:
194-
if old_exec_py_code == 1:
195-
raise RuntimeError('Python exec-mode before TimerHandle._run')
196-
self.loop._executing_py_code = 1
197181
if self.loop._debug:
198182
started = time_monotonic()
199183
try:
@@ -220,7 +204,6 @@ cdef class TimerHandle:
220204
'Executing %r took %.3f seconds',
221205
self, delta)
222206
finally:
223-
self.loop._executing_py_code = old_exec_py_code
224207
Py_DECREF(self)
225208

226209
# Public API

uvloop/handles/signal.pxd

Lines changed: 0 additions & 13 deletions
This file was deleted.

uvloop/handles/signal.pyx

Lines changed: 0 additions & 76 deletions
This file was deleted.

uvloop/handles/stream.pyx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,13 +832,9 @@ cdef void __uv_stream_on_read(uv.uv_stream_t* stream,
832832

833833
cdef:
834834
Loop loop = <Loop>stream.loop.data
835-
bint old_exec_py_code
836835

837-
old_exec_py_code = loop._executing_py_code
838-
loop._executing_py_code = 1
839836
# Don't need try-finally, __uv_stream_on_read_impl is void
840837
__uv_stream_on_read_impl(stream, nread, buf)
841-
loop._executing_py_code = old_exec_py_code
842838

843839

844840
cdef void __uv_stream_on_write(uv.uv_write_t* req, int status) with gil:
@@ -852,10 +848,6 @@ cdef void __uv_stream_on_write(uv.uv_write_t* req, int status) with gil:
852848

853849
cdef:
854850
Loop loop = <UVStream>(<_StreamWriteContext> req.data).stream._loop
855-
bint old_exec_py_code
856851

857-
old_exec_py_code = loop._executing_py_code
858-
loop._executing_py_code = 1
859852
# Don't need try-finally, __uv_stream_on_write_impl is void
860853
__uv_stream_on_write_impl(req, status)
861-
loop._executing_py_code = old_exec_py_code

uvloop/includes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncio
33
import collections
44
import concurrent.futures
5+
import errno
56
import functools
67
import gc
78
import inspect

uvloop/includes/stdlib.pxi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import asyncio, asyncio.log, asyncio.base_events, \
33
asyncio.futures
44
import collections
55
import concurrent.futures
6+
import errno
67
import functools
78
import gc
89
import inspect
@@ -43,6 +44,8 @@ cdef col_Counter = collections.Counter
4344
cdef cc_ThreadPoolExecutor = concurrent.futures.ThreadPoolExecutor
4445
cdef cc_Future = concurrent.futures.Future
4546

47+
cdef errno_EINVAL = errno.EINVAL
48+
4649
cdef ft_partial = functools.partial
4750

4851
cdef gc_disable = gc.disable
@@ -57,6 +60,7 @@ cdef socket_gaierror = socket.gaierror
5760
cdef socket_error = socket.error
5861
cdef socket_timeout = socket.timeout
5962
cdef socket_socket = socket.socket
63+
cdef socket_socketpair = socket.socketpair
6064
cdef socket_getservbyname = socket.getservbyname
6165

6266
cdef int socket_EAI_ADDRFAMILY = getattr(socket, 'EAI_ADDRFAMILY', -1)
@@ -110,6 +114,10 @@ cdef int subprocess_DEVNULL = subprocess.DEVNULL
110114
cdef subprocess_SubprocessError = subprocess.SubprocessError
111115

112116
cdef int signal_NSIG = std_signal.NSIG
117+
cdef signal_signal = std_signal.signal
118+
cdef signal_set_wakeup_fd = std_signal.set_wakeup_fd
119+
cdef signal_default_int_handler = std_signal.default_int_handler
120+
cdef signal_SIG_DFL = std_signal.SIG_DFL
113121

114122
cdef time_sleep = time.sleep
115123
cdef time_monotonic = time.monotonic
@@ -122,7 +130,7 @@ cdef warnings_warn = warnings.warn
122130

123131
# Cython doesn't clean-up imported objects properly in Py3 mode,
124132
# so we delete refs to all modules manually (except sys)
125-
del asyncio, concurrent, collections
133+
del asyncio, concurrent, collections, errno
126134
del functools, inspect, itertools, socket, os, threading
127135
del std_signal, subprocess, ssl
128136
del time, traceback, warnings

uvloop/loop.pxd

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ cdef class UVHandle
2121

2222
cdef class UVAsync(UVHandle)
2323
cdef class UVTimer(UVHandle)
24-
cdef class UVSignal(UVHandle)
2524
cdef class UVIdle(UVHandle)
2625

2726
ctypedef object (*method_t)(object)
@@ -45,11 +44,6 @@ cdef class Loop:
4544

4645
long _thread_id
4746
bint _thread_is_main
48-
bint _sigint_check
49-
50-
SignalsStack py_signals
51-
SignalsStack uv_signals
52-
bint _executing_py_code
5347

5448
object _task_factory
5549
object _exception_handler
@@ -58,19 +52,18 @@ cdef class Loop:
5852
set _queued_streams
5953
Py_ssize_t _ready_len
6054

55+
dict _signal_handlers
56+
object _ssock
57+
object _csock
58+
6159
set _timers
6260
dict _polls
6361

64-
dict _signal_handlers
65-
bint _custom_sigint
66-
6762
UVProcess active_process_handler
6863

6964
UVAsync handler_async
7065
UVIdle handler_idle
7166
UVCheck handler_check__exec_writes
72-
UVSignal handler_sigint
73-
UVSignal handler_sighup
7467

7568
object _last_error
7669

@@ -121,10 +114,6 @@ cdef class Loop:
121114

122115
cdef _on_wake(self)
123116
cdef _on_idle(self)
124-
cdef _on_sigint(self)
125-
cdef _on_sighup(self)
126-
127-
cdef _check_sigint(self)
128117

129118
cdef __run(self, uv.uv_run_mode)
130119
cdef _run(self, uv.uv_run_mode)
@@ -177,6 +166,12 @@ cdef class Loop:
177166

178167
cdef _sock_set_reuseport(self, int fd)
179168

169+
cdef _setup_signals(self)
170+
cdef _shutdown_signals(self)
171+
cdef _handle_signal(self, sig)
172+
cdef _read_from_self(self)
173+
cdef _process_self_data(self, data)
174+
180175
cdef _set_coroutine_wrapper(self, bint enabled)
181176

182177

@@ -187,7 +182,6 @@ include "handles/async_.pxd"
187182
include "handles/idle.pxd"
188183
include "handles/check.pxd"
189184
include "handles/timer.pxd"
190-
include "handles/signal.pxd"
191185
include "handles/poll.pxd"
192186
include "handles/basetransport.pxd"
193187
include "handles/stream.pxd"
@@ -201,5 +195,3 @@ include "request.pxd"
201195
include "handles/udp.pxd"
202196

203197
include "server.pxd"
204-
205-
include "os_signal.pxd"

0 commit comments

Comments
 (0)