Skip to content

socketpool: let ports raise gaierror with the real error code - #11215

Open
mikeysklar wants to merge 1 commit into
adafruit:mainfrom
mikeysklar:fix/socketpool-gaierror-real-code
Open

socketpool: let ports raise gaierror with the real error code#11215
mikeysklar wants to merge 1 commit into
adafruit:mainfrom
mikeysklar:fix/socketpool-gaierror-real-code

Conversation

@mikeysklar

Copy link
Copy Markdown
Collaborator

What

common_hal_socketpool_socketpool_raise_gaierror_noname() was the only way for a port to raise gaierror, so every getaddrinfo() failure surfaced as EAI_NONAME regardless of what actually went wrong. A lookup that failed because the resolver was unreachable looked identical to one for a name that does not exist.

How

Add common_hal_socketpool_socketpool_raise_gaierror(int err), which puts the real code in args[0] where callers can act on it, and express the existing _noname() helper in terms of it.

MP_WEAK MP_NORETURN
void common_hal_socketpool_socketpool_raise_gaierror_noname(void) {
    common_hal_socketpool_socketpool_raise_gaierror(SOCKETPOOL_EAI_NONAME);
}

Existing callers are unchanged and still raise EAI_NONAME with the same message.

No new translatable strings. The message stays "Name or service not known", with the numeric code appended only when it is not NONAME.

zephyr-cp is the first user. Its getaddrinfo path already had the resolver's error in hand and discarded it.

Testing

Built adafruit_metro_esp32s3 on the espressif port, which exercises the shared-bindings change against an existing consumer. Clean build.

No runtime behavior change for existing callers, so there is nothing to observe on hardware for them: _noname() raises the same exception type, code and message as before. The zephyr-cp boards are covered by the port matrix in CI.

Scope

Adds one function and refactors one. Does not change any existing error path.

AI assistance

Written with Claude Code.

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks - makes sense. I suggest a broader error message for non-NONAME.

Comment thread shared-bindings/socketpool/SocketPool.c Outdated
if (err == SOCKETPOOL_EAI_NONAME) {
mp_printf(&print, "%S", MP_ERROR_TEXT("Name or service not known"));
} else {
mp_printf(&print, "%S (%d)", MP_ERROR_TEXT("Name or service not known"), err);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think "Name or service not known" can be misleading for error codes other than EAI_NONAME. There are other possibilities, like bad family, etc.

You could use the generic "%q failure: %d", which already exists, and pass MP_QSTR_getaddrinfo for the %q.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done, with one adjustment: mp_printf takes const char *, so MP_ERROR_TEXT cannot be the format. Used mp_cprintf, which takes mp_rom_error_text_t.

Tested on a SiWx917 DK2605A. A bogus hostname there returns -4 rather than EAI_NONAME, so it lands on the new path:

>>> import wifi, socketpool
>>> pool = socketpool.SocketPool(wifi.radio)
>>> pool.getaddrinfo("no-such-host-xyz.invalid", 80)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gaierror: (-4, 'getaddrinfo failure: -4')
>>>

Before this change the same lookup reported gaierror(-2, 'Name or service not known'), which was both the wrong code and the wrong reason.

@mikeysklar
mikeysklar force-pushed the fix/socketpool-gaierror-real-code branch from 420c217 to a0dbfc6 Compare August 19, 2026 11:10
common_hal_socketpool_socketpool_raise_gaierror_noname() was the only way for a
port to raise gaierror, so every getaddrinfo failure surfaced as EAI_NONAME no
matter what actually went wrong. A name that failed because the resolver was
unreachable looked identical to one that does not exist.

Add common_hal_socketpool_socketpool_raise_gaierror(int err), which carries the
real code into args[0] where callers can act on it, and express the existing
_noname() helper in terms of it. Existing callers are unchanged.

No new translatable strings: the message is the same "Name or service not
known", with the code appended when it is not NONAME.

zephyr-cp is the first user. Its getaddrinfo path had the resolver's error in
hand and discarded it.
@mikeysklar
mikeysklar force-pushed the fix/socketpool-gaierror-real-code branch from a0dbfc6 to 2336983 Compare August 19, 2026 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants