Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run on macos report socket.gaierror: [Errno 8] nodename nor servname provided, or not known #1178

Closed
tianqiqiu opened this issue Jul 17, 2023 · 14 comments · Fixed by #1498
Closed

Comments

@tianqiqiu
Copy link

Description

when i run examples on macos, it fails, report socket.gaierror: [Errno 8] nodename nor servname provided, or not known. i use version is 1.3.3

@rodja
Copy link
Member

rodja commented Jul 17, 2023

Strange. We mostly develop on MacOS and have not encountered this error. Could you try another port like suggested in #1154?

@tianqiqiu
Copy link
Author

tianqiqiu commented Jul 17, 2023

i try change port ui.run(port=5500) or ui.run(port=9900) or ui.run(port=8989) . It still report socket.gaierror: [Errno 8] nodename nor servname provided, or not known. the error is

in File "/Users/tim/Documents/prjall/venv/nicepd/lib/python3.11/site-packages/nicegui/welcome.py", line 16, in get_all_ips
    return [info[4][0] for info in socket.getaddrinfo(socket.gethostname(), None) if len(info[4]) == 2]

i test socket.gethostname() the result is 'ddim.local', ddim is my username. socket.getaddrinfo(socket.gethostname(), None) is report error. my python verison is 3.11.4.

@tianqiqiu
Copy link
Author

my macos version is 13.4.1(c)

@rodja
Copy link
Member

rodja commented Jul 17, 2023

Ah your error output probably helped. You can simply fix the issue with python -m pip install netifaces. NiceGUI automatically uses it when available because it is more reliable (#1137). Unfortunately netifaces requires compilation on all platforms and therefore is optional (#1166).
But in the default installation it should definitely not crash. Could you provide the output of running

import socket
socket.getaddrinfo(socket.gethostname(), None)

in an interactive Python shell?

@tianqiqiu
Copy link
Author

i installed netifaces use python -m pip install netifaces.
run

import socket
socket.getaddrinfo(socket.gethostname(), Non
```e)
 the error still is  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

@tianqiqiu
Copy link
Author

tianqiqiu commented Jul 17, 2023

if add '127.0.0.1 [hostname]' in /etc/hosts, hostname is the result of socket.gethostname()

import socket
socket.getaddrinfo(socket.gethostname(), None)

it run correctly

@Devil-Ryu
Copy link

I had the same proble as you
you can specify host in program like this

ui.run(host='127.0.0.1')

@tianqiqiu
Copy link
Author

I had the same proble as you you can specify host in program like this

ui.run(host='127.0.0.1')

Thank you.

@pythoninthegrass
Copy link

Thanks for the workaround @Devil-Ryu!

Hardcoding the host IP as loopback works but is inconvenient.

Guessing the error handling is off somehow. If it's OS-specific, I've used platform in the past to some success. Patching run.py to catch the OS appears to work with minimal refactoring:

import platform
if platform.system() == 'Darwin':
    host = host or '127.0.0.1'
elif platform.system() == 'Linux':
    host = host or '127.0.0.1'
elif platform.system() == 'Windows':
    host = host or '127.0.0.1'
else:
    host = host or '0.0.0.0'

if fullscreen:
    native = True
if frameless:
    native = True
if window_size:
    native = True
if native:
    show = False
    # host = host or '127.0.0.1'
    port = native_mode.find_open_port()
    width, height = window_size or (800, 600)
    native_mode.activate(host, port, title, width, height, fullscreen, frameless)
# else:
#     host = host or '0.0.0.0'

Could have implications for running in a docker container (0.0.0.0), but otherwise negates having to hardcode the IP address as a keyword arg on macOS.

System Info

macOS 12.6.7 (21G651)
python 3.11.4

@rodja
Copy link
Member

rodja commented Aug 14, 2023

Using only 127.0.0.1 as default is not optimal because it will only allow local access and not open on all network interfaces. I would like to see a better fix but I'm not sure I've understood the root-cause of the socket.gaierror yet ...

@natankeddem
Copy link
Contributor

It seems like this known macos issue:
https://stackoverflow.com/questions/39970606/gaierror-errno-8-nodename-nor-servname-provided-or-not-known-with-macos-sie
https://apple.stackexchange.com/questions/253817/cannot-ping-my-local-machine

My understanding is that if you don't enable any sort of filesharing /etc/hosts is missing an entry or something else isn't being setup in the OS to allow this to work correctly.

@pythoninthegrass
Copy link

Using only 127.0.0.1 as default is not optimal because it will only allow local access and not open on all network interfaces.

That's what it already is. Only difference is now there's a platform check to make sure it's enforced. The if native conditional never hits otherwise which leaves a blank host variable and/or tries to set 0.0.0.0 which doesn't cooperate with the browser on macOS in my experience. 0.0.0.0 is crucial in Docker for the reason you listed though.

It seems like this known macos issue

I can ping my localhost just fine. Enabling file sharing to get a website working is not a tenable solution.

Looks like editing /etc/hosts to explicitly use the hostname vs. localhost is a workaround. Just not ideal from a DX perspective.

127.0.0.1       localhost
127.0.0.1       mbp
255.255.255.255 broadcasthost
::1             localhost

@pythoninthegrass
Copy link

import socket
socket.getaddrinfo(socket.gethostname(), None)

in an interactive Python shell?

FWIW hardcoding the hostname in /etc/hosts gets different results here:

# 127.0.0.1       mbp
[(<AddressFamily.AF_INET: 2>,
  <SocketKind.SOCK_DGRAM: 2>,
  17,
  '',
  ('127.0.0.1', 0)),
 (<AddressFamily.AF_INET: 2>,
  <SocketKind.SOCK_STREAM: 1>,
  6,
  '',
  ('127.0.0.1', 0))]

# 127.0.0.1       localhost
gaierror                                  Traceback (most recent call last)
Cell In [2], line 1
----> 1 socket.getaddrinfo(socket.gethostname(), None)

File ~/.asdf/installs/python/3.11.4/lib/python3.11/socket.py:962, in getaddrinfo(host, port, family, type, proto, flags)
    959 # We override this function since we want to translate the numeric family
    960 # and socket type values to enum constants.
    961 addrlist = []
--> 962 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    963     af, socktype, proto, canonname, sa = res
    964     addrlist.append((_intenum_converter(af, AddressFamily),
    965                      _intenum_converter(socktype, SocketKind),
    966                      proto, canonname, sa))

gaierror: [Errno 8] nodename nor servname provided, or not known

@rodja
Copy link
Member

rodja commented Aug 27, 2023

Ah by deactivating all sharing features in the macOS settings, restarting the laptop and deinstalling netifaces I can reproduce the original socket.getaddrinfo issue.

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 a pull request may close this issue.

5 participants