ioloop: add_callback_from_signal() calls add_callback()
Now that add_callback checks thread.ident() and no longer deadlocks from a signal handler, add_callback_from_signal can call it unconditionally.
This commit is contained in:
parent
8216a5e599
commit
d8e069ef1e
|
@ -929,33 +929,20 @@ class PollIOLoop(IOLoop):
|
|||
if self._closing:
|
||||
raise RuntimeError("IOLoop is closing")
|
||||
# If we're on the IOLoop's thread, we don't need the lock,
|
||||
# since we don't need to wake anyone, just add the callback.
|
||||
# Blindly insert into self._callbacks.
|
||||
# This is safe because the GIL makes list.append atomic.
|
||||
# One subtlety is that if the thread is interrupting another
|
||||
# thread holding the _callback_lock block in IOLoop.start,
|
||||
# we may modify either the old or new version of self._callbacks,
|
||||
# but either way will work.
|
||||
# since we don't need to wake anyone, just add the
|
||||
# callback. Blindly insert into self._callbacks. This is
|
||||
# safe even from signal handlers because the GIL makes
|
||||
# list.append atomic. One subtlety is that if the signal
|
||||
# is interrupting another thread holding the
|
||||
# _callback_lock block in IOLoop.start, we may modify
|
||||
# either the old or new version of self._callbacks, but
|
||||
# either way will work.
|
||||
self._callbacks.append(functools.partial(
|
||||
stack_context.wrap(callback), *args, **kwargs))
|
||||
|
||||
def add_callback_from_signal(self, callback, *args, **kwargs):
|
||||
with stack_context.NullContext():
|
||||
if thread.get_ident() != self._thread_ident:
|
||||
# if the signal is handled on another thread, we can add
|
||||
# it normally (modulo the NullContext)
|
||||
self.add_callback(callback, *args, **kwargs)
|
||||
else:
|
||||
# If we're on the IOLoop's thread, we cannot use
|
||||
# the regular add_callback because it may deadlock on
|
||||
# _callback_lock. Blindly insert into self._callbacks.
|
||||
# This is safe because the GIL makes list.append atomic.
|
||||
# One subtlety is that if the signal interrupted the
|
||||
# _callback_lock block in IOLoop.start, we may modify
|
||||
# either the old or new version of self._callbacks,
|
||||
# but either way will work.
|
||||
self._callbacks.append(functools.partial(
|
||||
stack_context.wrap(callback), *args, **kwargs))
|
||||
self.add_callback(callback, *args, **kwargs)
|
||||
|
||||
|
||||
class _Timeout(object):
|
||||
|
|
Loading…
Reference in New Issue