快速结论:在 gr.Server 应用里把 StaticFiles 挂到根路径 / 时,该挂载会先于 Gradio 内部路由匹配全部请求,导致 gr.Server 的 API 路由(如 /config、/gradio_api/info)返回 404。优先排查挂载路径与 Gradio 保留路由的冲突。
适用环境:Issue 确认环境为 Gradio 7.0-dev,使用 gr.Server + FastAPI StaticFiles;executor 默认 process 可复现,executor="thread" 同样触发启动失败。Issue 未提供操作系统、Python、CUDA、显卡或依赖版本信息。
最快修复方案:Issue 已通过 gradio-app/gradio PR #14085 修复:StaticFiles 挂载在 / 时不再抢占 Gradio 的 API 路径,并且当静态目录中存在与保留路径同名的文件或文件夹时,launch() 会抛出明确错误。升级到包含该修复的 Gradio 版本即可,具体版本号需以官方发布记录为准。
注意事项:修复只保证 Gradio 保留路径不被静态挂载遮蔽;如果静态目录里放了与保留路径同名的文件或目录,仍会报错,需要改名或移出。Issue 未明确说明哪些 7.0-dev 之后的正式版本包含修复,升级前请核对 Release Notes。
问题场景
用户在 Gradio 7.0-dev 中使用 gr.Server 搭建应用,并按照 FastAPI 常见做法把前端静态目录挂载到根路径,用于同时提供页面和 API:
from fastapi.staticfiles import StaticFiles
import gradio as gr
app = gr.Server()
@app.api(name="predict")
def predict(text: str) -> str:
return text.upper()
app.mount("/", StaticFiles(directory="site", html=True))
app.launch()
当 executor="process"(默认)时,页面能加载,但 Gradio 自身路由返回 404;当 executor="thread" 时,应用直接启动失败。
报错原文
gr.Server: StaticFiles mounted at "/" hides Gradio's API routes
Couldn't start the app because '.../gradio_api/startup-events' failed (code 404)
原因分析
根据 Issue 正文,gr.Server 自己注册的路由排在 Gradio 内部路由之前,而挂载在 / 的 Mount 会匹配所有路径。StaticFiles 挂载到根路径后,请求先被该挂载截获,/config、/gradio_api/info 等 Gradio 路由无法到达,因此客户端连不上;executor="thread" 下启动流程需要访问 gradio_api/startup-events,收到 404 后启动失败。
环境排查
- 确认 Gradio 版本是否为
7.0-dev或包含相关修复的版本。 - 确认是否使用
gr.Server并直接调用了app.mount("/", StaticFiles(...))。 - 确认
executor取值:process(默认)或thread,两者均可能触发。 - 检查静态目录
site中是否存在与 Gradio 保留路径同名的文件或文件夹,如config、gradio_api。 - 检查浏览器或客户端请求
/config、/gradio_api/info时的响应状态码是否为 404。 - Issue 未提供操作系统、Python、CUDA、PyTorch、显卡和依赖版本信息,这些无法作为排查依据。
解决步骤
- 确认当前 Gradio 版本低于包含 PR #14085 的修复版本。
- 升级 Gradio 到包含该 PR 的版本;具体可用版本请查阅官方发布记录,Issue 中未给出明确版本号。
- 升级后保持
app.mount("/", StaticFiles(directory="site", html=True))的写法,无需再挂到/site前缀。 - 检查静态目录:不要存在与 Gradio 保留路径同名的文件或文件夹,否则
launch()会按预期抛出明确错误。 - 若暂时无法升级,可优先尝试把静态文件挂到非根前缀(如
/site),避免Mount匹配全部路径;这是 Issue 中提到的现状,不是已验证修复。
验证方法
启动应用后访问根路径页面,确认前端正常加载;再请求 /config 和 /gradio_api/info,确认返回不再是 404;在 executor="thread" 下确认应用能正常启动且不再出现 gradio_api/startup-events 的 404 报错。若静态目录中有保留路径同名项,确认 launch() 给出明确错误提示。
参考来源
修复 PR:gradio-app/gradio #14085(链接来自 Issue 评论摘要,评论中仅给出该 PR 地址)。
AI 工具推荐
想把多个 AI 模型放在一个入口?
GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。
推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。
这个方案解决了吗?
可以继续搜索完整报错,或查看同一工具的其他排查指南。
![[Feature/Bug]: Add opt-in strict stream validation (strict_stream_completion) to catch truncated SSE streams](https://www.chat-gpts.plus/wp-content/uploads/2026/11/42085-9a3684e8-768x403.jpg)
![[Bug]: fastapi==0.124.4 pin blocks starlette CVE-2026-48710 (BadHost)](https://www.chat-gpts.plus/wp-content/uploads/2026/11/28993-0c3ce19a-768x403.jpg)
