Skip to content

Commit 11a00ee

Browse files
committed
fix(kqueue): fix syscall error propagation in completion handling
Extract errno_to_result helper to consistently convert posix errno values to the negative i32 format expected by syscall_result, replacing ad-hoc inline conversions. Fix shutdown operation to properly convert errno on failure instead of passing raw syscall result, and add NOTCONN error handling for shutdown completions.
1 parent 7f80318 commit 11a00ee

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/backend/kqueue.zig

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub const Loop = struct {
198198
// If we're deleting then we create a deletion event and
199199
// queue the completion to notify cancellation.
200200
.deleting => if (c.kevent()) |ev| {
201-
const ecanceled = -1 * @as(i32, @intCast(@intFromEnum(posix.system.E.CANCELED)));
201+
const ecanceled = errno_to_result(.CANCELED);
202202
c.result = c.syscall_result(ecanceled);
203203
c.flags.state = .dead;
204204
self.completions.push(c);
@@ -743,7 +743,7 @@ pub const Loop = struct {
743743
},
744744

745745
// Any other error we report
746-
else => break :action .{ .result = result },
746+
else => |errno| break :action .{ .result = errno_to_result(errno) },
747747
}
748748
}
749749
},
@@ -805,7 +805,11 @@ pub const Loop = struct {
805805
.both => posix.SHUT.RDWR,
806806
});
807807

808-
break :action .{ .result = result };
808+
if (result >= 0) {
809+
break :action .{ .result = result };
810+
} else {
811+
break :action .{ .result = errno_to_result(posix.errno(result)) };
812+
}
809813
},
810814

811815
.close => |v| action: {
@@ -863,7 +867,7 @@ pub const Loop = struct {
863867
// We use EPERM as a way to note there is no thread
864868
// pool. We can change this in the future if there is
865869
// a better choice.
866-
const eperm = -1 * @as(i32, @intCast(@intFromEnum(posix.system.E.PERM)));
870+
const eperm = errno_to_result(.PERM);
867871
c.result = c.syscall_result(eperm);
868872
self.completions.push(c);
869873
return false;
@@ -1391,6 +1395,7 @@ pub const Completion = struct {
13911395
.shutdown = switch (errno) {
13921396
.SUCCESS => {},
13931397
.CANCELED => error.Canceled,
1398+
.NOTCONN => error.SocketNotConnected,
13941399
else => |err| posix.unexpectedErrno(err),
13951400
},
13961401
},
@@ -1847,6 +1852,11 @@ fn kevent_syscall(
18471852
}
18481853
}
18491854

1855+
/// Converts a posix errno to the negative i32 format expected by syscall_result.
1856+
inline fn errno_to_result(errno: posix.E) i32 {
1857+
return -@as(i32, @intCast(@intFromEnum(errno)));
1858+
}
1859+
18501860
/// kevent_init initializes a Kevent from an posix.Kevent. This is used when
18511861
/// the "ext" fields are zero.
18521862
inline fn kevent_init(ev: posix.Kevent) Kevent {

0 commit comments

Comments
 (0)