[PATCH] osmo-gsm-tester[report]: Free resources and kill subprocesses when signal to stop is ...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Wed May 17 14:10:43 UTC 2017


Review at  https://gerrit.osmocom.org/2670

Free resources and kill subprocesses when signal to stop is received

Change-Id: I039e4d1908a04bf606b101ddc6a186ba67e6178e
---
M src/osmo-gsm-tester.py
M src/osmo_gsm_tester/suite.py
2 files changed, 29 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/70/2670/1

diff --git a/src/osmo-gsm-tester.py b/src/osmo-gsm-tester.py
index ddec926..b03ceca 100755
--- a/src/osmo-gsm-tester.py
+++ b/src/osmo-gsm-tester.py
@@ -68,10 +68,22 @@
 
 import sys
 import argparse
+from signal import *
 from osmo_gsm_tester import __version__
 from osmo_gsm_tester import trial, suite, log, config, trial_report
 
+def sig_handler_cleanup(signum, frame):
+    print("killed by signal %d" % signum)
+    # This sys.exit() will raise a SystemExit base exception at the current
+    # point of execution. Code must be prepared to clean system-wide resources
+    # by using the "finally" section. This allows at the end 'atexit' hooks to
+    # be called before exiting.
+    sys.exit(1)
+
 def main():
+
+    for sig in (SIGINT, SIGTERM, SIGQUIT, SIGPIPE, SIGHUP):
+        signal(sig, sig_handler_cleanup)
 
     parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawTextHelpFormatter)
     # Note: since we're using RawTextHelpFormatter to keep nicely separate
@@ -196,7 +208,7 @@
                     any_failed = True
                 report.write_junit_report()
                 trials_run.append((current_trial.name(), report))
-        except:
+        except Exception:
             current_trial.log_exn()
 
     sys.stderr.flush()
diff --git a/src/osmo_gsm_tester/suite.py b/src/osmo_gsm_tester/suite.py
index 4c99c5e..65e9470 100644
--- a/src/osmo_gsm_tester/suite.py
+++ b/src/osmo_gsm_tester/suite.py
@@ -296,17 +296,23 @@
 
     def run_tests(self, names=None):
         self.log('Suite run start')
-        if not self.reserved_resources:
-            self.reserve_resources()
+        # if sys.exit() called from SIGTINT handler, SystemExit base exception
+        # is raised. Make sure to clean resources/processes in the finally
+        # section:
         results = SuiteRun.Results(self)
-        for test in self.definition.tests:
-            if names and not test.name() in names:
-                test.set_skip()
-            else:
-                with self:
-                    test.run(self)
-            results.add_test(test)
-        self.stop_processes()
+        try:
+            if not self.reserved_resources:
+                self.reserve_resources()
+            for test in self.definition.tests:
+                if names and not test.name() in names:
+                    test.set_skip()
+                else:
+                    with self:
+                        test.run(self)
+                results.add_test(test)
+        finally:
+            self.stop_processes()
+
         return results.conclude()
 
     def remember_to_stop(self, process):

-- 
To view, visit https://gerrit.osmocom.org/2670
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I039e4d1908a04bf606b101ddc6a186ba67e6178e
Gerrit-PatchSet: 1
Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: report
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list