diff --git a/src/core/connection.zig b/src/core/connection.zig index a49c252..ec44610 100644 --- a/src/core/connection.zig +++ b/src/core/connection.zig @@ -432,10 +432,12 @@ pub const Connection = struct { // This was the last reference return true; } - // Safety check: underflow would indicate a bug + // Safety check: underflow is an invariant violation (double-free) if (old_count == 0) { - log.err("Connection reference count underflow! Double-free detected.", .{}); - return Error.InternalError; + // fetchSub already wrapped the counter — this is unrecoverable corruption. + // Panic immediately so the bug is caught in development rather than + // silently propagated through the call chain. + @panic("Connection reference count underflow: double-free detected"); } return false; } diff --git a/src/core/socket.zig b/src/core/socket.zig index 1a2119c..39a7ad7 100644 --- a/src/core/socket.zig +++ b/src/core/socket.zig @@ -156,13 +156,13 @@ pub const Socket = struct { } const domain: u32 = switch (self.config.protocol) { - .tcp, .udp => 2, // AF_INET - .unix_stream, .unix_dgram => 1, // AF_UNIX + .tcp, .udp => posix.AF.INET, + .unix_stream, .unix_dgram => posix.AF.UNIX, }; const sock_type: u32 = switch (self.config.protocol) { - .tcp, .unix_stream => 1, // SOCK_STREAM - .udp, .unix_dgram => 2, // SOCK_DGRAM + .tcp, .unix_stream => posix.SOCK.STREAM, + .udp, .unix_dgram => posix.SOCK.DGRAM, }; self.fd = posix.socket(domain, sock_type, 0) catch |err| { @@ -192,9 +192,10 @@ pub const Socket = struct { log.err("CRITICAL: Failed to set non-blocking mode: {}", .{err}); self.stats.recordError(); // Close the socket since it's in an invalid state + // Set state BEFORE closing FD to prevent another thread from using a closed FD + self.state = .closed; posix.close(self.fd); self.fd = -1; - self.state = .closed; return err; }; }