# HG changeset patch # User RhodeCode Admin # Date 2023-01-18 21:11:31 # Node ID 5793cdd5ba28b231d4d35ea82d3f7fc8668245b1 # Parent 07a18b1189681c33367c9d753efb43dd775025a0 cache-keys: fix inifinite look of using atexit with signals. This prevented a grafecull shutdown of gunicorn workers. 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) +