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/+lock-release-error.bugfix.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions changelog.d/+lock-release-unlock-test.misc.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion docs/orgmode/reference/api.org
Original file line number Diff line number Diff line change
Expand Up @@ -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]] |
Expand Down
32 changes: 25 additions & 7 deletions src/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};

Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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);
Expand Down
68 changes: 52 additions & 16 deletions src/state_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -73,26 +73,26 @@ 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" {
const root = begin();
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);
}

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);
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand All @@ -280,12 +280,48 @@ 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);
}

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);
Expand All @@ -303,7 +339,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));
Expand Down Expand Up @@ -418,7 +454,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" {
Expand Down Expand Up @@ -488,7 +524,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());
}
Expand All @@ -508,7 +544,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());
}
7 changes: 5 additions & 2 deletions tests/fs_lock_parity_probe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
Loading