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/+flock-eintr-retry.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An interrupted non-blocking flock is retried instead of reported as a busy lock.
6 changes: 4 additions & 2 deletions src/state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pub fn lockExclusiveCancel(fd: fsx.fd_t, timeout_ms: ?u32, cancel: ?*const std.a
const rc = lock_ops.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB);
switch (linux.errno(rc)) {
.SUCCESS => return,
.AGAIN, .INTR => {},
.INTR => continue,
.AGAIN => {},
else => return mapped(rc),
}
var sleep_ms: i64 = 20;
Expand Down Expand Up @@ -302,7 +303,8 @@ pub const RootLock = struct {
const rc = lock_ops.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB);
switch (linux.errno(rc)) {
.SUCCESS => return,
.AGAIN, .ACCES, .INTR => {},
.INTR => continue,
.AGAIN, .ACCES => {},
else => return mapped(rc),
}
if (try pause(d)) return error.Busy;
Expand Down
29 changes: 29 additions & 0 deletions src/state_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ test "failed local contenders keep the record lock" {
try assertRemoteExcluded(root);
}

var eintr_left: u8 = 0;

fn eintrFlock(fd: fsx.fd_t, op: i32) usize {
if (eintr_left > 0 and (op & std.posix.LOCK.NB) != 0) {
eintr_left -= 1;
return err(.INTR);
}
return linux.flock(fd, op);
}

test "an interrupted non-blocking flock is retried" {
const root = begin();
defer end(root);
eintr_left = 1;
state.lock_ops.flock = eintrFlock;
const held = try RootLock.acquire(A, root, 0);
held.release();
try testing.expectEqual(@as(u8, 0), eintr_left);

eintr_left = 1;
const dir = try fsx.openDir(fsx.cwd, root, false);
defer sys.close(dir);
const fd = (try state.openPrivate(dir, "extra", true)).?;
defer sys.close(fd);
try state.lockExclusive(fd, 0);
_ = linux.flock(fd, std.posix.LOCK.UN);
try testing.expectEqual(@as(u8, 0), eintr_left);
}

test "a second acquire by the holder keeps both locks" {
const root = begin();
defer end(root);
Expand Down
Loading