From ece7dbf9e6a81d3bb570c978adb4c6305afaade9 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Thu, 26 Nov 2026 21:55:20 +0000 Subject: [PATCH] fix: report submit script sync and close errors `writeReservedScript` wrote a batch script and closed it with `sys.close`, which ignores the result, then submit treated that as success and ran `sbatch`. On a network file system (NFS), a lost write surfaces only from `fsync` or `close`. Sync with `fsyncWritten`, close with `closeWritten`, and keep `last_errno` across the cleanup unlink. --- .../+submit-script-sync-close.bugfix.md | 1 + src/main.zig | 19 ++++++++++- tests/test_submit_cli.py | 33 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 changelog.d/+submit-script-sync-close.bugfix.md diff --git a/changelog.d/+submit-script-sync-close.bugfix.md b/changelog.d/+submit-script-sync-close.bugfix.md new file mode 100644 index 0000000..94040f0 --- /dev/null +++ b/changelog.d/+submit-script-sync-close.bugfix.md @@ -0,0 +1 @@ +A failed sync or close while writing a submit script is an error, and that script is not handed to sbatch. diff --git a/src/main.zig b/src/main.zig index 9185222..700afd3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6446,6 +6446,10 @@ fn submitReserved( var rollback_dirs = false; var created: std.ArrayList([]const u8) = .empty; errdefer { + // Cleanup runs before the failure is described. A failed unlink + // must not replace the errno of the write, sync or close. + const kept = sys.last_errno; + defer sys.last_errno = kept; if (rollback_dirs) { undoSubmitReserve(a, jobs_dir, logs_dir, jobs_missing, logs_missing, created.items); } else { @@ -6655,6 +6659,9 @@ fn reserveSubmitScript(a: Allocator, preferred: []const u8, fault: *SubmitFault) } /// Write `body` into a file `reserveSubmitScript` already created. +/// The bytes are synced and the close is checked. NFS reports a lost +/// write from those calls, so either failure is `error.SubmitFailed` +/// and the script is not handed to sbatch. fn writeReservedScript(path: []const u8, body: []const u8, fault: *SubmitFault) error{SubmitFailed}!void { const fd = sys.openat(fsx.cwd, path, .{ .ACCMODE = .WRONLY, @@ -6669,7 +6676,15 @@ fn writeReservedScript(path: []const u8, body: []const u8, fault: *SubmitFault) fault.* = .{ .kind = .write, .path = path, .err = e }; return error.SubmitFailed; }; - sys.close(fd); + storage.fsyncWritten(fd) catch |e| { + sys.close(fd); + fault.* = .{ .kind = .write, .path = path, .err = e }; + return error.SubmitFailed; + }; + storage.closeWritten(fd) catch |e| { + fault.* = .{ .kind = .write, .path = path, .err = e }; + return error.SubmitFailed; + }; } /// Write `script` at `preferred`, or at that path with a `-2`, `-3`, ... @@ -6679,7 +6694,9 @@ fn retainSubmitScript(a: Allocator, preferred: []const u8, script: []const u8) [ var fault: SubmitFault = undefined; const path = reserveSubmitScript(a, preferred, &fault) catch raiseSubmitFault(fault); writeReservedScript(path, script, &fault) catch { + const kept = sys.last_errno; sys.unlinkat(fsx.cwd, path, 0) catch {}; + sys.last_errno = kept; raiseSubmitFault(fault); }; return path; diff --git a/tests/test_submit_cli.py b/tests/test_submit_cli.py index 9059e89..aac89c0 100644 --- a/tests/test_submit_cli.py +++ b/tests/test_submit_cli.py @@ -354,6 +354,39 @@ def test_print_does_not_create_a_raced_submit_script(self): self.assertEqual(self.calls(), []) self.assertFalse((self.prefix / ".eb-local").exists()) + def test_submit_script_sync_or_close_failure_is_not_success(self): + # A lost write shows up from fsync or close. The script must not be + # handed to sbatch, and the errno must be the one those calls return. + jobs = self.prefix / ".eb-local" / "jobs" + calls = self.root / "sbatch-calls" + + def reset(): + if calls.exists(): + calls.unlink() + if jobs.exists(): + for path in jobs.iterdir(): + path.unlink() + + control = self.run_tool(["submit", "--on", "cpu", "recipe.eb"], SLURM_JOB_ID="") + self.assertEqual(control.returncode, 0, control.stdout + control.stderr) + self.assertEqual(len(self.calls()), 1) + reset() + + for hook in ("EB_LOCAL_TEST_FSYNC_ERRNO", "EB_LOCAL_TEST_CLOSE_ERRNO"): + with self.subTest(hook=hook): + reset() + got = self.run_tool( + ["submit", "--on", "cpu", "recipe.eb"], + SLURM_JOB_ID="", + **{hook: "EIO"}, + ) + self.assertEqual(got.returncode, 2, got.stdout + got.stderr) + self.assertIn("cannot write ", got.stderr) + self.assertIn("[Errno 5] Input/output error", got.stderr) + self.assertNotIn("Submitted batch job", got.stdout) + self.assertEqual(self.calls(), []) + self.assertEqual(list(jobs.iterdir()), []) + def test_sbatch_directives_quote_paths_containing_spaces(self): spaced_cwd = self.root / "work dir" spaced_cwd.mkdir()