diff --git a/app/nullius/execution.py b/app/nullius/execution.py index fcd04e579..03bf42d3e 100644 --- a/app/nullius/execution.py +++ b/app/nullius/execution.py @@ -1066,7 +1066,9 @@ def _copy_to(self, src: str, dest: str) -> tuple: def _copy_back(self, run: Optional[Callable] = None, timeout: float = 900.0) -> tuple: started = time.monotonic() copy = run or self._run - result = copy(["rsync", "-az", "-e", self._ssh_prefix(), f"{self._target()}:{self._remote('runs', self.plan.name, 'jobs')}/", self.jobs + "/"], timeout) + # a file being written there (ondisk.write_whole's .part, renamed once whole) would stay here: not copied + result = copy(["rsync", "-az", "--exclude=.*.part", "-e", self._ssh_prefix(), + f"{self._target()}:{self._remote('runs', self.plan.name, 'jobs')}/", self.jobs + "/"], timeout) if result[0] == 0 and self.plan.runner == STANDARD and self.catalogue_on_target: # Proofs write back to this run's definitions. Keep them beside # the job evidence, without replacing this user's catalogue. diff --git a/app/nullius/standard/runner/job.py b/app/nullius/standard/runner/job.py index 92ff8f21d..0b7dbb4df 100644 --- a/app/nullius/standard/runner/job.py +++ b/app/nullius/standard/runner/job.py @@ -232,17 +232,12 @@ def _version(bench: dict) -> dict: def _write(path: str, text: str) -> None: - """A trial file written as a new regular file: whatever is at the path - + """A trial file written whole (``ondisk.write_whole``) as a new regular file: whatever is at the path - a link the agent planted in a folder it could write, above all - is replaced, never written through (verifier/score.json -> ~/.bashrc).""" - if os.path.islink(path) or (os.path.lexists(path) and not os.path.isfile(path)): - if os.path.isdir(path) and not os.path.islink(path): - shutil.rmtree(path) - else: - os.unlink(path) - fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | getattr(os, "O_NOFOLLOW", 0), 0o644) - with os.fdopen(fd, "w") as f: - f.write(text) + if os.path.isdir(path) and not os.path.islink(path): + shutil.rmtree(path) # a rename replaces anything else, never a folder + ondisk.write_whole(path, text, mode=0o644) def mean_score(results: List[dict]): diff --git a/app/nullius/standard/runner/merge.py b/app/nullius/standard/runner/merge.py index 0b49eecb9..2e61c111f 100644 --- a/app/nullius/standard/runner/merge.py +++ b/app/nullius/standard/runner/merge.py @@ -333,9 +333,7 @@ def merge(job_dirs: List[str], out: str, force: bool = False) -> dict: res = _read(os.path.join(src, "result.json")) std = dict(res.get("standard") or {}, job=name, shard_job=s["name"]) res["standard"] = std - os.unlink(os.path.join(dest, "result.json")) - with open(os.path.join(dest, "result.json"), "w", encoding="utf-8") as f: - json.dump(res, f, indent=1) + ondisk.write_whole(os.path.join(dest, "result.json"), json.dumps(res, indent=1)) results.append(_read(os.path.join(out, t, "result.json"))) summary = result_of(shards, results) planned = summary["n_total_trials"] diff --git a/app/tests/test_standard_shard.py b/app/tests/test_standard_shard.py index 2a0f2aff6..2805b9c5f 100644 --- a/app/tests/test_standard_shard.py +++ b/app/tests/test_standard_shard.py @@ -91,7 +91,7 @@ def write_shard(root, name, units, index, of, *, total=10, launch="L1", model="g class JobFiles(unittest.TestCase): - """A job's config.json and result.json are read while a run goes on: a write that fails leaves the last whole one.""" + """A job's files, its trials' too, are read while a run goes on: a write that fails leaves the last whole one, or none.""" def test_a_write_that_fails_leaves_the_file_as_it_was(self): tmp = tempfile.mkdtemp(prefix="nullius-jobfiles-"); self.addCleanup(shutil.rmtree, tmp, True) @@ -106,6 +106,29 @@ def test_a_write_that_fails_leaves_the_file_as_it_was(self): self.assertEqual([open(p).read() for p in files], before) self.assertEqual(sorted(n for n in os.listdir(d) if not os.path.isdir(os.path.join(d, n))), ["config.json", "result.json"]) + def test_a_trial_file_whose_write_fails_halfway_is_left_as_it_was(self): + tmp = tempfile.mkdtemp(prefix="nullius-jobfiles-"); self.addCleanup(shutil.rmtree, tmp, True) + fdopen = os.fdopen + + def full(fd, *args, **kwargs): # the disk fills halfway through the write + f = fdopen(fd, *args, **kwargs) + write = f.write + + def half(text): + write(text[:len(text) // 2]); f.flush() + raise OSError(28, "No space left on device") + f.write = half + return f + + kept, new = os.path.join(tmp, "result.json"), os.path.join(tmp, "score.json") + with open(kept, "w") as f: + json.dump({"reward": 1.0}, f) + for path in (kept, new): + with patch("os.fdopen", full), self.assertRaises(OSError): + job._write(path, json.dumps({"reward": 0.0, "detail": {"ended": "done"}}, indent=1)) + self.assertEqual(os.listdir(tmp), ["result.json"]) + self.assertEqual(read(kept), {"reward": 1.0}) + class Merge(unittest.TestCase): def setUp(self):