uvicorn
uvicorn copied to clipboard
get_remote_addr return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Checklist
https://github.com/encode/uvicorn/issues/600 same issue like this old one
Describe the bug
on windows,venv
Package Version
------------------ ---------
asgiref 3.4.1
certifi 2021.5.30
charset-normalizer 2.0.4
click 8.0.1
colorama 0.4.4
fastapi 0.68.0
greenlet 1.1.0
h11 0.12.0
idna 3.2
Naked 0.1.31
pip 21.2.3
pycryptodome 3.10.1
pydantic 1.8.2
PyYAML 5.4.1
requests 2.26.0
setuptools 57.4.0
shellescape 3.8.1
SQLAlchemy 1.4.22
starlette 0.14.2
typing-extensions 3.10.0.0
urllib3 1.26.6
uvicorn 0.14.0
wheel 0.36.2
To reproduce
- 1step test.py:
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
async def root():
return {'message': 'hello world'}
- 2step use uvicorn:
uvicorn test:app --reload --port 61111
- 3step open browser and open 127.0.0.1:61111,got no response,and check console got
(total) E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend>uvicorn test:app --reload --port 61111
INFO: Uvicorn running on http://127.0.0.1:61111 (Press CTRL+C to quit)
INFO: Started reloader process [12332] using statreload
INFO: Started server process [10352]
INFO: Waiting for application startup.
INFO: Application startup complete.
Fatal error in server handler
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x00000188B5E8DF10>
transport: <_SelectorSocketTransport fd=904 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\server.py", line 92, in handler
await handle_http(
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\_handlers\http.py", line 75, in handle_http
protocol.connection_made(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 85, in connection_made
self.client = get_remote_addr(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\utils.py", line 13, in get_remote_addr
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Fatal error in server handler
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x00000188B5EA2310>
transport: <_SelectorSocketTransport fd=468 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\server.py", line 92, in handler
await handle_http(
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\_handlers\http.py", line 75, in handle_http
protocol.connection_made(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 85, in connection_made
self.client = get_remote_addr(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\utils.py", line 13, in get_remote_addr
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Fatal error in server handler
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x00000188B5EA26D0>
transport: <_SelectorSocketTransport fd=908 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\server.py", line 92, in handler
await handle_http(
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\_handlers\http.py", line 75, in handle_http
protocol.connection_made(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 85, in connection_made
self.client = get_remote_addr(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\utils.py", line 13, in get_remote_addr
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
INFO: - "GET / HTTP/1.1" 200 OK
Fatal error in server handler
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x00000188B5E8DF70>
transport: <_SelectorSocketTransport fd=916 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\server.py", line 92, in handler
await handle_http(
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\_handlers\http.py", line 75, in handle_http
protocol.connection_made(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 85, in connection_made
self.client = get_remote_addr(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\utils.py", line 13, in get_remote_addr
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Fatal error in server handler
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x00000188B5EA29D0>
transport: <_SelectorSocketTransport fd=920 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\server.py", line 92, in handler
await handle_http(
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\_handlers\http.py", line 75, in handle_http
protocol.connection_made(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 85, in connection_made
self.client = get_remote_addr(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\utils.py", line 13, in get_remote_addr
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Fatal error in server handler
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x00000188B5EA2B20>
transport: <_SelectorSocketTransport fd=916 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\server.py", line 92, in handler
await handle_http(
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\_handlers\http.py", line 75, in handle_http
protocol.connection_made(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 85, in connection_made
self.client = get_remote_addr(transport)
File "E:\Desktop_Lite_AfterWin7\tiandao\qinmo_enc\backend\total\lib\site-packages\uvicorn\protocols\utils.py", line 13, in get_remote_addr
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
ValueError: invalid literal for int() with base 10: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
INFO: - "GET / HTTP/1.1" 200 OK
NOTE:
- Please list tracebacks in full (don't truncate them).
- If relevant, consider turning on DEBUG or TRACE logs for additional details (see the Logging section on https://www.uvicorn.org/settings/ specifically the
log-level
flag). - Consider using
<details>
to make tracebacks/logs collapsible if they're very large (see https://gist.github.com/ericclemmons/b146fe5da72ca1f706b2ef72a20ac39d). -->
Environment
Running uvicorn 0.14.0 with CPython 3.8.3 on Windows 10 x64
Additional context
try to modify the code in site-packages\uvicorn\protocols\utils.py
def get_remote_addr(transport: asyncio.Transport) -> Optional[Tuple[str, int]]:
socket_info = transport.get_extra_info("socket")
if socket_info is not None:
try:
info = socket_info.getpeername()
print("info:")
print(info)
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
except OSError:
# This case appears to inconsistently occur with uvloop
# bound to a unix domain socket.
return None
got info print as below
info:
(0, b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
wont have a windows machine at hand to reproduce that, would you throw up a PR that would make our CI fail ?
wont have a windows machine at hand to reproduce that, would you throw up a PR that would make our CI fail ?
my way to make it back to work quickly is dirty,I just let this socket_info be None,
def get_remote_addr(transport: asyncio.Transport) -> Optional[Tuple[str, int]]:
socket_info = transport.get_extra_info("socket")
if socket_info is not None:
try:
info = socket_info.getpeername()
print("info:")
print(info)
#return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
return None
except OSError:
# This case appears to inconsistently occur with uvloop
# bound to a unix domain socket.
return None
unsure what effects it will produce.
I tried 0.14 and 0.15, but none of them reproduced your problem.
I had the same problem with version 0.15.0 as with the 600 and 1145. At the same time, I tried to shrink the version to 0.14.0 and 0.13.4, but the problem remained.
Describe the bug on windows 10 , venv
Package Version
----------------- --------
asgiref 3.4.1
click 7.1.2
colorama 0.4.4
fastapi 0.68.0
h11 0.12.0
httptools 0.2.0
pip 21.2.4
pydantic 1.8.2
python-dotenv 0.19.0
PyYAML 5.4.1
setuptools 56.0.0
starlette 0.14.2
typing-extensions 3.10.0.0
uvicorn 0.13.4
watchgod 0.7
websockets 9.1
My main.py module code is as follows:
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/message")
async def root():
return {"message": "Hello World"}
if __name__ == '__main__':
uvicorn.run(app='main:app', host="127.0.0.1", port=5000, reload=True, debug=True)
I tried modifying the uvicorn\protocols\utils.py code to print some information, as follow
def get_remote_addr(transport):
socket_info = transport.get_extra_info("socket")
print(socket_info)
if socket_info is not None:
try:
info = socket_info.getpeername()
return (str(info[0]), int(info[1])) if isinstance(info, tuple) else None
except OSError:
# This case appears to inconsistently occur with uvloop
# bound to a unix domain socket.
return None
When a request is sent, the following information is printed:
<asyncio.TransportSocket fd=588, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 5000), raddr=(0, b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')>
it's quite weired problem,when I first use the 0.14 and code I provide above it was normal,but another day rerun its broken.it's totally the same code.and I can only fix it by the way I provided above
https://github.com/LmeSzinc/AzurLaneAutoScript/issues/876#issuecomment-1013101714
Summary
- Problems occurs only when reload is enabled (
--reload
), not sure about--workers
because we don't use it
Solution
- Reset Winsock
netsh winsock reset
(From https://github.com/nonebot/nonebot2/issues/20#issuecomment-703582940)
Is this still an issue?
This title scared me lol
Since I can't reproduce the issue, and I didn't receive any reply about it, I'll assume that folks can solve the issue with the comment above: https://github.com/encode/uvicorn/issues/1145#issuecomment-1013610173.
I'll be closing this, but if you have a solution that needs a PR, feel free to create it, and explain how to reproduce, and how the fix works. :pray:
LmeSzinc/AzurLaneAutoScript#876 (comment)
Summary
- Problems occurs only when reload is enabled (
--reload
), not sure about--workers
because we don't use itSolution
- Reset Winsock
netsh winsock reset
(From 使用nb-cli创建项目后运行出现错误 nonebot/nonebot2#20 (comment))
Thank you!
I also encountered this issue in uvicorn version 0.26.0, and this command netsh winsock reset
saved me!
my environment: windows10 python3.8