Skip to content
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: Mounted asgi app unable to connect via websocket #3980

Closed
2 of 4 tasks
GH-maggio opened this issue Feb 1, 2025 · 7 comments
Closed
2 of 4 tasks

Bug: Mounted asgi app unable to connect via websocket #3980

GH-maggio opened this issue Feb 1, 2025 · 7 comments

Comments

@GH-maggio
Copy link

GH-maggio commented Feb 1, 2025

Description

While mounting a asgi app (marimo), websocket connections cannot be accepted.

environment: Win10, WSL2 - Rockylinux 9.5

URL to code causing the issue

https://github.com/GH-maggio/hello_marimo

MCVE

from litestar import Litestar, asgi
import marimo

server = marimo.create_asgi_app().with_app(
    path="/notebook1", root="./notebooks/notebook1.py"
)


get_notebook = asgi(path="/notebooks", is_mount=True)(server.build())


app = Litestar(
    route_handlers=[
        get_notebook,
    ]
)

Steps to reproduce

1. uv run litestar run
2. navigate via web browser to http://127.0.0.1:8000/notebooks/notebook1
3. See error in terminal

Screenshots

NA

Logs

 ┌──────────────────────────────┬──────────────────────┐
 │ Litestar version             │ 2.14.0               │
 │ Debug mode                   │ Disabled             │
 │ Python Debugger on exception │ Disabled             │
 │ CORS                         │ Disabled             │
 │ CSRF                         │ Disabled             │
 │ OpenAPI                      │ Enabled path=/schema │
 │ Compression                  │ Disabled             │
 └──────────────────────────────┴──────────────────────┘
 INFO:     Started server process [1394]
 INFO:     Waiting for application startup.
 INFO:     Application startup complete.
 INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
 INFO:     127.0.0.1:56740 - "GET / HTTP/1.1" 200 OK
 INFO:     127.0.0.1:56740 - "GET /favicon.ico HTTP/1.1" 404 Not Found
 INFO:     127.0.0.1:56750 - "GET /notebook1/ HTTP/1.1" 200 OK
 INFO:     ('127.0.0.1', 35512) - "WebSocket /notebook1/ws/?session_id=s_u9diol" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed
 INFO:     127.0.0.1:56750 - "GET /notebook1/public-files-sw.js/ HTTP/1.1" 404 Not Found
 INFO:     ('127.0.0.1', 35518) - "WebSocket /notebook1/ws/?session_id=s_u9diol" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed
 INFO:     ('127.0.0.1', 35520) - "WebSocket /notebook1/ws/?session_id=s_u9diol" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed
 INFO:     ('127.0.0.1', 35530) - "WebSocket /notebook1/ws/?session_id=s_u9diol" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed
 INFO:     ('127.0.0.1', 35538) - "WebSocket /notebook1/ws/?session_id=s_u9diol" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed
 INFO:     127.0.0.1:56750 - "GET /notebook2/ HTTP/1.1" 200 OK
 INFO:     ('127.0.0.1', 35544) - "WebSocket /notebook2/ws/?session_id=s_ujsdgw" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed
 INFO:     127.0.0.1:56750 - "GET /notebook2/public-files-sw.js/ HTTP/1.1" 404 Not Found
 INFO:     ('127.0.0.1', 44400) - "WebSocket /notebook2/ws/?session_id=s_ujsdgw" 403
 INFO:     connection rejected (403 Forbidden)
 INFO:     connection closed

Litestar Version

pyproject.toml

[project]
name = "hello-marimo"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"fastapi[standard]>=0.115.7",
"litestar[standard]>=2.14.0",
"marimo>=0.10.19",
]

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
@GH-maggio GH-maggio added the Bug 🐛 This is something that is not working as expected label Feb 1, 2025
@euri10
Copy link
Contributor

euri10 commented Feb 1, 2025

I cant reproduce there's a bunch of statics that arent shipped, can you give a simpler MCVE ? :

