Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+submit-script-sync-close.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A failed sync or close while writing a submit script is an error, and that script is not handed to sbatch.
19 changes: 18 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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`, ...
Expand All @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions tests/test_submit_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading