Skip to content

Differend websockets states in endpoint and exception handler #5775

@Whatislove118

Description

@Whatislove118

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@router.websocket("/ws/")
async def execute_command(
    socket: CommunicationSocket = Depends(get_communication_socket),
) -> None:
    manager = socket.get_manager()
    await manager.connect(socket.websocket)
    logger.info(f"Client {socket.app_resolver.app.name} connected")
    try:
        await manager.process(socket)
    except WebSocketDisconnect:
        manager.disconnect(socket.websocket)
        logger.info(f"Client {socket.app_resolver.app.name} disconnected")
    except pydantic.ValidationError as e:
        raise CommunicationValidationError(str(e))


## EXCEPTION HANDLER

async def handle_service_exception(request: HTTPConnection, exc: LimIDException) -> JSONResponse:
    exception_data = exc.data
    status_code = exception_data.status_code[request.scope["type"]]

    if status_code is None:
        raise exc

    if isinstance(request, WebSocket):
        # request.application_state = WebSocketState.CONNECTED # this allows it to work only if the socket is open (i.e. one has to try to guess)
        if request.application_state == WebSocketState.CONNECTING:
            await request.accept()
            
        
        await request.send_json({"message": exception_data.message, "status": False, "code": status_code})
        await request.close(code=int(status_code), reason=exception_data.message)

    return JSONResponse({"message": exception_data.message, "status": False}, status_code=status_code)

Description

In endpoint, I open and accept web socket connection. State of web socket set to CONNECTED.
If an exception raised in endpoint, the exception handler should process that. But request web socket doesn't equal to endpoint web socket by state (always CONNECTING in handler).

On connect:
image

After exception raised:
image

And finally, send and receive are common, I can send/receive message from this "different" web sockets

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.88.0

Python Version

3.11

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions