fix(backend): Cleanup service on service closure (#9735)

The cleanup command was only called on SIGTERM, making it possible for
the service to close without being cleaned. Risking the connection not
being proactively closed when the service is unused.

### Changes 🏗️

Call the cleanup command on the service finally block.

### Checklist 📋

#### For code changes:
- [x] I have clearly listed my changes in the PR description
- [x] I have made a test plan
- [x] I have tested my changes according to the test plan:
  - [x] Run the service, stop it, see the log is printed (locally)
This commit is contained in:
Zamil Majdy
2025-04-02 08:21:40 +04:00
committed by GitHub
parent d879df062e
commit 200e5814b3

View File

@@ -28,6 +28,7 @@ class AppProcess(ABC):
"""
process: Optional[Process] = None
cleaned_up = False
set_start_method("spawn", force=True)
configure_logging()
@@ -62,6 +63,7 @@ class AppProcess(ABC):
def execute_run_command(self, silent):
signal.signal(signal.SIGTERM, self._self_terminate)
signal.signal(signal.SIGINT, self._self_terminate)
try:
if silent:
@@ -73,9 +75,16 @@ class AppProcess(ABC):
self.run()
except (KeyboardInterrupt, SystemExit) as e:
logger.warning(f"[{self.service_name}] Terminated: {e}; quitting...")
finally:
if not self.cleaned_up:
self.cleanup()
self.cleaned_up = True
logger.info(f"[{self.service_name}] Terminated.")
def _self_terminate(self, signum: int, frame):
self.cleanup()
if not self.cleaned_up:
self.cleanup()
self.cleaned_up = True
sys.exit(0)
# Methods that are executed OUTSIDE the process #