Preserve getaddrinfo error code in SocketAddressError#3558
Open
thisismanan wants to merge 3 commits intoapple:mainfrom
Open
Preserve getaddrinfo error code in SocketAddressError#3558thisismanan wants to merge 3 commits intoapple:mainfrom
thisismanan wants to merge 3 commits intoapple:mainfrom
Conversation
Motivation: When getaddrinfo fails, SwiftNIO throws SocketAddressError.unknown which discards the error code. This makes it impossible for callers to distinguish between different failure reasons (e.g., EAI_NONAME vs EAI_AGAIN), which is needed for retry logic and diagnostics. Modifications: - Add SocketAddressError.UnknownHost struct that carries host, port, errorCode (from getaddrinfo), and errorDescription (from gai_strerror). - Update SocketAddress.makeAddressResolvingHost and GetaddrinfoResolver to throw/fail with the new error type on both POSIX and Windows paths. - Update existing test catch blocks to use the new error type. - Add testGetaddrinfoErrorCodeIsPreserved to verify the error code is captured Result: Callers can now inspect the getaddrinfo error code when host resolution fails, enabling more specific error handling.
Author
|
Hey @Lukasa, just checking if you've had a chance to look at this. Happy to make any changes! |
simonjbeaumont
requested changes
Apr 8, 2026
Contributor
simonjbeaumont
left a comment
There was a problem hiding this comment.
Thanks for this PR. Looks in pretty good shape to me. A few minor tweaks suggested.
To answer the open questions in your PR description...
Questions for reviewer
- Should
case unknown(host:port:)be deprecated? — This enum case is no longer thrown after this PR. Callers catching.unknownwill silently stop matching. Should I add
@available(*, deprecated, renamed: "UnknownHost")to provide a migration path?
I think that's a good idea. Not sure if the renamed: will provide the correct Fix-It since the replacement is not an enum case so a trivial source replacement may not be possible. Worth trying. If not, then a message: variant should be used.
- Should the doc comment on
makeAddressResolvingHostbe updated? — The- Throws:line still referencesSocketAddressError.unknowninstead of the new
SocketAddressError.UnknownHost.
Yes.
Motivation: Review feedback from PR apple#3558 suggested several improvements. Modifications: - Deprecate `case unknown(host:port:)` with @available annotation - Change errorCode type from CInt to Int (NIO public API convention) - Make UnknownHost init package access - Use guard instead of if for consistency - Update docstring to reference UnknownHost Result: Cleaner public API surface that follows NIO conventions.
simonjbeaumont
approved these changes
Apr 9, 2026
Contributor
simonjbeaumont
left a comment
There was a problem hiding this comment.
This LGTM. @weissi did you want to have a pass on this before it lands?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As observed by @weissi #3382
Motivation:
When
getaddrinfofails, SwiftNIO throwsSocketAddressError.unknownwhich discards the error code. This makes it impossible for callers todistinguish between different failure reasons (e.g.,
EAI_NONAMEvsEAI_AGAIN), which is needed for retry logic and diagnostics.Modifications:
SocketAddressError.UnknownHoststruct that carrieshost,port,errorCode(fromgetaddrinfo), anderrorDescription(fromgai_strerror).SocketAddress.makeAddressResolvingHostandGetaddrinfoResolverto throw/fail with the new error type on both POSIX and Windows paths.testGetaddrinfoErrorCodeIsPreservedto verify the error code is captured.Result:
Callers can now inspect the
getaddrinfoerror code when host resolution fails, enabling more specific error handling. Resolves #3382Questions for reviewer
Should
case unknown(host:port:)be deprecated? — This enum case is no longer thrown after this PR. Callers catching.unknownwill silently stop matching. Should I add@available(*, deprecated, renamed: "UnknownHost")to provide a migration path?Should the doc comment on
makeAddressResolvingHostbe updated? — The- Throws:line still referencesSocketAddressError.unknowninstead of the newSocketAddressError.UnknownHost.