question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proxy errors due to improper inference of `websockets` version (based on Python version)

See original GitHub issue

Checklist

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

Describe the bug

With Python >= 3.7 and websockets<10, websockets is passed a logger parameter that the legacy server does not support.

The _LoggerMixin class is required to prevent the logger from being passed into the legacy server that websockets uses in earlier (<10) versions. Not having the mixin yields the following error:

Fatal error: protocol.data_received() call failed.
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x104daa910>
transport: <_SelectorSocketTransport fd=22 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/Users/shy/.pyenv/versions/3.9.6/lib/python3.9/asyncio/selector_events.py", line 870, in _read_ready__data_received
    self._protocol.data_received(data)
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 131, in data_received
    self.handle_events()
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 180, in handle_events
    self.handle_upgrade(event)
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 268, in handle_upgrade
    protocol = self.ws_protocol_class(
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 77, in __init__
    super().__init__(
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/websockets/legacy/server.py", line 201, in __init__
    super().__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'logger'

which results in errors proxying sockets on the frontend:

Proxy error: Could not proxy request /socket.io/?EIO=4&transport=polling&t=NvBQjFH from localhost:3000 to http://127.0.0.1:8080/.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNREFUSED).

The class is implemented in /uvicorn/protocols/websockets/websockets_impl.py and infers the version of websockets from the Python version. Websockets conforms to PEP8’s __version__ so that could be used instead.

# special case logger kwarg in websockets >=10
if sys.version_info >= (3, 7):
    class _LoggerMixin:
        pass
else:
    class _LoggerMixin:
        def __init__(self, *args, logger, **kwargs):
            super().__init__(*args, **kwargs)
            self.logger = logging.LoggerAdapter(logger, {"websocket": self})

Steps to reproduce the bug

Dependencies:

uvicorn==16.0.0
websockets==0.9.1
fastapi==0.70.1

This fails:

from fastapi import FastAPI, WebSocket
from fastapi.responses import HTMLResponse

app = FastAPI()

html = """
<script>
    var ws = new WebSocket("ws://localhost:8000/ws");
</script>
"""

@app.get("/")
async def get():
    return HTMLResponse(html)

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(f"Message text was: {data}")

Expected behavior

Establishing a connection successfully.

Actual behavior

Pretty much the same error:

Fatal error: protocol.data_received() call failed.
protocol: <uvicorn.protocols.http.h11_impl.H11Protocol object at 0x1067ffc70>
transport: <_SelectorSocketTransport fd=12 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/Users/shy/.pyenv/versions/3.9.6/lib/python3.9/asyncio/selector_events.py", line 870, in _read_ready__data_received
    self._protocol.data_received(data)
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 131, in data_received
    self.handle_events()
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 180, in handle_events
    self.handle_upgrade(event)
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 268, in handle_upgrade
    protocol = self.ws_protocol_class(
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 77, in __init__
    super().__init__(
  File "/Users/shy/Projects/nftychat/venv/lib/python3.9/site-packages/websockets/legacy/server.py", line 201, in __init__
    super().__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'logger'

Debugging material

No response

Environment

Uvicorn 0.16.0, CPython 3.9.6

uvicorn example:app fails with the prior snippet.

image

Additional context

I’m going to make a PR using the dunder.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
iCalculatedcommented, Jan 12, 2022

Yes, that will resolve the issue.

2reactions
Kludexcommented, Jan 15, 2022

Well… It was already released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

1006 Connection closed abnormally error with python 3.7 ...
it means that the TCP connection was lost. As a consequence, the WebSocket connection was closed without receiving a close frame, which is ......
Read more >
Examples — websocket-client 1.4.2 documentation
You can create your first custom connection with this library using one of the simple examples below. Note that the first WebSocket example...
Read more >
Testing Guide - OWASP Foundation
The Open Web Application Security Project (OWASP) is a worldwide free and open com- munity focused on improving the security of application software....
Read more >
Google Cloud release notes | Documentation
31 and later versions resulted in intermittent issues with database connections. Cloud Composer 1.20.2 and 2.1.2 are versions with an extended upgrade timeline....
Read more >
Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found