Skip to content

Commit

Permalink
trick to execute atexit functions in case of SIGTERM
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Apr 24, 2020
1 parent 52c1beb commit f4cce68
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions psutil/tests/__init__.py
Expand Up @@ -20,6 +20,7 @@
import re
import select
import shutil
import signal
import socket
import stat
import subprocess
Expand Down Expand Up @@ -222,27 +223,6 @@ def attempt(exe):
_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
# ===================================================================
Expand Down Expand Up @@ -1238,3 +1218,36 @@ def copyload_shared_lib(suffix=""):
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))

0 comments on commit f4cce68

Please sign in to comment.