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

ValueError: 'localhost' does not appear to be an IPv4 or IPv6 address #836

Closed
1 of 2 tasks
florimondmanca opened this issue Oct 24, 2020 · 1 comment
Closed
1 of 2 tasks

Comments

@florimondmanca
Copy link
Member

florimondmanca commented Oct 24, 2020

Hey folks!

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

When specifying --host localhost (or passing host="localhost" to uvicorn.run()), I'm getting a ValueError that prevents the server from starting.

This seems to have been introduced by #803, since the error happens on this line:

https://github.com/encode/uvicorn/pull/803/files#diff-ab3f29e794a752a9514c33ce772320a2cfbeab571f5254207f096e2d8da7e077R506

To reproduce

# server.py
import uvicorn
from starlette.applications import Starlette

app = Starlette()

if __name__ == "__main__":
    uvicorn.run(app, host="localhost")

Expected behavior

Server should start and listen on localhost:8000.

Actual behavior

Server fails to start and raises exception.

Debugging material

$ python debug/server.py                   
INFO:     Started server process [86782]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
Traceback (most recent call last):
  File "/Users/florimond/Developer/python-projects/httpx/debug/server.py", line 7, in <module>
    uvicorn.run(
  File "/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.9/site-packages/uvicorn/main.py", line 391, in run
    server.run()
  File "/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.9/site-packages/uvicorn/main.py", line 419, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "/Users/florimond/.pyenv/versions/3.9.0/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.9/site-packages/uvicorn/main.py", line 436, in serve
    await self.startup(sockets=sockets)
  File "/Users/florimond/Developer/python-projects/httpx/venv/lib/python3.9/site-packages/uvicorn/main.py", line 526, in startup
    if isinstance(ip_address(config.host), IPv6Address):
  File "/Users/florimond/.pyenv/versions/3.9.0/lib/python3.9/ipaddress.py", line 53, in ip_address
    raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
ValueError: 'localhost' does not appear to be an IPv4 or IPv6 address

Environment

  • OS / Python / Uvicorn version:
Running uvicorn 0.12.2 with CPython 3.9.0 on Darwin
  • The exact command you're running uvicorn with, all flags you passed included. If you run it with gunicorn please do the same. If there is a reverse-proxy involved and you cannot reproduce without it please give the minimal config of it to reproduce.
python server.py

Additional context

Found this while investigating a deadlock in the HTTPX test suite: encode/httpx#1366

Our HTTPS server fixture passes host="localhost" (not required, removing that flag makes the test suite pass again) which is causing the Uvicorn server to fail to start, and we're not checking for that potentiality so the fixture keeps checking for server.ready indefinitely, hence the deadlock.

@florimondmanca
Copy link
Member Author

@euri10 Oops, just found out that there's a fix in master for this already — #827.

I'll close as a duplicate, but I'll take a look at the fix in #827 (you mentioned you were "not super happy" with it, and it does seem a bit complicated — maybe there's a way to simplify things).

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

No branches or pull requests

1 participant