Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve force exiting (double interrupt) #1745

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changes/1745.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve handing of force exiting a bot (double interrupt)
- Improve exception message
- Reset signal handlers to original ones after no longer capturing signals
3 changes: 3 additions & 0 deletions hikari/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class HikariInterrupt(KeyboardInterrupt, HikariError):
signame: str = attrs.field()
"""The signal name that was raised."""

def __str__(self) -> str:
return f"Signal {self.signum} ({self.signame}) received"


@attrs.define(auto_exc=True, repr=False, slots=False)
class ComponentStateConflictError(HikariError):
Expand Down
39 changes: 22 additions & 17 deletions hikari/impl/gateway_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,10 @@ def run(
except AttributeError:
_LOGGER.log(ux.TRACE, "cannot set coroutine tracking depth for sys, no functionality exists for this")

try:
with signals.handle_interrupts(
enabled=enable_signal_handlers, loop=loop, propagate_interrupts=propagate_interrupts
):
with signals.handle_interrupts(
enabled=enable_signal_handlers, loop=loop, propagate_interrupts=propagate_interrupts
):
try:
loop.run_until_complete(
self.start(
activity=activity,
Expand All @@ -825,22 +825,27 @@ def run(

loop.run_until_complete(self.join())

finally:
if self._closing_event:
if self._closing_event.is_set():
loop.run_until_complete(self._closing_event.wait())
else:
loop.run_until_complete(self.close())
finally:
try:
if self._closing_event:
if self._closing_event.is_set():
loop.run_until_complete(self._closing_event.wait())
else:
loop.run_until_complete(self.close())

if close_passed_executor and self._executor is not None:
_LOGGER.debug("shutting down executor %s", self._executor)
self._executor.shutdown(wait=True)
self._executor = None

if close_passed_executor and self._executor is not None:
_LOGGER.debug("shutting down executor %s", self._executor)
self._executor.shutdown(wait=True)
self._executor = None
if close_loop:
aio.destroy_loop(loop, _LOGGER)

if close_loop:
aio.destroy_loop(loop, _LOGGER)
_LOGGER.info("successfully terminated")

_LOGGER.info("successfully terminated")
except errors.HikariInterrupt:
_LOGGER.warning("forcefully terminated")
raise

async def start(
self,
Expand Down
39 changes: 22 additions & 17 deletions hikari/impl/rest_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,10 @@ def run(
except AttributeError:
_LOGGER.log(ux.TRACE, "cannot set coroutine tracking depth for sys, no functionality exists for this")

try:
with signals.handle_interrupts(
enabled=enable_signal_handlers, loop=loop, propagate_interrupts=propagate_interrupts
):
with signals.handle_interrupts(
enabled=enable_signal_handlers, loop=loop, propagate_interrupts=propagate_interrupts
):
try:
loop.run_until_complete(
self.start(
backlog=backlog,
Expand All @@ -588,22 +588,27 @@ def run(
)
loop.run_until_complete(self.join())

finally:
if self._close_event:
if self._is_closing:
loop.run_until_complete(self._close_event.wait())
else:
loop.run_until_complete(self.close())
finally:
try:
if self._close_event:
if self._is_closing:
loop.run_until_complete(self._close_event.wait())
else:
loop.run_until_complete(self.close())

if close_passed_executor and self._executor:
_LOGGER.debug("shutting down executor %s", self._executor)
self._executor.shutdown(wait=True)
self._executor = None

if close_passed_executor and self._executor:
_LOGGER.debug("shutting down executor %s", self._executor)
self._executor.shutdown(wait=True)
self._executor = None
if close_loop:
aio.destroy_loop(loop, _LOGGER)

if close_loop:
aio.destroy_loop(loop, _LOGGER)
_LOGGER.info("successfully terminated")

_LOGGER.info("successfully terminated")
except errors.HikariInterrupt:
_LOGGER.warning("forcefully terminated")
raise

async def start(
self,
Expand Down
2 changes: 1 addition & 1 deletion hikari/internal/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ async def murder(future: asyncio.Future[typing.Any]) -> None:
remaining_tasks = tuple(t for t in asyncio.all_tasks(loop) if not t.done())

if remaining_tasks:
logger.debug("terminating %s remaining tasks forcefully", len(remaining_tasks))
logger.warning("terminating %s remaining tasks forcefully", len(remaining_tasks))
loop.run_until_complete(_gather((murder(task) for task in remaining_tasks)))
else:
logger.debug("No remaining tasks exist, good job!")
Expand Down
21 changes: 10 additions & 11 deletions hikari/internal/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
from hikari import errors
from hikari.internal import ux

if typing.TYPE_CHECKING:
_SignalHandlerT = typing.Callable[[int, typing.Optional[types.FrameType]], None]

_INTERRUPT_SIGNALS: typing.Tuple[str, ...] = ("SIGINT", "SIGTERM")
_LOGGER: typing.Final[logging.Logger] = logging.getLogger("hikari.signals")

Expand All @@ -49,9 +52,7 @@ def _raise_interrupt(signum: int) -> typing.NoReturn:
raise errors.HikariInterrupt(signum, signame)


def _interrupt_handler(
loop: asyncio.AbstractEventLoop,
) -> typing.Callable[[int, typing.Optional[types.FrameType]], None]:
def _interrupt_handler(loop: asyncio.AbstractEventLoop) -> _SignalHandlerT:
loop_thread_id = threading.get_native_id()

def handler(signum: int, frame: typing.Optional[types.FrameType]) -> None:
Expand Down Expand Up @@ -102,13 +103,16 @@ def handle_interrupts(
return

interrupt_handler = _interrupt_handler(loop)
original_handlers: typing.Dict[int, typing.Union[int, _SignalHandlerT, None]] = {}

for sig in _INTERRUPT_SIGNALS:
try:
signum = getattr(signal, sig)
signal.signal(signum, interrupt_handler)
except AttributeError:
_LOGGER.log(ux.TRACE, "signal %s is not implemented on your platform; skipping", sig)
else:
original_handlers[signum] = signal.getsignal(signum)
signal.signal(signum, interrupt_handler)

try:
yield
Expand All @@ -118,10 +122,5 @@ def handle_interrupts(
raise

finally:
for sig in _INTERRUPT_SIGNALS:
try:
signum = getattr(signal, sig)
signal.signal(signum, signal.SIG_DFL)
except AttributeError:
# Signal not implemented. We already logged this earlier.
pass
for signum, handler in original_handlers.items():
signal.signal(signum, handler)
4 changes: 3 additions & 1 deletion tests/hikari/internal/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def test_behaviour(self):
register_signal_handler.reset_mock()

assert register_signal_handler.call_count == 2
register_signal_handler.assert_has_calls([mock.call(2, signal.SIG_DFL), mock.call(15, signal.SIG_DFL)])
register_signal_handler.assert_has_calls(
[mock.call(2, signal.default_int_handler), mock.call(15, signal.SIG_DFL)]
)

def test_when_disabled(self):
with mock.patch.object(signal, "signal") as register_signal_handler:
Expand Down