From e0f8956a2be898dc2430cf1788bc24b3d8eee737 Mon Sep 17 00:00:00 2001 From: Priyanshu Maurya <101447544+PriyansuMaurya@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:40:09 +0000 Subject: [PATCH 1/2] docs: explain directory isolation in spawned workers Add an executable process-pool example and a spawned-process test covering independent child paths, explicit child isolation, file placement and restoration. --- .../+isolation-worker-processes.doc.rst | 1 + docs/howto.rst | 32 ++++++++++++ tests/test_testing.py | 49 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 docs/changelog/+isolation-worker-processes.doc.rst diff --git a/docs/changelog/+isolation-worker-processes.doc.rst b/docs/changelog/+isolation-worker-processes.doc.rst new file mode 100644 index 00000000..07b1753e --- /dev/null +++ b/docs/changelog/+isolation-worker-processes.doc.rst @@ -0,0 +1 @@ +Document how to isolate directories inside spawned worker processes. diff --git a/docs/howto.rst b/docs/howto.rst index 935c0c49..c9a3fae0 100644 --- a/docs/howto.rst +++ b/docs/howto.rst @@ -256,6 +256,38 @@ root until the child exits. A spawned child starts with the original paths. With inherits the server's state. Isolation applies to :data:`~platformdirs.PlatformDirs`. Other platform classes, such as :class:`~platformdirs.unix.Unix` on macOS, keep their original paths. +For a spawned worker, enter isolation inside the worker itself. Wrapping only the parent in ``isolated_dirs`` does not +redirect the child's paths. Define the worker at module level and protect process creation with the ``__main__`` +guard so the example works with ``spawn`` on every platform: + +.. code-block:: python + + from concurrent.futures import ProcessPoolExecutor + from multiprocessing import get_context + from pathlib import Path + from tempfile import TemporaryDirectory + + from platformdirs import PlatformDirs + from platformdirs.testing import isolated_dirs + + + def write_config(root: Path) -> Path: + with isolated_dirs(root): + config = PlatformDirs("MyApp").place_config_file("settings.toml") + config.write_text('theme = "dark"', encoding="utf-8") + return config + + + if __name__ == "__main__": + with TemporaryDirectory() as temporary: + root = Path(temporary) + with ProcessPoolExecutor(max_workers=1, mp_context=get_context("spawn")) as pool: + config = pool.submit(write_config, root / "worker").result() + assert config == root / "worker" / "user_config" / "MyApp" / "settings.toml" + +Give concurrent workers separate roots when their files must not overlap. Keep each root available until its workers +have exited, as the temporary directory above does. + Overriding directories with environment variables ================================================= diff --git a/tests/test_testing.py b/tests/test_testing.py index d412f183..1a8433e7 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -182,6 +182,55 @@ def test_isolated_dirs_restores_after_exception(tmp_path: Path) -> None: assert _resolve_all() == _ORIGINAL +def test_isolated_dirs_spawned_process(tmp_path: Path) -> None: + # A real script makes the worker importable under spawn, including on Windows. + script = tmp_path / "check_spawned_process.py" + code = """ +from multiprocessing import get_context +from pathlib import Path +from sys import argv + +import platformdirs +from platformdirs.testing import isolated_dirs + + +def check_child(parent_root: Path, child_root: Path) -> None: + dirs = platformdirs.PlatformDirs("app", version="1.0") + original = dirs.user_config_path + assert not original.is_relative_to(parent_root) + assert not platformdirs.user_cache_path("app").is_relative_to(parent_root) + with isolated_dirs(child_root): + assert dirs.user_config_path == child_root / "user_config/app/1.0" + assert platformdirs.user_cache_path("app") == child_root / "user_cache/app" + config = dirs.place_config_file("settings.toml") + config.write_text("enabled = true", encoding="utf-8") + assert dirs.user_config_path == original + + +if __name__ == "__main__": + parent_root, child_root = Path(argv[1]) / "parent", Path(argv[1]) / "child" + original = platformdirs.user_config_path("app") + with isolated_dirs(parent_root): + process = get_context("spawn").Process(target=check_child, args=(parent_root, child_root)) + process.start() + try: + process.join(timeout=10) + assert process.exitcode == 0, process.exitcode + finally: + if process.is_alive(): + process.kill() + process.join() + process.close() + assert platformdirs.user_config_path("app") == parent_root / "user_config/app" + assert not parent_root.exists() + assert platformdirs.user_config_path("app") == original + assert (child_root / "user_config/app/1.0/settings.toml").read_text(encoding="utf-8") == "enabled = true" + print("ok") +""" + script.write_text(code, encoding="utf-8") + assert check_output([sys.executable, str(script), str(tmp_path)], text=True, timeout=20).strip() == "ok" + + def test_isolated_dirs_nested_restores_outer(tmp_path: Path) -> None: with isolated_dirs(tmp_path / "outer"): with isolated_dirs(tmp_path / "inner"): From 789ce2de3d84d7ed1774ad57b31e75ac63d55733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Fri, 27 Nov 2026 12:17:38 -0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20test:=20tighten=20spawned=20?= =?UTF-8?q?worker=20docs=20and=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/howto.rst | 5 ++--- tests/test_testing.py | 17 ++++++----------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/docs/howto.rst b/docs/howto.rst index c9a3fae0..0166a6d8 100644 --- a/docs/howto.rst +++ b/docs/howto.rst @@ -256,9 +256,8 @@ root until the child exits. A spawned child starts with the original paths. With inherits the server's state. Isolation applies to :data:`~platformdirs.PlatformDirs`. Other platform classes, such as :class:`~platformdirs.unix.Unix` on macOS, keep their original paths. -For a spawned worker, enter isolation inside the worker itself. Wrapping only the parent in ``isolated_dirs`` does not -redirect the child's paths. Define the worker at module level and protect process creation with the ``__main__`` -guard so the example works with ``spawn`` on every platform: +To isolate a spawned worker, enter ``isolated_dirs`` inside the worker. Define the worker at module level and guard +process creation with ``if __name__ == "__main__"``, so the child can import the worker without starting another pool: .. code-block:: python diff --git a/tests/test_testing.py b/tests/test_testing.py index 1a8433e7..d19074dd 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -6,7 +6,7 @@ from contextlib import ExitStack from multiprocessing import get_context from pathlib import Path -from subprocess import check_output # ruff:ignore[suspicious-subprocess-import] # bound the deadlock regression +from subprocess import check_output # ruff:ignore[suspicious-subprocess-import] # run checks in a fresh interpreter from threading import Event from types import MappingProxyType from typing import TYPE_CHECKING, Final @@ -202,8 +202,7 @@ def check_child(parent_root: Path, child_root: Path) -> None: with isolated_dirs(child_root): assert dirs.user_config_path == child_root / "user_config/app/1.0" assert platformdirs.user_cache_path("app") == child_root / "user_cache/app" - config = dirs.place_config_file("settings.toml") - config.write_text("enabled = true", encoding="utf-8") + dirs.place_config_file("settings.toml").write_text("enabled = true", encoding="utf-8") assert dirs.user_config_path == original @@ -213,14 +212,10 @@ def check_child(parent_root: Path, child_root: Path) -> None: with isolated_dirs(parent_root): process = get_context("spawn").Process(target=check_child, args=(parent_root, child_root)) process.start() - try: - process.join(timeout=10) - assert process.exitcode == 0, process.exitcode - finally: - if process.is_alive(): - process.kill() - process.join() - process.close() + process.join(timeout=10) + process.kill() + process.join() + assert process.exitcode == 0, process.exitcode assert platformdirs.user_config_path("app") == parent_root / "user_config/app" assert not parent_root.exists() assert platformdirs.user_config_path("app") == original