You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LoggerAdapter: using request_headers in DEBUG-level logs raises: AttributeError("'WebSocketServerProtocol' object has no attribute 'request_headers'")
#1428
Open
rayrapetyan opened this issue
Dec 29, 2023
· 5 comments
The following attributes are available after the opening handshake, once the WebSocket connection is open: ...
You're trying to use the attribute before it's available, which is why the AttributeError happens (in your code, not in websockets).
request_headers init happens few lines below
That's incorrect. It's only a type declaration, not an initiatialization. The attribute doesn't exist yet.
I can see how it's an easy mistake to make and a relatively hard one to debug.
Unfortunately I don't see an obvious way to fix it.
Perhaps the best fix is to remove that logging call entirely. I think it's the only logging call happening before the opening handshake (to be verified).
This can happen in other places, notably here if the handshake request or response received from the other side isn't a valid HTTP message.
One possible solution would be to initialize request_headers with an empty Headers() instance (and the same for other similar attributes). Currently, all these attributes are conceptually immutable; I can see a risk of causing more confusion by returning "wrong" values (like path = "/").
The other issue was swallowing of exceptions (maybe similar to #1415 ?). Clients just get connection reset on their side with no output in the server logs. I believe this should be also fixed...
When LoggerAdapter is utilized and DEBUG-level logs are activated, it results in the generation of an AttributeError exception.
Similar to #1398, but on the server side.
Seems this issue is caused by this line:
websockets/src/websockets/legacy/protocol.py
Line 256 in 33b20e1
Possible fix would be putting request_headers initialization above the:
line.
The only aspect that confuses me is how such an error could go unnoticed for a period of 2 years (https://github.com/python-websockets/websockets/blame/33b20e11e86f8490770185c78ed39adab8db4560/src/websockets/legacy/protocol.py#L256). I might be overlooking something, but I can't identify what it might be.
This might be due to the fact that this error does not produce any output in the server logs; the client simply receives a connection reset error.
The text was updated successfully, but these errors were encountered: