Handler timeout leaves zombie threads (can't cancel blocking sync code)
评估
调研方向
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:
- Sync handlers run directly in the event loop thread (
handler(event)), blocking the loop — timeout never fires. - Async handlers using
run_in_executor:CancelledErroris queued but never delivered (noawaitpoint 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
browser-use/bubus 的其他 Issue
-
browser-use/bubus#46 ·
-
Dispatch after `stop()` restarts `_run_loop` on the shut-down queue and spins a CPU core forever可能已有人在做 @zwk110 于 46 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 54/100
browser-use/bubus#38 ·
-
难度 3/5 1-2 天 新手友好度 48/100
browser-use/bubus#26 · 1 条评论 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 38/100
browser-use/bubus#20 · 3 条评论 ·
-
Add support for unsubscribing after using `EventBus.on`可能已有人在做 @HankGrimm 于 86 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 45/100
browser-use/bubus#5 · 6 条评论 ·
查看 browser-use/bubus 的全部 Issue
相似的 Issue
-
docs(contributing): the two things that silently block a PR可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭available documentation good first issue
难度 1/5 1-3 小时 新手友好度 95/100
Nixitov/Riptide-Native#944 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 88/100
JTHCode/secretguard#24 ·
维护者通常 1 天内回复
-
area: middleware documentation good first issue
难度 2/5 1-3 小时 新手友好度 88/100
-
enhancement good first issue
难度 2/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
good first issue
难度 1/5 1 小时以内 新手友好度 95/100
omarelshamy25x/shamy-hijri#63 ·
维护者通常 1 天内回复