Skip to content

Preserve getaddrinfo error code in SocketAddressError#3558

Open
thisismanan wants to merge 3 commits intoapple:mainfrom
thisismanan:getaddrinfo-err-code
Open

Preserve getaddrinfo error code in SocketAddressError#3558
thisismanan wants to merge 3 commits intoapple:mainfrom
thisismanan:getaddrinfo-err-code

Conversation

@thisismanan
Copy link
Copy Markdown

@thisismanan thisismanan commented Mar 24, 2026

As observed by @weissi #3382

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. Resolves #3382

Questions for reviewer

  1. Should case unknown(host:port:) be deprecated? — This enum case is no longer thrown after this PR. Callers catching .unknown will silently stop matching. Should I add
    @available(*, deprecated, renamed: "UnknownHost") to provide a migration path?

  2. Should the doc comment on makeAddressResolvingHost be updated? — The - Throws: line still references SocketAddressError.unknown instead of the new
    SocketAddressError.UnknownHost.

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.
@thisismanan
Copy link
Copy Markdown
Author

Hey @Lukasa, just checking if you've had a chance to look at this. Happy to make any changes!

Copy link
Copy Markdown
Contributor

@simonjbeaumont simonjbeaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Should case unknown(host:port:) be deprecated? — This enum case is no longer thrown after this PR. Callers catching .unknown will 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.

  1. Should the doc comment on makeAddressResolvingHost be updated? — The - Throws: line still references SocketAddressError.unknown instead of the new
    SocketAddressError.UnknownHost.

Yes.

@simonjbeaumont simonjbeaumont added the 🆕 semver/minor Adds new public API. label Apr 8, 2026
thisismanan and others added 2 commits April 8, 2026 22:48
  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.
Copy link
Copy Markdown
Contributor

@simonjbeaumont simonjbeaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. @weissi did you want to have a pass on this before it lands?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getaddrinfo resolver throws away error code

2 participants