Handler timeout leaves zombie threads (can't cancel blocking sync code)

未关闭
#31 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

  • #32 来自 @neubig —— 已关闭,未合并
  • #33 来自 @neubig —— 已关闭,未合并

评估

难度
4/5
预计耗时
3-5 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

Start at execute_handler and trace both synchronous handlers and async handlers using run_in_executor, then review EventBus lifecycle behavior around bus.stop. Run tests/test_handler_timeout_zombie_thread.py with the provided uv command; done means the timeout path completes without leaving a live non-daemon worker thread.

由索引模型根据 Issue 内容生成。

描述

Background

The OpenHands agent-server uses bubus (via browser_use) for browser tool event handling. When a browser launch hangs on a C-level lock (e.g. missing browser deps on a headless server), the handler timeout fires after 30s but the worker thread is never freed. Zombie threads accumulate until the thread pool is exhausted — the server reports healthy (/health → 200) but new conversation creation stalls. Only a process restart clears it.

Observed in production: 21 zombie threads across 11 conversations. Tracked in OpenHands/software-agent-sdk#4598.

Bug

execute_handler's timeout cancels the asyncio task but CancelledError can't be delivered to a handler blocked in synchronous code (run_in_executor, C-level lock). The finally block waits only 0.1s then silently abandons the task, leaving a zombie thread:

if handler_task and not handler_task.done():
    handler_task.cancel()
    try:
        await asyncio.wait_for(handler_task, timeout=0.1)
    except (asyncio.CancelledError, TimeoutError):
        pass

Reproduce

git clone https://github.com/browser-use/bubus.git && cd bubus && uv sync

Save as tests/test_handler_timeout_zombie_thread.py:

import asyncio, threading, time, pytest
from bubus import BaseEvent, EventBus

class BlockEvent(BaseEvent[str]):
    pass

@pytest.mark.asyncio
async def test_timeout_does_not_leave_zombie_thread():
    thread_started = threading.Event()
    thread_should_stop = threading.Event()

    def _block_sync():
        thread_started.set()
        thread_should_stop.wait(timeout=30)
        return "done"

    async def blocking_handler(event: BlockEvent) -> str:
        loop = asyncio.get_event_loop()
        return await loop.run_in_executor(None, _block_sync)

    bus = EventBus(name="test_zombie")
    bus.on(BlockEvent, blocking_handler)
    bus._start()
    bus.dispatch(BlockEvent(event_timeout=1.0))
    try:
        await asyncio.wait_for(bus.step(timeout=1.0), timeout=5.0)
    except (asyncio.TimeoutError, TimeoutError, Exception):
        pass
    thread_should_stop.set()
    time.sleep(1)
    await bus.stop(timeout=1, clear=True)
    assert thread_started.is_set()
    zombies = [t for t in threading.enumerate()
               if t is not threading.main_thread() and t.is_alive() and not t.daemon]
    assert len(zombies) == 0, f"{len(zombies)} zombie thread(s) alive: {[t.name for t in zombies]}"
uv run pytest tests/test_handler_timeout_zombie_thread.py -xvs
FAILED  AssertionError: 1 zombie thread(s) alive: ['asyncio_0']

Root Cause

Two code paths, same problem:

  1. Sync handlers run directly in the event loop thread (handler(event)), blocking the loop — timeout never fires.
  2. Async handlers using run_in_executor: CancelledError is queued but never delivered (no await point in the blocking thread).

Proposed Fixes

  • #32: Abandon uncancellable handlers with a warning (1s grace period, then release the global lock). Doesn't eliminate zombies.
  • #33: Run sync handlers via anyio.to_thread.run_sync(abandon_on_cancel=True) so the timeout can actually fire.
主要语言
Python
星标
131
派生
28
平均合并
18 分钟
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

browser-use/bubus 的其他 Issue

查看 browser-use/bubus 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。