-
-
Notifications
You must be signed in to change notification settings - Fork 387
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
Bug: 405: Method Not Allowed when using Websockets with Litestar and Nginx Unit #3887
Comments
interesting, I dont remember we do something fancy in uvicorn. |
This is actually for a work project so I'm a bit limited in what software I'm allowed to install - IT Security and all that. I've only tested uvicorn and Nginx Unit but I've tested this on multiple machines. I didn't actually have this problem using uvicorn and developing the whole thing it was only when I deployed to our production server that was running a production asgi server that this issue cropped up. Afterwards, I also installed Nginx Unit on my dev machine to confirm the issue and find the cause.
If it isn't uvicorn doing it, my only other thought was maybe there was some upstream code in Litestar that clears the method during the websocket connection in certain scenarios but, assuming the method parameter is literal and unaltered (meaning it is supposed to be exactly as sent GET, POST etc.) then according to the spec, method should be "GET" for websocket connections so I wouldn't think the way it is written now would be a valid way to determine if the connection is a websocket or not, no? 1 if node.is_asgi:
2 return node.asgi_handlers["asgi"]
3 if method:
4 return node.asgi_handlers[method]
5 return node.asgi_handlers["websocket"] I'm trying to understand how line 5 will ever get hit if method is always supposed to be "GET" for the incoming websocket connection? |
ok too bad, that would have been useful to pinpoint if uvicorn is doing something fancy. |
Sorry for the delay was out of the office Friday and Monday. I wasn't entirely sure how to package this for you for a repro because of all the requirements but I tried my best. Check the README.md on this repo: https://github.com/FixFlare/bug_3887 and let me know if you have any questions. There is a sample nginx unit config in there as well for you to use. |
@euri10 This feels the same as #3840 (comment) |
So while this isn't really a bug, as it's caused by nginx unit not implementing the ASGI spec correctly, I'm thinking if we should maybe make some changes that would avoid this. Right not we're inferring if we should use a WS handler based on the absence of a method, but this information is also passed to us trough |
@euri10 Noted - thanks @Alc-Alc I agree, looks to be the same issue and I understand the point being made by @provinzkraut - thank you both With that said, I opened up an issue over on the Nginx Unit github here The good news is I have a workaround for now and I think it's best for everyone if the issue gets fixed at the actual source. |
@FixFlare Just as an aside
uvicorn is not just a dev server btw 🙂 |
@Alc-Alc Fair enough, didn't mean to imply that was its only use. That more came from this page Where on Nginx Unit it mentions:
To me that reads like use uvicorn for dev, use Nginx Unit if you're running in production. |
Description
I believe there may be an issue with how Litestar handles Websocket connections incoming from a client app hosted with Nginx Unit.
This problem does not happen with uvicorn, only Nginx Unit.
From my typescript react app I initiate the websocket connection:
and receive this error from litestar:
I traced the issue back to this function in that same file:
When I watch the "method" parameter on the incoming connection from the websocket and I'm using Uvicorn, method is "None" and everything works as expected.
When using Nginx Unit method is "GET" so it tries to handle it like an http connection rather than a websocket one and you get the above error.
If I then modify
to
I get past the "method not allowed" error but then I get
I then took a look at the function from the first error:
I then modified
to
and now everything works as expected.
The reason I believe this may be a bug is the way if its determining if it's a websocket connection in the "parse_node_handlers" function.
When I check the websocket spec, page 17, point 2 it says
So I think the method coming through as "GET" on the websocket connection from Nginx Unit is normal behavior and the method being "None" from Uvicorn is abnormal.
Unfortunately, it seems like Litestar relies on method being "none" currently to handle the websocket connection which is breaking websockets for servers following that spec.
URL to code causing the issue
No response
MCVE
No response
Steps to reproduce
No response
Screenshots
No response
Logs
Litestar Version
2.13.0
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: