Skip to content

Commit 40fd920

Browse files
Enhance GracefulShutdownHandler to prevent duplicate shutdown execution
- Added checks in `_handle_sigint` and `_handle_sigterm` methods to ignore signals if a shutdown is already in progress, improving robustness and logging clarity.
1 parent c064560 commit 40fd920

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

py_spring_core/core/interfaces/graceful_shutdown_handler.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def __init__(self, timeout_seconds: float, timeout_enabled: bool) -> None:
3939

4040
def _handle_sigint(self, signum: int, frame: Optional[FrameType]) -> None:
4141
try:
42+
# Check if shutdown is already in progress to prevent duplicate execution
43+
if self._shutdown_event.is_set():
44+
logger.debug("[Signal] SIGINT ignored - shutdown already in progress")
45+
return
46+
4247
logger.info("[Signal] SIGINT received")
4348
self._shutdown_type = ShutdownType.MANUAL
4449
self._shutdown_event.set()
@@ -49,6 +54,11 @@ def _handle_sigint(self, signum: int, frame: Optional[FrameType]) -> None:
4954

5055
def _handle_sigterm(self, signum: int, frame: Optional[FrameType]) -> None:
5156
try:
57+
# Check if shutdown is already in progress to prevent duplicate execution
58+
if self._shutdown_event.is_set():
59+
logger.debug("[Signal] SIGTERM ignored - shutdown already in progress")
60+
return
61+
5262
logger.info("[Signal] SIGTERM received")
5363
self._shutdown_type = ShutdownType.SIGTERM
5464
self._shutdown_event.set()
@@ -111,17 +121,6 @@ def get_shutdown_elapsed_time(self) -> Optional[float]:
111121
return None
112122
return time.time() - self._shutdown_start_time
113123

114-
def trigger_manual_shutdown(self, shutdown_type: ShutdownType = ShutdownType.MANUAL) -> None:
115-
"""Manually trigger shutdown without signal."""
116-
try:
117-
logger.info(f"[Manual Shutdown] Triggering manual shutdown: {shutdown_type.name}")
118-
self._shutdown_type = shutdown_type
119-
self._shutdown_event.set()
120-
self._start_shutdown_timer()
121-
self.on_shutdown(shutdown_type)
122-
except Exception as error:
123-
self.on_error(error)
124-
125124
@abstractmethod
126125
def on_shutdown(self, shutdown_type: ShutdownType) -> None:
127126
"""

0 commit comments

Comments
 (0)