From af38047591ffcabe0287f493835d6cf964420a6e Mon Sep 17 00:00:00 2001 From: Shunping Huang Date: Thu, 14 May 2026 00:01:54 -0400 Subject: [PATCH 1/2] Isolate tests with unique temp path. --- .../utils/multi_process_shared_test.py | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/sdks/python/apache_beam/utils/multi_process_shared_test.py b/sdks/python/apache_beam/utils/multi_process_shared_test.py index 18ed49c6fa17..35a7f9f17964 100644 --- a/sdks/python/apache_beam/utils/multi_process_shared_test.py +++ b/sdks/python/apache_beam/utils/multi_process_shared_test.py @@ -23,11 +23,31 @@ import threading import unittest from typing import Any + +import pytest from unittest.mock import patch from apache_beam.utils import multi_process_shared +@pytest.fixture(autouse=True) +def isolate_multi_process_shared_tests(tmp_path, monkeypatch): + """Isolates MultiProcessShared tests by using a unique temp directory per test. + + This prevents tests running in parallel (e.g. with pytest-xdist) from + interfering with each other by writing to the same shared default temp directory. + """ + orig_init = multi_process_shared.MultiProcessShared.__init__ + + def mock_init(self, constructor, tag, *args, **kwargs): + if 'path' not in kwargs: + kwargs['path'] = str(tmp_path) + return orig_init(self, constructor, tag, *args, **kwargs) + + monkeypatch.setattr( + multi_process_shared.MultiProcessShared, '__init__', mock_init) + + class CallableCounter(object): def __init__(self, start=0): self.running = start @@ -285,22 +305,6 @@ def test_release_always_proxy(self): class MultiProcessSharedSpawnProcessTest(unittest.TestCase): - def setUp(self): - tempdir = tempfile.gettempdir() - for tag in ['basic', - 'main', - 'to_delete', - 'to_keep', - 'mix1', - 'mix2', - 'test_process_exit', - 'thundering_herd_test', - 'transient_test']: - for ext in ['', '.address', '.address.error']: - try: - os.remove(os.path.join(tempdir, tag + ext)) - except OSError: - pass def tearDown(self): for p in multiprocessing.active_children(): From 3a3135c5d0bb92b1e0dc2ef7a3d48272d581c328 Mon Sep 17 00:00:00 2001 From: Shunping Huang Date: Thu, 14 May 2026 08:46:23 -0400 Subject: [PATCH 2/2] Fix lints. --- sdks/python/apache_beam/utils/multi_process_shared_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sdks/python/apache_beam/utils/multi_process_shared_test.py b/sdks/python/apache_beam/utils/multi_process_shared_test.py index 35a7f9f17964..c597e459e1a1 100644 --- a/sdks/python/apache_beam/utils/multi_process_shared_test.py +++ b/sdks/python/apache_beam/utils/multi_process_shared_test.py @@ -23,9 +23,9 @@ import threading import unittest from typing import Any +from unittest.mock import patch import pytest -from unittest.mock import patch from apache_beam.utils import multi_process_shared @@ -305,7 +305,6 @@ def test_release_always_proxy(self): class MultiProcessSharedSpawnProcessTest(unittest.TestCase): - def tearDown(self): for p in multiprocessing.active_children(): if p.is_alive():