Skip to content

Commit f1aea59

Browse files
committed
fix(ci): make test-barrier writes atomic
Path.write_text is not atomic: open(..., "w") publishes a zero-byte file that a reader polling for existence can observe before the content lands at close. The parallel harness contract polls for "<suite>.ready" and then parses its content as the leader pid, so it could win that gap and hit ValueError: invalid literal for int() with base 10: ''. Windows CI hit this on the test-windows CLANG64 2/2 shard, which then produced no shard manifest and cascaded into shard-completeness and ci-ok. Write the barrier files to a same-directory temp path and os.replace() them onto the destination, which is atomic on POSIX and Windows. A reader now sees either no file or complete content. Signed-off-by: Kody <kaidi.shi.1121@gmail.com>
1 parent d846808 commit f1aea59

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎scripts/run-test-wave.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def append_log(path: pathlib.Path, message: str) -> None:
9191
stream.write("\n")
9292

9393

94+
def _atomic_write_text(path: pathlib.Path, text: str) -> None:
95+
temporary = path.parent / f".{path.name}.{os.getpid()}.tmp"
96+
temporary.write_text(text, encoding="utf-8")
97+
os.replace(temporary, path)
98+
99+
94100
def start_suite(
95101
suite: str,
96102
runner_command: list[str],
@@ -255,12 +261,12 @@ def wait_for_test_pre_terminate_barrier(
255261
ready = barrier_dir / f"{active.name}.ready"
256262
leader_exited = barrier_dir / f"{active.name}.leader-exited"
257263
release = barrier_dir / f"{active.name}.release"
258-
ready.write_text(f"{active.process.pid}\n", encoding="utf-8")
264+
_atomic_write_text(ready, f"{active.process.pid}\n")
259265
deadline = time.monotonic() + 10
260266
while not release.exists():
261267
returncode = active.process.poll()
262268
if returncode is not None and not leader_exited.exists():
263-
leader_exited.write_text(f"{returncode}\n", encoding="utf-8")
269+
_atomic_write_text(leader_exited, f"{returncode}\n")
264270
if time.monotonic() >= deadline:
265271
raise RuntimeError(
266272
f"test pre-terminate barrier for {active.name!r} was not released"
@@ -279,7 +285,7 @@ def wait_for_test_post_exit_barrier(
279285
return
280286
ready = barrier_dir / f"{suite}.ready"
281287
release = barrier_dir / f"{suite}.release"
282-
ready.write_text("child exited; result intentionally not recorded\n", encoding="utf-8")
288+
_atomic_write_text(ready, "child exited; result intentionally not recorded\n")
283289
deadline = time.monotonic() + 10
284290
while not release.exists():
285291
if time.monotonic() >= deadline:

0 commit comments

Comments
 (0)