Skip to content

Commit 673eec3

Browse files
committed
Fixed issue where persistent history file was not saved upon SIGTERM and SIGHUP signals.
1 parent be15939 commit 673eec3

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

cmd2/cmd2.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def unregister_command_set(self, cmdset: CommandSet) -> None:
780780

781781
methods = inspect.getmembers(
782782
cmdset,
783-
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type]
783+
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type, var-annotated]
784784
and hasattr(meth, '__name__')
785785
and meth.__name__.startswith(COMMAND_FUNC_PREFIX),
786786
)
@@ -811,7 +811,7 @@ def unregister_command_set(self, cmdset: CommandSet) -> None:
811811
def _check_uninstallable(self, cmdset: CommandSet) -> None:
812812
methods = inspect.getmembers(
813813
cmdset,
814-
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type]
814+
predicate=lambda meth: isinstance(meth, Callable) # type: ignore[arg-type, var-annotated]
815815
and hasattr(meth, '__name__')
816816
and meth.__name__.startswith(COMMAND_FUNC_PREFIX),
817817
)
@@ -2418,7 +2418,7 @@ def get_help_topics(self) -> List[str]:
24182418
def sigint_handler(self, signum: int, _: FrameType) -> None:
24192419
"""Signal handler for SIGINTs which typically come from Ctrl-C events.
24202420
2421-
If you need custom SIGINT behavior, then override this function.
2421+
If you need custom SIGINT behavior, then override this method.
24222422
24232423
:param signum: signal number
24242424
:param _: required param for signal handlers
@@ -2437,6 +2437,30 @@ def sigint_handler(self, signum: int, _: FrameType) -> None:
24372437
if raise_interrupt:
24382438
self._raise_keyboard_interrupt()
24392439

2440+
def sigterm_handler(self, signum: int, _: FrameType) -> None:
2441+
"""
2442+
Signal handler for SIGTERMs which are sent to politely ask this app to terminate.
2443+
2444+
If you need custom SIGTERM behavior, then override this method.
2445+
2446+
:param signum: signal number
2447+
:param _: required param for signal handlers
2448+
"""
2449+
# Gracefully exit so the persistent history file will be written.
2450+
sys.exit(self.exit_code)
2451+
2452+
def sighup_handler(self, signum: int, _: FrameType) -> None:
2453+
"""
2454+
Signal handler for SIGHUPs which are sent when the terminal is closed.
2455+
2456+
If you need custom SIGHUP behavior, then override this method.
2457+
2458+
:param signum: signal number
2459+
:param _: required param for signal handlers
2460+
"""
2461+
# Gracefully exit so the persistent history file will be written.
2462+
sys.exit(self.exit_code)
2463+
24402464
def _raise_keyboard_interrupt(self) -> None:
24412465
"""Helper function to raise a KeyboardInterrupt"""
24422466
raise KeyboardInterrupt("Got a keyboard interrupt")
@@ -5447,6 +5471,12 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
54475471
original_sigint_handler = signal.getsignal(signal.SIGINT)
54485472
signal.signal(signal.SIGINT, self.sigint_handler) # type: ignore
54495473

5474+
original_sigterm_handler = signal.getsignal(signal.SIGTERM)
5475+
signal.signal(signal.SIGTERM, self.sigterm_handler) # type: ignore
5476+
5477+
original_sighup_handler = signal.getsignal(signal.SIGHUP)
5478+
signal.signal(signal.SIGHUP, self.sighup_handler) # type: ignore
5479+
54505480
# Grab terminal lock before the command line prompt has been drawn by readline
54515481
self.terminal_lock.acquire()
54525482

@@ -5479,8 +5509,10 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
54795509
# This will also zero the lock count in case cmdloop() is called again
54805510
self.terminal_lock.release()
54815511

5482-
# Restore the original signal handler
5512+
# Restore the original signal handlers
54835513
signal.signal(signal.SIGINT, original_sigint_handler)
5514+
signal.signal(signal.SIGTERM, original_sigterm_handler)
5515+
signal.signal(signal.SIGHUP, original_sighup_handler)
54845516

54855517
return self.exit_code
54865518

0 commit comments

Comments
 (0)