INFO:     Started server process [696455]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:57296 - "GET /notebook1/ HTTP/1.1" 200 OK
INFO:     127.0.0.1:57296 - "GET /assets/gradient-yHQUC_QB.png/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57308 - "GET /assets/Lora-VariableFont_wght-B2ootaw-.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57312 - "GET /assets/PTSans-Regular-CxL0S8W7.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57322 - "GET /assets/PTSans-Bold-D9fedIX3.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57328 - "GET /assets/FiraMono-Regular-BTCkDNvf.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57336 - "GET /assets/FiraMono-Medium-DU3aDxX5.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57296 - "GET /assets/index-DfLILvXO.css/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57308 - "GET /assets/noise-60BoTA8O.png/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57312 - "GET /assets/index-C_IDebto.js/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57336 - "GET /assets/FiraMono-Bold-CLVRCuM9.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57312 - "GET /public-files-sw.js/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:57348 - "GET /manifest.json/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55268 - "GET /notebook1/ HTTP/1.1" 200 OK
INFO:     127.0.0.1:55268 - "GET /assets/gradient-yHQUC_QB.png/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55272 - "GET /assets/Lora-VariableFont_wght-B2ootaw-.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55278 - "GET /assets/PTSans-Regular-CxL0S8W7.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55288 - "GET /assets/PTSans-Bold-D9fedIX3.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55304 - "GET /assets/FiraMono-Regular-BTCkDNvf.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55306 - "GET /assets/index-DfLILvXO.css/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55268 - "GET /assets/FiraMono-Medium-DU3aDxX5.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55268 - "GET /assets/FiraMono-Bold-CLVRCuM9.ttf/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55306 - "GET /assets/index-C_IDebto.js/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55278 - "GET /assets/noise-60BoTA8O.png/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55272 - "GET /public-files-sw.js/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:55314 - "GET /manifest.json/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:59600 - "GET /notebook1/ HTTP/1.1" 200 OK
INFO:     127.0.0.1:59600 - "GET /assets/gradient-yHQUC_QB.png/ HTTP/1.1" 404 Not Found
INFO:     127.0.0.1:59602 - "GET /assets/Lora-VariableFont_wght-B2ootaw-.ttf/ HTTP/1.1" 404 Not Found

@provinzkraut provinzkraut added the Needs MCVE This needs an MCVE label Feb 1, 2025
@GH-maggio
Copy link
Author

@euri10 slimmed down the MVCE, if possible can you see the FastAPI version that is in the repo:

uv run fastapi dev
      INFO   Will watch for changes in these directories: ['/home/cmaggio/repos/hello_marimo']
      INFO   Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
      INFO   Started reloader process [1535] using WatchFiles
      INFO   Started server process [1537]
      INFO   Waiting for application startup.
      INFO   Application startup complete.
      INFO   127.0.0.1:42232 - "GET /notebooks/notebook1/ HTTP/1.1" 200
      INFO   ('127.0.0.1', 42240) - "WebSocket /notebooks/notebook1/ws?session_id=s_oddu05"
      INFO   connection open
      INFO   127.0.0.1:42232 - "POST /notebooks/notebook1/api/kernel/instantiate HTTP/1.1" 200
      INFO   127.0.0.1:42232 - "GET /notebooks/notebook1/public-files-sw.js HTTP/1.1" 200

@GH-maggio
Copy link
Author

I'm not sure if the root path is being passed to the ASGI marimo app /notebooks/notebook1

@euri10
Copy link
Contributor

euri10 commented Feb 4, 2025

add a / to your ws route seems to solve it
seems like an issue with starlette and forward slash ?

@router.websocket("/ws/")
async def websocket_endpoint(
    websocket: WebSocket,
) -> None:
    """
    responses:
        200:
            description: Websocket endpoint
    """

@euri10
Copy link
Contributor

euri10 commented Feb 4, 2025

usually with ws when you get a 403 it's a pretty good indication the route is not found, hence how I found the reason.

more on starlette trailing slashes: encode/starlette#869

@euri10 euri10 closed this as completed Feb 4, 2025
@euri10 euri10 removed Bug 🐛 This is something that is not working as expected Needs MCVE This needs an MCVE labels Feb 4, 2025
@GH-maggio
Copy link
Author

@euri10 thanks for investigating, wasn't aware litestar used starlette as the backend.

@euri10
Copy link
Contributor

euri10 commented Feb 5, 2025

@euri10 thanks for investigating, wasn't aware litestar used starlette as the backend.

mmmm litestar was using starlette a while ago, though now it's completely seperated

you wouldn't have that bug using litestar as a base, just saying

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

No branches or pull requests

3 participants