From 167e899599b9ebd328fa0fa88152310d9859d12b Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Fri, 20 Nov 2026 06:36:00 +0000 Subject: [PATCH 1/2] fix: report a failed release of the state-root lock `RootLock.release` ignored a failed `fcntl(F_SETLK, F_UNLCK)` or `flock(LOCK_UN)`, so the caller exited successfully. The Python reference closes both descriptors and then lets the `OSError` out. `release` returns `sys.Error!void` and does the same: both descriptors are closed whatever either lock call returns, and the first error is returned after the second close. `sys.last_errno` is restored to the errno of that first error, so `sys.describe` names the call whose error is returned. The `rootlock` mode of the filesystem parity probe reports a failed release in its status, and the API reference lists `sys.Error` for `release`, the set its signature names. --- changelog.d/+lock-release-error.bugfix.md | 1 + docs/orgmode/reference/api.org | 2 +- src/state.zig | 32 ++++++++++++++++++----- src/state_test.zig | 32 +++++++++++------------ tests/fs_lock_parity_probe.zig | 7 +++-- 5 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 changelog.d/+lock-release-error.bugfix.md diff --git a/changelog.d/+lock-release-error.bugfix.md b/changelog.d/+lock-release-error.bugfix.md new file mode 100644 index 0000000..88986b2 --- /dev/null +++ b/changelog.d/+lock-release-error.bugfix.md @@ -0,0 +1 @@ +`state.RootLock.release` reports a failed release of the state-root lock. It closes both lock files and returns the first error as `sys.Error`. It used to drop that error, so callers that discarded the result have to handle it. diff --git a/docs/orgmode/reference/api.org b/docs/orgmode/reference/api.org index 4f6e9b7..bb4deab 100644 --- a/docs/orgmode/reference/api.org +++ b/docs/orgmode/reference/api.org @@ -130,7 +130,7 @@ Error sets: [[zig:zig_common.state.Error]], [[zig:zig_common.state.RootLock.Acqu | Function | Error set | |---------------------------------------------+---------------------------------------------------| | [[zig:zig_common.state.RootLock.acquire]] | [[zig:zig_common.state.RootLock.AcquireError]] | -| [[zig:zig_common.state.RootLock.release]] | none | +| [[zig:zig_common.state.RootLock.release]] | [[zig:zig_common.sys.Error]] | | [[zig:zig_common.state.privateOk]] | none | | [[zig:zig_common.state.openPrivate]] | [[zig:zig_common.state.Error]] | | [[zig:zig_common.state.lockExclusive]] | [[zig:zig_common.state.LockError]] | diff --git a/src/state.zig b/src/state.zig index edc6159..d8e9528 100644 --- a/src/state.zig +++ b/src/state.zig @@ -591,16 +591,34 @@ pub const RootLock = struct { /// Unlock the record lock, close its descriptor, then unlock and close /// the gate. Both descriptors are closed whatever an unlock returns; a - /// forked child only closes them. - pub fn release(l: RootLock) void { + /// forked child only closes them. The first unlock error is returned + /// after both closes, as Python's release lets the OSError out, and + /// `sys.last_errno` holds that error's errno, not a later one's. + pub fn release(l: RootLock) sys.Error!void { + var first: ?sys.Error = null; + var first_errno: linux.E = .SUCCESS; if (linux.getpid() == l.owner) { var fl: linux.Flock = std.mem.zeroes(linux.Flock); fl.type = linux.F.UNLCK; - _ = lock_ops.fcntl(l.posix, linux.F.SETLK, @intFromPtr(&fl)); + _ = lockCall(lock_ops.fcntl, .{ l.posix, linux.F.SETLK, @intFromPtr(&fl) }) catch |err| { + first = err; + first_errno = sys.last_errno; + }; } sys.close(l.posix); - if (linux.getpid() == l.owner) _ = lock_ops.flock(l.gate, std.posix.LOCK.UN); + if (linux.getpid() == l.owner) { + _ = lockCall(lock_ops.flock, .{ l.gate, std.posix.LOCK.UN }) catch |err| { + if (first == null) { + first = err; + first_errno = sys.last_errno; + } + }; + } sys.close(l.gate); + if (first) |err| { + sys.last_errno = first_errno; + return err; + } } }; @@ -781,9 +799,9 @@ test "RootLock refuses a second holder and is free after release" { defer fsx.deleteTree(gpa, fsx.cwd, base) catch {}; const first = try RootLock.acquire(gpa, base, 0); try std.testing.expectError(error.Busy, RootLock.acquire(gpa, base, 0)); - first.release(); + first.release() catch unreachable; const again = try RootLock.acquire(gpa, base, 0); - again.release(); + again.release() catch unreachable; } test "lockExclusiveCancel returns Canceled when cancel is set" { @@ -859,7 +877,7 @@ test "creating the gate in a read-only directory names .lock" { const cwd = try fsx.realpath(fsx.cwd, ".", &cwd_buf); const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-ro-{d}", .{ cwd, linux.getpid() }); const held = try RootLock.acquire(gpa, base, 0); - held.release(); + held.release() catch unreachable; var zb: [sys.path_max:0]u8 = undefined; const gate = try std.mem.print(zb[0..zb.len], "{s}/.lock", .{base}); try sys.unlinkat(fsx.cwd, gate, 0); diff --git a/src/state_test.zig b/src/state_test.zig index 09d5878..339abbf 100644 --- a/src/state_test.zig +++ b/src/state_test.zig @@ -56,7 +56,7 @@ fn assertRemoteExcluded(root: []const u8) !void { if (pid == 0) { state.lock_ops.flock = noFlock; if (RootLock.acquire(A, root, 0)) |l| { - l.release(); + l.release() catch unreachable; linux.exit_group(1); } else |e| linux.exit_group(if (e == error.Busy) 0 else 2); } @@ -73,7 +73,7 @@ fn assertGateHeld(root: []const u8) !void { fn acquireAndRelease(root: []const u8) !void { const l = try RootLock.acquire(A, root, 0); - l.release(); + l.release() catch unreachable; } test "the record lock excludes an independent kernel gate" { @@ -81,10 +81,10 @@ test "the record lock excludes an independent kernel gate" { defer end(root); const holder = try RootLock.acquire(A, root, 0); assertRemoteExcluded(root) catch |e| { - holder.release(); + holder.release() catch unreachable; return e; }; - holder.release(); + holder.release() catch unreachable; try acquireAndRelease(root); } @@ -92,7 +92,7 @@ test "failed local contenders keep the record lock" { const root = begin(); defer end(root); const holder = try RootLock.acquire(A, root, 0); - defer holder.release(); + defer holder.release() catch unreachable; for (0..4) |_| { try testing.expectError(error.Busy, RootLock.acquire(A, root, 0)); try testing.expect(state.osFailure() == null); @@ -116,7 +116,7 @@ test "an interrupted non-blocking flock is retried" { eintr_left = 1; state.lock_ops.flock = eintrFlock; const held = try RootLock.acquire(A, root, 0); - held.release(); + held.release() catch unreachable; try testing.expectEqual(@as(u8, 0), eintr_left); eintr_left = 1; @@ -133,7 +133,7 @@ test "a second acquire by the holder keeps both locks" { const root = begin(); defer end(root); const holder = try RootLock.acquire(A, root, 0); - defer holder.release(); + defer holder.release() catch unreachable; try testing.expectError(error.Busy, RootLock.acquire(A, root, 0)); try assertGateHeld(root); try assertRemoteExcluded(root); @@ -170,7 +170,7 @@ test "acquisition errors release the descriptors and the gate" { if (comptime std.mem.eql(u8, target, "ftruncate")) state.lock_ops.ftruncate = eioFtruncate; if (comptime std.mem.eql(u8, target, "write")) state.lock_ops.write = eioWrite; if (RootLock.acquire(A, root, null)) |l| { - l.release(); + l.release() catch unreachable; return error.TestUnexpectedResult; } else |_| {} const failure = state.osFailure() orelse unreachable; @@ -220,7 +220,7 @@ test "a short pid write does not reuse a stale errno" { const root = begin(); defer end(root); const initialized = try RootLock.acquire(A, root, 0); - initialized.release(); + initialized.release() catch unreachable; sys.last_errno = .DQUOT; state.lock_ops.write = shortWrite; try testing.expectError(error.NoSpace, RootLock.acquire(A, root, 0)); @@ -262,10 +262,10 @@ test "a forked child's release does not unlock the parent" { const root = begin(); defer end(root); const holder = try RootLock.acquire(A, root, 0); - defer holder.release(); + defer holder.release() catch unreachable; const pid: isize = @bitCast(linux.fork()); if (pid == 0) { - holder.release(); + holder.release() catch unreachable; linux.exit_group(0); } try testing.expectEqual(@as(u32, 0), waitExit(pid)); @@ -280,7 +280,7 @@ test "a release error still closes both layers" { const holder = try RootLock.acquire(A, root, 0); const before = try test_support.fdCount(A); state.lock_ops.fcntl = eioFcntl; - holder.release(); + try testing.expectError(error.Unexpected, holder.release()); state.lock_ops.* = .{}; try testing.expectEqual(before - 2, try test_support.fdCount(A)); try acquireAndRelease(root); @@ -303,7 +303,7 @@ test "a timed acquire does not wait forever" { const root = begin(); defer end(root); const holder = try RootLock.acquire(A, root, 0); - defer holder.release(); + defer holder.release() catch unreachable; var t0: linux.timespec = undefined; _ = linux.clock_gettime(.MONOTONIC, &t0); try testing.expectError(error.Busy, RootLock.acquire(A, root, 200)); @@ -418,7 +418,7 @@ test "a hard link planted while the gate is awaited stops the truncate" { state.lock_ops.* = .{}; try sys.unlinkat(dir, "alias", 0); const l = try RootLock.acquire(gpa, root, 0); - l.release(); + l.release() catch unreachable; } test "a gate moved to another name while it is awaited is not truncated" { @@ -488,7 +488,7 @@ test "both lock files are created mode 0600" { const root = try scratchRoot(gpa); defer removeRoot(gpa, root); const l = try RootLock.acquire(gpa, root, 0); - defer l.release(); + defer l.release() catch unreachable; try testing.expectEqual(@as(u32, 0o600), (try fsx.fstat(l.gate)).perm()); try testing.expectEqual(@as(u32, 0o600), (try fsx.fstat(l.posix)).perm()); } @@ -508,7 +508,7 @@ test "acquire tightens existing owned lock files to mode 0600" { try testing.expectEqual(@as(u32, 0o644), (try fsx.statAt(dir, ".lock.posix", false)).perm()); const l = try RootLock.acquire(gpa, root, 0); - defer l.release(); + defer l.release() catch unreachable; try testing.expectEqual(@as(u32, 0o600), (try fsx.fstat(l.gate)).perm()); try testing.expectEqual(@as(u32, 0o600), (try fsx.fstat(l.posix)).perm()); } diff --git a/tests/fs_lock_parity_probe.zig b/tests/fs_lock_parity_probe.zig index 0890e26..e26fcde 100644 --- a/tests/fs_lock_parity_probe.zig +++ b/tests/fs_lock_parity_probe.zig @@ -295,8 +295,11 @@ fn osFailMode() void { fn lockCase(name: []const u8, root: []const u8) void { if (state.RootLock.acquire(A, root, 0)) |held| { - held.release(); - emit(&.{ "rootlock", name, "OK", "", "" }); + const status = if (held.release()) |_| + "OK" + else |e| + std.fmt.allocPrint(A, "ERR:release:{s}", .{@errorName(e)}) catch unreachable; + emit(&.{ "rootlock", name, status, "", "" }); } else |e| { const status = std.fmt.allocPrint(A, "ERR:{s}", .{@errorName(e)}) catch unreachable; if (state.osFailure()) |failure| { From acd7fb49e7f86c317b549a2be50a8b30e0eab881 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Wed, 25 Nov 2026 07:52:49 +0000 Subject: [PATCH 2/2] test: check that `RootLock.release` returns a failed lock call The case uses `state.lock_ops` to make the `fcntl` release fail, the `flock` release fail, and both fail at once, each with its own errno. It then checks three things. `release` returns the record lock's error first. Neither descriptor stays open. The state root can be locked again. `sys.last_errno` and `sys.describe` must name the errno of the returned error, `.IO` when both fail. --- changelog.d/+lock-release-unlock-test.misc.md | 1 + src/state_test.zig | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 changelog.d/+lock-release-unlock-test.misc.md diff --git a/changelog.d/+lock-release-unlock-test.misc.md b/changelog.d/+lock-release-unlock-test.misc.md new file mode 100644 index 0000000..380e7a9 --- /dev/null +++ b/changelog.d/+lock-release-unlock-test.misc.md @@ -0,0 +1 @@ +Add a test that `state.RootLock.release` returns the error when the `fcntl` or `flock` release fails through `state.lock_ops`, returns the record lock error when both fail, and closes both lock descriptors in each case. diff --git a/src/state_test.zig b/src/state_test.zig index 339abbf..d1dbc4e 100644 --- a/src/state_test.zig +++ b/src/state_test.zig @@ -286,6 +286,42 @@ test "a release error still closes both layers" { try acquireAndRelease(root); } +fn einvalFlock(_: fsx.fd_t, _: i32) usize { + return err(.INVAL); +} + +fn isOpen(fd: fsx.fd_t) bool { + return linux.errno(linux.fcntl(fd, linux.F.GETFD, 0)) != .BADF; +} + +test "a failed unlock is returned with both descriptors closed" { + const root = begin(); + defer end(root); + const Case = struct { fcntl: bool, flock: bool, want: sys.Error, errno: linux.E, text: []const u8 }; + // with both unlocks failing, the record lock's error and errno are the + // ones returned and kept, not the gate's + for ([_]Case{ + .{ .fcntl = true, .flock = false, .want = error.Unexpected, .errno = .IO, .text = "[Errno 5] Input/output error" }, + .{ .fcntl = false, .flock = true, .want = error.InvalidArgument, .errno = .INVAL, .text = "[Errno 22] Invalid argument" }, + .{ .fcntl = true, .flock = true, .want = error.Unexpected, .errno = .IO, .text = "[Errno 5] Input/output error" }, + }) |c| { + const holder = try RootLock.acquire(A, root, 0); + const before = try test_support.fdCount(A); + if (c.fcntl) state.lock_ops.fcntl = eioFcntl; + if (c.flock) state.lock_ops.flock = einvalFlock; + sys.last_errno = .SUCCESS; + const released = holder.release(); + state.lock_ops.* = .{}; + try testing.expectError(c.want, released); + try testing.expectEqual(c.errno, sys.last_errno); + try testing.expectEqualStrings(c.text, sys.describe(c.want)); + try testing.expect(!isOpen(holder.posix)); + try testing.expect(!isOpen(holder.gate)); + try testing.expectEqual(before - 2, try test_support.fdCount(A)); + try acquireAndRelease(root); + } +} + test "a hard-linked sidecar is refused" { const root = begin(); defer end(root);