fix: one thread at a time in the campaign cwd

This commit is contained in:
Abdessamad Derraz committed 2026-10-28 03:56:25 +01:00
1 parent ae5e377f21
commit 14b7fd1c98
2 files changed
+18 -7

No files matched your search

+15 -7
View File
@@ -69,6 +69,7 @@ import stat
import subprocess
import sys
import tarfile
import threading
import time
import urllib.error
import urllib.request
@@ -454,15 +455,22 @@ def copy_regular(src: Path, dest: Path) -> None:
shutil.copyfileobj(handle, out)
# The working directory belongs to the process. Two passes in threads that
# each saved and restored it could leave the process in the other's root,
# then in a folder deleted with its workspace.
_CWD_LOCK = threading.Lock()
@contextlib.contextmanager
def working_directory(path: Path) -> Iterator[None]:
"""contextlib.chdir, which Python 3.9 lacks."""
before = Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(before)
"""contextlib.chdir, which Python 3.9 lacks, one thread at a time."""
with _CWD_LOCK:
before = Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(before)
@contextlib.contextmanager
+3
View File
@@ -1170,10 +1170,13 @@ class FakeCorePasses(Workspace):
c.host.run = timed
passes.append(c)
threads = [threading.Thread(target=c.run, args=(set(), None)) for c in passes]
cwd = Path.cwd()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
# Each pass moved into its own root and back: the process ends where it began.
self.assertEqual(Path.cwd(), cwd)
ends = {name: sorted(end for _, end in runs) for name, runs in spans.items()}
self.assertGreater(len(ends["a"]), 2)
self.assertGreater(len(ends["b"]), 2)