diff --git a/rhodecode/lib/rc_cache/cache_key_meta.py b/rhodecode/lib/rc_cache/cache_key_meta.py --- a/rhodecode/lib/rc_cache/cache_key_meta.py +++ b/rhodecode/lib/rc_cache/cache_key_meta.py @@ -19,22 +19,37 @@ # and proprietary license terms, please see https://rhodecode.com/licenses/ import os +import sys import atexit import logging import signal +import rhodecode log = logging.getLogger(__name__) cache_keys_by_pid = set() +def sigHandler(signo, frame): + """ + signals trigger sys.exit so there's a single handler to cleanup the code. + """ + if rhodecode.is_test: + return + + sys.exit(0) + + def free_cache_keys(*args): + from rhodecode.model.db import Session, CacheKey + + if rhodecode.is_test: + return + ssh_cmd = os.environ.get('RC_CMD_SSH_WRAPPER') if ssh_cmd: return - from rhodecode.model.db import Session, CacheKey - if cache_keys_by_pid: try: for cache_proc in set(cache_keys_by_pid): @@ -48,7 +63,8 @@ def free_cache_keys(*args): except Exception: log.exception('Failed to clear keys, exiting gracefully') +atexit.register(free_cache_keys) -atexit.register(free_cache_keys) -signal.signal(signal.SIGTERM, free_cache_keys) -signal.signal(signal.SIGINT, free_cache_keys) +signal.signal(signal.SIGTERM, sigHandler) +signal.signal(signal.SIGINT, sigHandler) +