[elbe-devel] [PATCH 2/6] elbepack: daemon: explicitly shut down daemon components

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Jun 25 16:25:34 CEST 2024


Instead of relying on the cherrypy message bus to call MySession.stop(),
which is really not obvious, perform explicit recource management in
daemon.run_command().

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/daemon.py       | 48 +++++++++++++++++++++------------------
 elbepack/daemons/soap/__init__.py |  1 -
 2 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/elbepack/commands/daemon.py b/elbepack/commands/daemon.py
index da811da8b717..e1ad8cf5a913 100644
--- a/elbepack/commands/daemon.py
+++ b/elbepack/commands/daemon.py
@@ -2,6 +2,7 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # SPDX-FileCopyrightText: 2014, 2017 Linutronix GmbH
 
+import contextlib
 import importlib
 from optparse import OptionParser
 from pkgutil import iter_modules
@@ -34,25 +35,28 @@ def run_command(argv):
 
     (opt, _) = oparser.parse_args(argv)
 
-    for d in daemons:
-        print(f'enable {d}')
-        module = 'elbepack.daemons.' + str(d)
-        cmdmod = importlib.import_module(module)
-        cherrypy.tree.graft(
-            cmdmod.get_app(cherrypy.engine), '/' + str(d))
-
-    cherrypy.server.unsubscribe()
-    server = cherrypy._cpserver.Server()
-    server.socket_host = opt.host
-    server.socket_port = opt.port
-    server.thread_pool = 30
-
-    # For SSL Support
-    # server.ssl_module            = 'pyopenssl'
-    # server.ssl_certificate       = 'ssl/certificate.crt'
-    # server.ssl_private_key       = 'ssl/private.key'
-    # server.ssl_certificate_chain = 'ssl/bundle.crt'
-
-    server.subscribe()
-    cherrypy.engine.start()
-    cherrypy.engine.block()
+    with contextlib.ExitStack() as stack:
+        for d in daemons:
+            print(f'enable {d}')
+            module = 'elbepack.daemons.' + str(d)
+            cmdmod = importlib.import_module(module)
+            app = cmdmod.get_app(cherrypy.engine)
+            if hasattr(app, 'stop'):
+                stack.callback(app.stop)
+            cherrypy.tree.graft(app, '/' + str(d))
+
+        cherrypy.server.unsubscribe()
+        server = cherrypy._cpserver.Server()
+        server.socket_host = opt.host
+        server.socket_port = opt.port
+        server.thread_pool = 30
+
+        # For SSL Support
+        # server.ssl_module            = 'pyopenssl'
+        # server.ssl_certificate       = 'ssl/certificate.crt'
+        # server.ssl_private_key       = 'ssl/private.key'
+        # server.ssl_certificate_chain = 'ssl/bundle.crt'
+
+        server.subscribe()
+        cherrypy.engine.start()
+        cherrypy.engine.block()
diff --git a/elbepack/daemons/soap/__init__.py b/elbepack/daemons/soap/__init__.py
index 0d28c509ef19..7062b6d02443 100644
--- a/elbepack/daemons/soap/__init__.py
+++ b/elbepack/daemons/soap/__init__.py
@@ -31,7 +31,6 @@ class MySession (SessionMiddleware, SimplePlugin):
         SessionMiddleware.__init__(self, app)
 
         SimplePlugin.__init__(self, engine)
-        self.subscribe()
 
     def stop(self):
         self.pm.stop()

-- 
2.45.2



More information about the elbe-devel mailing list