Repository navigation
Refactor DNS resolution to use posix.getaddrinfo with IpAddressIterator - #112
Conversation
- Implemented custom getAddressListBlocking() using posix.getaddrinfo - Returns IpAddressList instead of std.net.AddressList - Removed unused canon_name field from IpAddressList - Added IpAddress.initPosix() method for safe sockaddr conversion - Uses @memcpy to handle alignment differences between sockaddr types - Added link_libc to build.zig as required for getaddrinfo - Maintains blocking approach via thread pool (getaddrinfo_a uses threads internally anyway)
- Replace IpAddressList with IpAddressIterator for zero-allocation iteration - Rename getAddressList to lookupHost for better semantics - Remove allocator parameter - uses stack-based FixedBufferAllocator internally - Update tcpConnectToHost to use iterator pattern - Use fully qualified names instead of posix/windows aliases - Single return statement in lookupHost - Updated tests to use iterator pattern
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughReplaced allocator-based address-list APIs with an iterator-based lookup (IpAddressIterator), added IpAddress.initPosix to construct from POSIX sockaddr, updated tcpConnectToHost to use the new lookup, and enabled linking the C standard library for the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
build.zig(1 hunks)src/io/net.zig(1 hunks)src/net.zig(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: test (windows-latest)
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/net.zig (1)
101-123: WSANO_DATA still needs mapping to HostLacksNetworkAddresses.Windows error code WSANO_DATA (11004) indicates the host has no usable network addresses but currently falls through to the else branch and surfaces as error.Unexpected. This breaks consistent handling across platforms. Add a case mapping
.WSANO_DATA => return error.HostLacksNetworkAddressesto mirror the POSIX.NODATAhandling at line 133.Apply this diff:
.WSAHOST_NOT_FOUND => return error.UnknownHostName, .WSATYPE_NOT_FOUND => return error.ServiceUnavailable, + .WSANO_DATA => return error.HostLacksNetworkAddresses, .WSAEINVAL => unreachable,
🧹 Nitpick comments (1)
src/net.zig (1)
81-86: Buffer size is adequate for typical use.The 512-byte stack buffer handles hostnames up to 253 characters (DNS limit) plus port formatting. Edge cases that exceed this will return OutOfMemory, which is already in the LookupHostError set. If you expect unusually long inputs, consider bumping to 768 or 1024 bytes, but this is optional.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/net.zig(3 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-10T05:59:17.087Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 41
File: src/udp.zig:79-82
Timestamp: 2025-10-10T05:59:17.087Z
Learning: In UDP socket operations (src/udp.zig), error.Canceled from result_data.result represents OS/IO-level cancellation (e.g., io_uring), not user-requested cancellation. User cancellation is handled by waitForXevCompletion() via try. OS-level error.Canceled should be mapped to error.Unexpected as it represents an unexpected system event.
Applied to files:
src/net.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: test (windows-latest)
🔇 Additional comments (5)
src/net.zig (5)
17-41: Iterator pattern is clean.The IpAddressIterator implementation correctly wraps the addrinfo linked list, filters unsupported address families, and handles platform-specific cleanup. The @intcast at line 27 will panic on overflow in debug/safe modes, which is the right behavior for malformed addrinfo data.
124-142: POSIX error handling is complete.All EAI error codes are properly mapped, programmer errors correctly marked unreachable, and SYSTEM errors delegate to errno handling. The .NODATA case at line 133 correctly maps to HostLacksNetworkAddresses, which is what Windows should also do.
150-167: tcpConnectToHost refactor is solid.Removing the allocator parameter and adopting the iterator pattern simplifies the code. The deferred cleanup ensures resources are freed, and the fallback logic correctly tries each address and returns UnknownHostName if the iterator is empty.
177-224: Tests correctly validate the iterator API.The updated tests properly use the iterator pattern with deferred cleanup, verify that results are returned for localhost and numeric IPs, and confirm that port numbers are preserved correctly.
282-340: Integration test validates the end-to-end flow.The tcpConnectToHost test correctly uses the updated API without the allocator parameter and confirms that the DNS lookup and connection flow works as expected.
Add error case for WSANO_DATA in Windows getaddrinfo error handling to match the POSIX .NODATA case. This ensures consistent behavior across platforms when a host has no usable network addresses. Co-authored-by: Lukáš Lalinský <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/net.zig (3)
27-27: Consider validating the cast or using a comptime check.The
@intCast(info.addrlen)assumes thataddrlenfits in the target type. While sockaddr lengths are typically small (16-28 bytes), you might want to add a debug assertion or use a safer cast pattern to catch unexpected values.Example with debug assertion:
- return IpAddress.initPosix(addr, @intCast(info.addrlen)); + const len: u32 = @intCast(info.addrlen); + std.debug.assert(len <= 128); // sanity check + return IpAddress.initPosix(addr, len);
106-106: Simplify the success case comparison.The pattern
@as(std.os.windows.ws2_32.WinsockError, @enumFromInt(0))in the switch case is unnecessarily verbose. You can simplify to just@enumFromInt(0)since the switch expression is already cast toWinsockError.- @as(std.os.windows.ws2_32.WinsockError, @enumFromInt(0)) => break, + @enumFromInt(0) => break,
167-167: Return a more accurate error when no addresses are available.If
lookupHostsucceeds butiter.next()never returns an address (either because the result was empty or all addresses were filtered out), returningerror.UnknownHostNameis misleading.UnknownHostNameimplies DNS resolution failed, but here the lookup succeeded—we just have no usable addresses. Consider returningerror.HostLacksNetworkAddressesinstead.- return error.UnknownHostName; + return error.HostLacksNetworkAddresses;
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/net.zig(3 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-10T05:59:17.087Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 41
File: src/udp.zig:79-82
Timestamp: 2025-10-10T05:59:17.087Z
Learning: In UDP socket operations (src/udp.zig), error.Canceled from result_data.result represents OS/IO-level cancellation (e.g., io_uring), not user-requested cancellation. User cancellation is handled by waitForXevCompletion() via try. OS-level error.Canceled should be mapped to error.Unexpected as it represents an unexpected system event.
Applied to files:
src/net.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: test (windows-latest)
Summary
Refactors DNS resolution to use
posix.getaddrinfodirectly with an iterator-based API, eliminating unnecessary allocations and improving the API ergonomics.Changes
Core refactoring
IpAddressIterator: New iterator struct that wraps the
addrinfolinked listnext() ?IpAddressfor zero-allocation iterationdeinit()handles platform-specific cleanup (Windowsws2_32.freeaddrinfovs POSIXsystem.freeaddrinfo)lookupHost: Renamed from
getAddressListfor better semanticsFixedBufferAllocatorfor temporary C stringsIpAddressIteratorby value instead of pointerIpAddress.initPosix(): New method for safe
sockaddrconversion*const sockaddrandsocklen_tparameters@memcpyto handle alignment differences betweensockaddr(align 2) andsockaddr_in/sockaddr_in6(align 4)Implementation details
posix.system.getaddrinfoon POSIX systemsstd.os.windows.ws2_32.getaddrinfoon Windowslink_libc = trueto build.zig (required forgetaddrinfo)API improvements
tcpConnectToHostno longer needs allocator parameterTesting
Migration notes