summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2025-12-11 11:14:09 -0500
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-11 12:11:47 -0500
commit2b808d567de43d8ec591d9eb688ae66055e3aefc (patch)
tree96d1ca4ac9855da2a5301f636462f432edbee61d
parent728e3067a647eff81615fcc3bcff4e01cb028272 (diff)
[6.0.x] Refs #27890 -- Avoided overwriting TMPDIR in runtests.py under forkserver mode.
This variable should only be set once. Under forkserver, this module was getting executed multiple times, causing nested temporary dirs that didn't clean up properly, raising FileNotFoundError. This similar to #27890 although a slightly different cause. Backport of cd6278c4c09e4af9b2988d585b372d9abeeb63ee from main.
-rwxr-xr-xtests/runtests.py25
1 files changed, 9 insertions, 16 deletions
diff --git a/tests/runtests.py b/tests/runtests.py
index 679f5269ca..1936c46aab 100755
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -65,15 +65,6 @@ RUNTESTS_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIR = os.path.join(RUNTESTS_DIR, "templates")
-# Create a specific subdirectory for the duration of the test suite.
-TMPDIR = tempfile.mkdtemp(prefix="django_")
-# Set the TMPDIR environment variable in addition to tempfile.tempdir
-# so that children processes inherit it.
-tempfile.tempdir = os.environ["TMPDIR"] = TMPDIR
-
-# Removing the temporary TMPDIR.
-atexit.register(shutil.rmtree, TMPDIR)
-
# Add variables enabling coverage to trace code in subprocesses.
os.environ["RUNTESTS_DIR"] = RUNTESTS_DIR
os.environ["COVERAGE_PROCESS_START"] = os.path.join(RUNTESTS_DIR, ".coveragerc")
@@ -201,6 +192,7 @@ def get_filtered_test_modules(start_at, start_after, gis_enabled, test_labels=No
def setup_collect_tests(start_at, start_after, test_labels=None):
+ TMPDIR = os.environ["TMPDIR"]
state = {
"INSTALLED_APPS": settings.INSTALLED_APPS,
"ROOT_URLCONF": getattr(settings, "ROOT_URLCONF", ""),
@@ -328,13 +320,6 @@ def setup_run_tests(verbosity, start_at, start_after, test_labels=None):
def teardown_run_tests(state):
teardown_collect_tests(state)
- # Discard the multiprocessing.util finalizer that tries to remove a
- # temporary directory that's already removed by this script's
- # atexit.register(shutil.rmtree, TMPDIR) handler. Prevents
- # FileNotFoundError at the end of a test run (#27890).
- from multiprocessing.util import _finalizer_registry
-
- _finalizer_registry.pop((-100, 0), None)
del os.environ["RUNNING_DJANGOS_TEST_SUITE"]
@@ -535,6 +520,14 @@ def paired_tests(paired_test, options, test_labels, start_at, start_after):
if __name__ == "__main__":
+ # Create a specific subdirectory for the duration of the test suite.
+ TMPDIR = tempfile.mkdtemp(prefix="django_")
+ # Set the TMPDIR environment variable in addition to tempfile.tempdir
+ # so that children processes inherit it.
+ tempfile.tempdir = os.environ["TMPDIR"] = TMPDIR
+ # Remove the temporary TMPDIR.
+ atexit.register(shutil.rmtree, TMPDIR)
+
parser = argparse.ArgumentParser(description="Run the Django test suite.")
parser.add_argument(
"modules",