socketpool: let ports raise gaierror with the real error code - #11215
socketpool: let ports raise gaierror with the real error code#11215mikeysklar wants to merge 1 commit into
Conversation
dhalbert
left a comment
There was a problem hiding this comment.
Thanks - makes sense. I suggest a broader error message for non-NONAME.
| 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
420c217 to
a0dbfc6
Compare
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.
a0dbfc6 to
2336983
Compare
What
common_hal_socketpool_socketpool_raise_gaierror_noname()was the only way for a port to raisegaierror, so everygetaddrinfo()failure surfaced asEAI_NONAMEregardless 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 inargs[0]where callers can act on it, and express the existing_noname()helper in terms of it.Existing callers are unchanged and still raise
EAI_NONAMEwith the same message.No new translatable strings. The message stays
"Name or service not known", with the numeric code appended only when it is notNONAME.zephyr-cp is the first user. Its
getaddrinfopath already had the resolver's error in hand and discarded it.Testing
Built
adafruit_metro_esp32s3on 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.