You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ray Serve部署FastAPI WebSocket服务时超5连接触发"ASGI callable returned without sending handshake"错误的排查求助

Ray Serve部署FastAPI WebSocket服务时超5连接触发"ASGI callable returned without sending handshake"错误的排查求助

我正在开发一个基于FastAPI的WebSocket服务器,用Ray Serve部署来处理多WebSocket连接,目标是支持最高5万并发用户。但集成Ray Serve做多进程处理后,遇到了一个棘手的问题:

前5个用户连接时一切正常,第6个用户连接时就触发了错误。

错误日志详情

ERROR:    ASGI callable returned without sending handshake.
(ProxyActor pid=1264842) Task exception was never retrieved
(ProxyActor pid=1264842) future: <Task finished name='Task-740' coro=<ProxyResponseGenerator._await_response_anext() done, defined at /home/user/lib/python3.11/site-packages/ray/serve/_private/proxy_response_generator.py:115> exception=RequestCancelledError('e85273c8-f582-49a4-8dbe-0a86b464ad90')>
(ProxyActor pid=1264842) Traceback (most recent call last):
(ProxyActor pid=1264842)   File "/home/user/lib/python3.11/site-packages/ray/serve/_private/proxy_response_generator.py", line 116, in _await_response_anext
(ProxyActor pid=1264842)     return await self._response.__anext__()
(ProxyActor pid=1264842)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(ProxyActor pid=1264842)   File "/home/user/lib/python3.11/site-packages/ray/serve/handle.py", line 559, in __anext__
(ProxyActor pid=1264842)     replica_result = await self._fetch_future_result_async()
(ProxyActor pid=1264842)                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(ProxyActor pid=1264842)   File "/home/user/lib/python3.11/site-packages/ray/serve/handle.py", line 280, in _fetch_future_result_async
(ProxyActor pid=1264842)     raise RequestCancelledError(self.request_id) from None
(ProxyActor pid=1264842) ray.serve.exceptions.RequestCancelledError: Request e85273c8-f582-49a4-8dbe-0a86b464ad90 was cancelled.
(ProxyActor pid=1264842) Task exception was never retrieved
(ProxyActor pid=1264842) future: <Task finished name='Task-738' coro=<WebSocketProtocol.run_asgi() done, defined at /home/user/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py:236> exception=RuntimeError('unable to perform operation on <TCPTransport closed=True reading=False 0x7f0420176800>; the handler is closed')>
(ProxyActor pid=1264842) Traceback (most recent call last):
(ProxyActor pid=1264842)   File "/home/user/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 258, in run_asgi
(ProxyActor pid=1264842)     self.send_500_response()
(ProxyActor pid=1264842)   File "/home/user/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 222, in send_500_response
(ProxyActor pid=1264842)     self.transport.write(b"".join(content))
(ProxyActor pid=1264842)   File "uvloop/handles/stream.pyx", line 678, in uvloop.loop.UVStream.write
(ProxyActor pid=1264842)   File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
(ProxyActor pid=1264842) RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x7f0420176800>; the handler is closed

相关代码

主应用与Ray Serve部署代码

from fastapi import FastAPI, WebSocket
from ray import serve

app = FastAPI()

@serve.deployment
@serve.ingress(app)
class FastAPIDeployment:
    def __init__(self):
        self.connections = {}

    @app.websocket("/ws")
    async def websocket_endpoint(self, websocket: WebSocket):
        await websocket.accept()
        # Connection handling logic

WebSocket管理器

我用了一个基于Redis的管理器来追踪所有WebSocket连接。

环境版本

  • Python 3.11
  • ray[serve]==2.40.0
  • fastapi==0.115.5
  • uvicorn==0.32.1
  • asyncio==3.4.3

已尝试的操作

  1. 验证了Redis连接的可用性,以及活跃WebSocket连接的管理逻辑,暂时没发现问题;
  2. 修改了serve.start()的http_options参数,调整了连接相关配置:
serve.start(http_options={
    "host": "0.0.0.0",
    "port": 8000,
    "keep_alive_timeout": 60,
    "max_keep_alive_requests": 10
})

求助问题

  • Ray Serve或FastAPI是否存在我没注意到的并发WebSocket连接限制?
  • 该如何调试并解决这个"ASGI callable returned without sending handshake"错误?
  • 这个问题会不会和Redis中WebSocket连接状态的管理方式,或者Ray Serve的状态管理逻辑有关?

备注:内容来源于stack exchange,提问作者Musab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:13:05