Skip to content

Allow to raise HTTPException before websocket.accept()#2725

Merged
Kludex merged 4 commits intomasterfrom
allow-http-exception-in-websocket-endpoint
Oct 15, 2024
Merged

Allow to raise HTTPException before websocket.accept()#2725
Kludex merged 4 commits intomasterfrom
allow-http-exception-in-websocket-endpoint

Conversation

@Kludex
Copy link
Copy Markdown
Owner

@Kludex Kludex commented Oct 13, 2024

Before the websocket.accept() is called, we can actually return a different response... Right now it can be done with websocket.send_denial_response(), which I'm not sure it was the best choice of API, maybe we shouldn't have added that method, but just used the HTTPException.

Reviews are welcome. I want to make a release on October 15.

@Kludex Kludex requested a review from a team October 13, 2024 16:32
Copy link
Copy Markdown
Contributor

@adriangb adriangb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! What happens if you raise the exception after accepting? I assume it's unchanged and basically undefined behavior, but it would be nice to document and maybe give users an informative error in the future?

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Oct 13, 2024

This looks great! What happens if you raise the exception after accepting? I assume it's unchanged and basically undefined behavior, but it would be nice to document and maybe give users an informative error in the future?

Good question. And actually... This PR solves a problem that is happening on master:

from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.routing import WebSocketRoute
from starlette.websockets import WebSocket


async def ws_endpoint(websocket: WebSocket):
    await websocket.accept()
    raise HTTPException(status_code=400, detail="Bad request")


app = Starlette(routes=[WebSocketRoute("/ws", ws_endpoint)])

The connection stays open...
Screenshot 2024-10-13 at 19 06 19

With this PR, we can see a server exception, given that the HTTPException will try to send a 'websocket.http.response.start' on the ExceptionMiddleware:

Screenshot 2024-10-13 at 19 08 06

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Oct 13, 2024

maybe give users an informative error in the future?

We can do that, yep.

@adriangb
Copy link
Copy Markdown
Contributor

That's great! We can merge this as is then or if you want to add a slightly better error message for that case feel free to do so.

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Oct 14, 2024

That's great! We can merge this as is then or if you want to add a slightly better error message for that case feel free to do so.

I think we have an issue on the wrap_app_handling_exceptions where if I'm creating a Starlette application the function will be called twice (the first middleware, and the router), and then the response_start will always be False. And I need a similar logic to check if websocket_accepted... In any case, I'll check later.

@Kludex Kludex merged commit 99b6938 into master Oct 15, 2024
@Kludex Kludex deleted the allow-http-exception-in-websocket-endpoint branch October 15, 2024 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants