-
Notifications
You must be signed in to change notification settings - Fork 954
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
WebSocket Connection Error with Gunicorn and Multiple Workers #1369
Comments
Hello, @huoxiao-C! I'm here to help you with any bugs, questions, or contributions you have regarding Chainlit. Let's tackle this issue together! I found a similar closed issue that might be relevant to your problem: Error Message "Could not reach the server" on mounting to FastAPI. The issue was resolved by pull request #1337, which addressed WebSocket connection issues when submounting Chainlit. The specific changes introduced in pull request #1337 include modifications to the def mount_chainlit(app: FastAPI, target: str, path="/chainlit"):
os.environ["CHAINLIT_ROOT_PATH"] = path
os.environ["CHAINLIT_SUBMOUNT"] = "true"
from chainlit.config import config, load_module
from chainlit.server import app as chainlit_app
config.run.root_path = path
config.run.debug = os.environ.get("CHAINLIT_DEBUG", False)
check_file(target)
# Load the module provided by the user
config.run.module_name = target
load_module(config.run.module_name)
ensure_jwt_secret()
app.mount(path, chainlit_app) These changes ensure that the WebSocket connections work correctly when Chainlit is submounted [1][2]. Additionally, there are specific configurations required in Gunicorn to support WebSocket connections for Chainlit. The relevant settings are managed through Uvicorn, which is used to run the Chainlit server. Here are the key configurations:
These settings are then passed to the Uvicorn configuration when starting the server: config = uvicorn.Config(
app,
host=host,
port=port,
ws=ws_protocol,
log_level=log_level,
ws_per_message_deflate=ws_per_message_deflate,
ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile,
) To summarize, you need to ensure that the If you continue to experience issues, please verify that you are using the latest version of Chainlit and that the environment variables are correctly set. |
@dosu Could you suggest a more concise title for the issue? @huoxiao-C Do let us know whether the bot helped you out! |
hello dokterbob,the bot method is not ok,we still cannot specify multiworker with gunicorn |
@huoxiao-C Sorry it took a while. Could you perhaps increase logging to provide a full traceback for the 403? Also, make sure you're on the latest version (we improved some things around error handling). We need to know the exact code generating this error. Should work roughly like this: https://search.brave.com/search?q=fastapi+logging+of+tracebacks&source=web&summary=1&conversation=7c7357e15d8a0fc3f88da7&summary_og=bb464324cb55a1e106371f Could you also double check (if you haven't already) that |
@dokterbob hello dokterbob, thanks for your reply This is my testing codemain.py
cl_test.py
app run command software information |
After mounting Chainlit to the FastAPI application, if the Gunicorn command is used to start the application and multiple processes are specified (using the -w option), an error will occur. The startup command is: gunicorn -w 2 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:3010
bug description
If the method based on FastAPI provided on the Chainlit official website (https://docs.chainlit.io/deploy/api) is used to mount Chainlit, and then the application is started using the Gunicorn command with multiple processes specified, such as:
gunicorn -w 2 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:3010
, an error will be encountered on the web UI stating "Unable to connect to server".Additionally, the console will display the following errors:
[2024-09-24 11:03:23 +0800] [10116] [INFO] connection closed
[2024-09-24 11:03:24 +0800] [10116] [INFO] ('127.0.0.1', 63124) - "WebSocket /chat/ws/socket.io/?EI0=4&transport=websocket&sid=TVH1xbI1HXjNDocwAAAK" 403
[2024-09-24 11:03:24 +0800] [10116] [INFO] connection rejected (403 Forbidden)
[2024-09-24 11:03:24 +0800] [10116] [INFO] connection closed
[2024-09-24 11:03:25 +0800] [10116] [INFO] ('127.0.0.1', 63129) - "WebSocket /chat/ws/socket.io/?EI0=4&transport=websocket&sid=i_R_YcNkA9SIqMBuAAAc" 403
[2024-09-24 11:03:25 +0800] [10116] [INFO] connection rejected (403 Forbidden)
[2024-09-24 11:03:25 +0800] [10116] [INFO] connection closed
Chainlit mounting code:
software information
The text was updated successfully, but these errors were encountered: