Skip to content

🐛 MCP 调用可能无限阻塞 + 认证状态未检测导致反复弹出浏览器登录 #7

Description

@kentkentlin

问题描述

在使用 Gemini MCP 过程中,发现以下严重问题:

1. 无限阻塞风险

当 Gemini CLI 卡住或等待认证输入时,MCP 调用会永远阻塞,因为 run_shell_command 缺乏全局超时机制。

代码位置: server.py:64-73

while True:
    try:
        line = output_queue.get(timeout=0.5)
        if line is None:
            break
        yield line
    except queue.Empty:
        if process.poll() is not None and not thread.is_alive():
            break
# 没有最大执行时间检查

2. 认证状态未检测

当 Gemini CLI 认证 token 过期时,CLI 会尝试弹出浏览器进行认证。但由于:

  • stdin=subprocess.DEVNULL 禁止了交互输入
  • MCP 服务器在后台运行,用户可能无法及时响应认证请求
  • Claude Code 的 MCP 健康检查可能反复触发这个问题

导致用户体验:即使不主动调用 MCP,浏览器也会反复自动弹出 Google 登录页面

3. 进程泄漏风险

thread.join(timeout=5) 后没有检查线程是否真正退出,可能留下僵尸线程/进程。

复现步骤

  1. 配置 Gemini MCP 到 Claude Code
  2. 等待 Gemini CLI 的认证 token 过期
  3. 打开多个 Claude Code 终端窗口
  4. 观察:浏览器会反复弹出 Google 登录页面

环境信息

  • OS: macOS (Darwin 24.6.0)
  • Gemini CLI: v0.22.2
  • geminimcp: v0.1.0
  • Claude Code: latest

建议修复方案

1. 增加全局超时机制

async def gemini(
    ...,
    timeout: Annotated[int, Field(description="最大执行时间(秒)")] = 300,
):

2. 调用前检查认证状态

def check_auth_status() -> tuple[bool, str]:
    """检查 gemini CLI 是否已认证"""
    try:
        result = subprocess.run(
            ["gemini", "auth", "status"],
            capture_output=True,
            timeout=10,
            text=True
        )
        # 根据输出判断认证状态
        return ("authenticated" in result.stdout.lower(), result.stdout)
    except Exception as e:
        return (False, str(e))

3. 改进进程管理

def run_shell_command(cmd, cwd=None, timeout=300):
    start_time = time.time()
    ...
    while True:
        elapsed = time.time() - start_time
        if elapsed > timeout:
            process.kill()
            raise TimeoutError(f"Gemini CLI execution timeout after {timeout}s")
        ...

相关链接

  • Issue 发现自实际使用场景的 Code Review
  • 两个独立 AI(Claude Opus 4.5 + OpenAI Codex)对代码进行了 Review,得出一致结论

Reported via Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions