summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-04-25 00:20:43 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-04-25 00:20:43 +0200
commitf4cce6862758cc4502553c05cc04bbc5055b2982 (patch)
tree1490f19140237afc7f4b68140919be1ed1b089fe
parent52c1bebf35d6b4e6a315be72589b0d244ee023f5 (diff)
downloadpsutil-f4cce6862758cc4502553c05cc04bbc5055b2982.tar.gz
trick to execute atexit functions in case of SIGTERM
-rw-r--r--psutil/tests/__init__.py55
1 files changed, 34 insertions, 21 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 187552d5..ec406a27 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -20,6 +20,7 @@ import random
import re
import select
import shutil
+import signal
import socket
import stat
import subprocess
@@ -222,27 +223,6 @@ _pids_started = set()
_testfiles_created = set()
-# --- exit funs (first is executed last)
-
-atexit.register(DEVNULL.close)
-
-
-@atexit.register
-def cleanup_test_files():
- while _testfiles_created:
- path = _testfiles_created.pop()
- try:
- safe_rmpath(path)
- except Exception:
- traceback.print_exc()
-
-
-# this is executed first
-@atexit.register
-def cleanup_test_procs():
- reap_children(recursive=True)
-
-
# ===================================================================
# --- threads
# ===================================================================
@@ -1238,3 +1218,36 @@ else:
if ret == 0:
WinError()
safe_rmpath(dst)
+
+
+# ===================================================================
+# --- Exit funs (first is executed last)
+# ===================================================================
+
+
+atexit.register(DEVNULL.close)
+
+
+@atexit.register
+def cleanup_test_files():
+ while _testfiles_created:
+ path = _testfiles_created.pop()
+ try:
+ safe_rmpath(path)
+ except Exception:
+ traceback.print_exc()
+
+
+# this is executed first
+@atexit.register
+def cleanup_test_procs():
+ reap_children(recursive=True)
+
+
+# atexit module does not execute exit functions in case of SIGTERM, which
+# gets sent to test subprocesses, which is a problem if they import this
+# modul. With this it will. See:
+# http://grodola.blogspot.com/
+# 2016/02/how-to-always-execute-exit-functions-in-py.html
+if POSIX and 'PSUTIL_TESTING' in os.environ:
+ signal.signal(signal.SIGTERM, lambda sig, frame: sys.exit(sig))