From d8d4248101eda56eceb22f2586e95aa4cae8f749 Mon Sep 17 00:00:00 2001 From: Romuald Brunet Date: Wed, 9 Feb 2022 11:38:46 +0100 Subject: [PATCH] mark_process_dead: avoid error with bad env When the multiprocess environemnt is not properly set-up (missing environment variable) and mark_process_dead is called, the method raises an error on path.join() because the path is None which may be problematic This commit simply ignores the cleanup when the variable is not present Signed-off-by: Romuald Brunet --- prometheus_client/multiprocess.py | 5 +++++ tests/test_multiprocess.py | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/prometheus_client/multiprocess.py b/prometheus_client/multiprocess.py index 6c953747..514d7784 100644 --- a/prometheus_client/multiprocess.py +++ b/prometheus_client/multiprocess.py @@ -156,6 +156,11 @@ def mark_process_dead(pid, path=None): """Do bookkeeping for when one process dies in a multi-process setup.""" if path is None: path = os.environ.get('PROMETHEUS_MULTIPROC_DIR', os.environ.get('prometheus_multiproc_dir')) + + if path is None: + # Avoid error when environment is not correctly set-up + return + for f in glob.glob(os.path.join(path, f'gauge_livesum_{pid}.db')): os.remove(f) for f in glob.glob(os.path.join(path, f'gauge_liveall_{pid}.db')): diff --git a/tests/test_multiprocess.py b/tests/test_multiprocess.py index 9ec0578f..29739997 100644 --- a/tests/test_multiprocess.py +++ b/tests/test_multiprocess.py @@ -60,7 +60,7 @@ def _value_class(self): return def tearDown(self): - del os.environ['PROMETHEUS_MULTIPROC_DIR'] + os.environ.pop('PROMETHEUS_MULTIPROC_DIR', None) shutil.rmtree(self.tempdir) values.ValueClass = MutexValue @@ -303,6 +303,11 @@ def test_missing_gauge_file_during_merge(self): os.path.join(self.tempdir, 'gauge_livesum_9999999.db'), ])) + def test_mark_dead_no_env(self): + del os.environ['PROMETHEUS_MULTIPROC_DIR'] + + mark_process_dead(123) + class TestMmapedDict(unittest.TestCase): def setUp(self):