`/config` returns `username` as an unawaited coroutine string (regression from #13749])

该报错是 Gradio 源码安装(main 分支)在请求 `/config` 接口时,同步端点误调用了异步函数 `get_current_user` 且未 `await`,导致 `username` 字段返回协程对象而非用户名或 `null`。优先排查 `gradio/routes.py` 中 `g

快速结论:该报错是 Gradio 源码安装(main 分支)在请求 `/config` 接口时,同步端点误调用了异步函数 `get_current_user` 且未 `await`,导致 `username` 字段返回协程对象而非用户名或 `null`。优先排查 `gradio/routes.py` 中 `get_config` 是否为 `async def` 并正确 `await get_current_user(request)`。

适用环境:macOS(Darwin)、Python 3.12、Gradio 6.24.0(源码安装,main @ 3f52f1737)、gradio_client 2.6.0、fastapi 0.141.1、orjson 3.12.0。该回归由 #13749 引入(将 `get_current_user` 改为 async)。

最快修复方案:暂无官方确认的一步修复方案(Issue 提交时维护者已确认 bug,但未给出合并后的 patch)。可优先尝试将 `gradio/routes.py` 中的 `get_config` 改为 `async def`,并在赋值 `config[“username”]` 时使用 `await get_current_user(request)`。

注意事项:此修复方案来自 Issue 中的建议性代码,未在评论中标注为已合并或已验证;修改后需要重新构建前端或重启服务。若你使用的是 PyPI 稳定版而非 main 分支,可能不受影响,请先确认版本。

问题场景

在源码安装的 Gradio main 分支上运行任意 demo(如 gradio demo/chatbot_simple/run.py)或最小复现脚本(gr.ChatInterface(fn=echo).launch()),前端页面加载时会触发 `/config` 请求。服务端控制台出现 RuntimeWarning: coroutine ... was never awaited,且通过 curl 访问 `/config` 时,username 字段返回的是协程对象的字符串表示。

报错原文

RuntimeWarning: coroutine 'App.create_app.<locals>.get_current_user' was never awaited
/config returns `username` as an unawaited coroutine string (regression from #13749])

"username": "<coroutine object App.create_app.<locals>.get_current_user at 0x125f2eb20>"

原因分析

可能原因gradio/routes.pyget_config 是同步端点,但在设置 config["username"] 时调用了异步函数 get_current_user 且没有 await。由于 Gradio 的 ORJSONResponse.default 会对不可序列化对象回退到 str(),因此协程对象被静默转成字符串,接口不会崩溃但返回错误数据。此调用点最初在 #8720 添加时 get_current_user 还是同步函数,而 #13749 将其改为 async def(以支持可等待的 auth_dependency),但 get_config 内的调用点未同步更新。

环境排查

  • 确认 Gradio 版本是否为 6.24.0 或更高(源码安装 main 分支);PyPI 稳定版可能不含此回归。
  • 检查 gradio/routes.pyget_config 的定义是否为 async def
  • 确认 get_current_user 在当前代码中是否为 async def(#13749 之后的改动)。
  • 若启用了 auth_dependency 或登录认证,更易观察到 username 异常。

解决步骤

  1. 打开 gradio/routes.py,定位 get_config 函数定义。
  2. def get_config(...) 改为 async def get_config(...)(保持原有参数不变)。
  3. 在函数体内,将 config["username"] = get_current_user(request) 改为 config["username"] = await get_current_user(request)
  4. 保存文件并重启 Gradio 服务(源码安装需确保前端已构建,或使用开发模式)。
  5. 重新打开前端页面,或再次执行 curl -s http://127.0.0.1:7860/config 验证。

验证方法

重启服务后,观察控制台不再出现 RuntimeWarning: coroutine ... was never awaited。同时访问 /config,确认 username 返回 null(未启用认证)或实际登录用户名(启用认证时),而不是协程对象字符串。

参考来源

gradio-app/gradio #13758

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 18871

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注