From ce9ec48e6dcd490f53076123873cd8cc7c50379e Mon Sep 17 00:00:00 2001 From: Hongkai Liu Date: Tue, 23 Jun 2026 14:51:30 -0400 Subject: [PATCH] OCPBUGS-91663: Clean up old temp directories in downloads pod Add cleanup logic to remove old download-* temp directories before creating a new one. This prevents accumulation of stale temp directories from previous pod instances. Changes: - Define TEMP_DIR_PREFIX constant for 'download-' prefix - Add cleanup logic to remove existing download-* directories on startup - Use prefix parameter in tempfile.mkdtemp() for easier identification Co-Authored-By: Claude Sonnet 4.5 --- .../deployments/downloads-deployment.yaml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bindata/assets/deployments/downloads-deployment.yaml b/bindata/assets/deployments/downloads-deployment.yaml index 5fd1a64dc..e5a8dafa1 100644 --- a/bindata/assets/deployments/downloads-deployment.yaml +++ b/bindata/assets/deployments/downloads-deployment.yaml @@ -79,7 +79,7 @@ spec: - '-c' - | cat </tmp/serve.py - import errno, http.server, os, re, signal, socket, sys, tarfile, tempfile, threading, time, zipfile + import errno, http.server, os, re, shutil, signal, socket, sys, tarfile, tempfile, threading, time, zipfile def shutdown_handler(signum, frame): print("Received signal {}, shutting down...".format(signum), flush=True) @@ -124,7 +124,22 @@ spec: httpd.serve_forever() print('Starting downloads server...', flush=True) - temp_dir = tempfile.mkdtemp() + + # Clean up any existing download-* directories + TEMP_DIR_PREFIX = 'download-' + tmp_base = tempfile.gettempdir() + print('Cleaning up old temp directories in {}...'.format(tmp_base), flush=True) + for item in os.listdir(tmp_base): + if item.startswith(TEMP_DIR_PREFIX): + old_dir = os.path.join(tmp_base, item) + try: + if os.path.isdir(old_dir): + print(' Removing old directory: {}'.format(old_dir), flush=True) + shutil.rmtree(old_dir) + except Exception as e: + print(' WARNING: Failed to remove {}: {}'.format(old_dir, str(e)), flush=True) + + temp_dir = tempfile.mkdtemp(prefix=TEMP_DIR_PREFIX) print('Serving from: {}'.format(temp_dir), flush=True) os.chdir(temp_dir)