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

Avoid signal handling reentrancy issues #1650

Merged
merged 1 commit into from
May 3, 2024
Merged
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
19 changes: 13 additions & 6 deletions alot/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def __init__(self, dbman, initialcmdline):
mainframe = urwid.Frame(urwid.SolidFill())
self.root_widget = urwid.AttrMap(mainframe, global_att)

signal.signal(signal.SIGINT, self.handle_signal)
signal.signal(signal.SIGUSR1, self.handle_signal)
signal.signal(signal.SIGINT, self._handle_signal)
signal.signal(signal.SIGUSR1, self._handle_signal)

# load histories
self._cache = os.path.join(
Expand Down Expand Up @@ -728,21 +728,28 @@ async def apply_command(self, cmd):
logging.info('calling post-hook')
await cmd.posthook(ui=self, dbm=self.dbman, cmd=cmd)

def handle_signal(self, signum, frame):
def _handle_signal(self, signum, _frame):
"""
Handle UNIX signals: add a new task onto the event loop.
Doing it this way ensures what our handler has access to whatever
synchronization primitives or async calls it may require.
"""
loop = asyncio.get_event_loop()
asyncio.run_coroutine_threadsafe(self.handle_signal(signum), loop)

async def handle_signal(self, signum):
"""
handles UNIX signals

This function currently just handles SIGUSR1. It could be extended to
handle more

:param signum: The signal number (see man 7 signal)
:param frame: The execution frame
(https://docs.python.org/2/reference/datamodel.html#frame-objects)
"""
# it is a SIGINT ?
if signum == signal.SIGINT:
logging.info('shut down cleanly')
asyncio.ensure_future(self.apply_command(globals.ExitCommand()))
await self.apply_command(globals.ExitCommand())
elif signum == signal.SIGUSR1:
if isinstance(self.current_buffer, SearchBuffer):
self.current_buffer.rebuild()
Expand Down
Loading