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/+dev-lock-durable-publish.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A failed sync or close while writing `eb-local.lock` leaves the previous lock in place. A new lock is published only after its bytes are durable, and a failed write removes its temporary file.
80 changes: 72 additions & 8 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1343,16 +1343,80 @@ const DevLock = struct {
modules: []const []const u8,
};

/// Create `name` in the working directory, or replace it atomically with its mode kept.
/// Close `fd` when it is still open, remove `tmp`, and restore `sys.last_errno`.
fn dropUnpublished(fd: ?sys.fd_t, tmp: []const u8) void {
const kept = sys.last_errno;
if (fd) |open_fd| sys.close(open_fd);
sys.unlinkat(fsx.cwd, tmp, 0) catch {};
sys.last_errno = kept;
}

/// Create `name` in the working directory, or replace it.
/// The bytes are written beside the final name and that name changes only
/// after the write, sync and close have succeeded. A failure removes the
/// temporary file and leaves a file that was already there.
fn writeCwdFile(a: Allocator, name: []const u8, text: []const u8) void {
if (fsx.statAt(fsx.cwd, name, true)) |_| {
fsx.replaceContents(a, fsx.cwd, name, text) catch |e| die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
} else |_| {
const fd = sys.openat(fsx.cwd, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .EXCL = true, .NOFOLLOW = true }, 0o644) catch |e|
die("cannot create {s} ({s})", .{ name, storage.describeError(e) });
sys.writeAll(fd, text) catch |e| die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
storage.closeWritten(fd) catch |e| die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
const existing: ?fsx.Stat = fsx.statAt(fsx.cwd, name, false) catch |e| switch (e) {
error.NotFound => null,
else => die("cannot write {s} ({s})", .{ name, storage.describeError(e) }),
};
if (existing) |st| if (!st.isReg()) {
const why: sys.Error = if (st.isLink()) error.SymLink else if (st.isDir()) error.IsDir else error.NotRegularFile;
die("cannot write {s} ({s})", .{ name, storage.describeError(why) });
};
var rnd: [6]u8 = undefined;
_ = sys.call(linux.getrandom, .{ &rnd, rnd.len, @as(u32, 0) }) catch |e|
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
const tmp = std.fmt.allocPrint(a, ".{s}.{d}.{x}.tmp", .{ name, linux.getpid(), std.mem.readInt(u48, &rnd, .little) }) catch oom();
const mode: linux.mode_t = if (existing) |st| @intCast(st.perm()) else 0o644;
const fd = sys.openat(fsx.cwd, tmp, .{
.ACCMODE = .WRONLY,
.CREAT = true,
.EXCL = true,
.NOFOLLOW = true,
}, mode) catch |e| die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
if (existing) |st| {
const now = fsx.fstat(fd) catch |e| {
dropUnpublished(fd, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
if (now.uid != st.uid or now.gid != st.gid) {
sys.fchown(fd, st.uid, st.gid) catch |e| switch (e) {
error.AccessDenied => {},
else => {
dropUnpublished(fd, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
},
};
}
sys.fchmod(fd, mode) catch |e| {
dropUnpublished(fd, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
}
sys.writeAll(fd, text) catch |e| {
dropUnpublished(fd, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
storage.fsyncWritten(fd) catch |e| {
dropUnpublished(fd, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
storage.closeWritten(fd) catch |e| {
dropUnpublished(null, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
sys.renameat(fsx.cwd, tmp, fsx.cwd, name) catch |e| {
dropUnpublished(null, tmp);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
const dir = fsx.openDir(fsx.cwd, ".", false) catch |e|
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
storage.fsyncWritten(dir) catch |e| {
sys.close(dir);
die("cannot write {s} ({s})", .{ name, storage.describeError(e) });
};
sys.close(dir);
}

/// `dev --lock`: run the dev lines in a bash child and record what loaded.
Expand Down
40 changes: 40 additions & 0 deletions tests/test_write_close_errors_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,46 @@ def test_dev_lock_reports_a_failed_close_of_a_new_lock_file(self):
self.assertEqual(got.returncode, 2, got.stdout + got.stderr)
self.assertIn(f"eb-local: cannot write eb-local.lock ({EIO_TEXT})", got.stderr)

def test_dev_lock_publishes_only_after_a_durable_close(self):
# A lost sync or close must not leave a lock `dev --locked` can read.
# A new file stays absent. A replacement keeps the bytes already there.
repo = self.fake_eessi()
extra = {"SLURM_JOB_ID": "", "EESSI_CVMFS_REPO": str(repo), "BASH_ENV": ""}
cwd = self.fresh_dir("lock-durable")
first = self.run_bin(["dev", "--lock", "demo/1.0"], cwd=cwd, **extra)
self.assertEqual(first.returncode, 0, first.stdout + first.stderr)
lock = cwd / "eb-local.lock"
kept = lock.read_bytes()

for hook in HOOKS:
with self.subTest(hook=hook, case="new"):
slug = "fsync" if "FSYNC" in hook else "close"
fresh = self.fresh_dir("lock-durable-new-" + slug)
created = self.run_bin(
["dev", "--lock", "demo/1.0"],
cwd=fresh,
**{hook: "EIO", **extra},
)
self.assertEqual(created.returncode, 2, created.stdout + created.stderr)
self.assertIn(f"eb-local: cannot write eb-local.lock ({EIO_TEXT})", created.stderr)
self.assertNotIn("wrote eb-local.lock", created.stderr)
self.assertFalse((fresh / "eb-local.lock").exists(), hook)
temps = [p.name for p in fresh.iterdir() if p.name.startswith(".eb-local.lock.")]
self.assertEqual(temps, [], hook)

replaced = self.run_bin(
["dev", "--lock", "demo/1.0"],
cwd=cwd,
EB_LOCAL_TEST_CLOSE_ERRNO="EIO",
**extra,
)
self.assertEqual(replaced.returncode, 2, replaced.stdout + replaced.stderr)
self.assertIn(f"eb-local: cannot write eb-local.lock ({EIO_TEXT})", replaced.stderr)
self.assertNotIn("wrote eb-local.lock", replaced.stderr)
self.assertEqual(lock.read_bytes(), kept)
temps = [p.name for p in cwd.iterdir() if p.name.startswith(".eb-local.lock.")]
self.assertEqual(temps, [])

def test_doctor_workspace_fails_the_probe_when_its_close_fails(self):
ws = self.fresh_dir("probe-ws")
ok = self.run_bin(["doctor", "--workspace", str(ws)], SLURM_JOB_ID="")
Expand Down
Loading