Skip to content

fix(agent-server): offload blocking subprocess calls in DesktopService.start() - #3810

Merged
enyst merged 2 commits into
OpenHands:mainfrom
ly-wang19:fix/desktop-service-offload-blocking-subprocess
Jul 1, 2026
Merged

enyst merged 2 commits into
OpenHands:mainfrom
ly-wang19:fix/desktop-service-offload-blocking-subprocess

Conversation

@ly-wang19

@ly-wang19 ly-wang19 commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:
Verified locally — these VNC startup subprocess calls block the event loop during desktop boot; offloading them keeps the agent-server responsive.


AGENT:

Why

DesktopService.start() (openhands-agent-server) is an async def, and it already launches the long-running noVNC proxy correctly via await asyncio.create_subprocess_exec(...). But the surrounding steps still ran synchronously on the event loop:

  • is_running() — called at entry and at the final health check; each invocation runs a pgrep subprocess;
  • the Xvnc and novnc_proxy readiness pgrep probes;
  • the vncserver launch, which is passed no timeout (unbounded).

While the desktop starts, these calls block the agent server's event loop and stall every other concurrent task. The vncserver spawn is the worst case since it has no timeout.

Summary

  • Dispatch the synchronous subprocess.run(...) calls and the self.is_running() calls inside start() through asyncio.to_thread(...), matching the already-async noVNC launch. Same commands, same return-code handling — only the event loop is no longer blocked.
  • Add a regression test (test_start_offloads_blocking_calls_to_thread) asserting start() routes its blocking subprocess.run work through asyncio.to_thread.

Evidence

Commands run from the repo root:

$ uv run pytest tests/agent_server/test_desktop_service.py -q
27 passed, 2 warnings

Red→green proof the new test guards the fix:

# fix reverted (original synchronous start()):
$ git stash push openhands-agent-server/openhands/agent_server/desktop_service.py
$ uv run pytest tests/agent_server/test_desktop_service.py::TestDesktopService::test_start_offloads_blocking_calls_to_thread -q
1 failed   # AssertionError: blocking subprocess.run must be offloaded via asyncio.to_thread
$ git stash pop      # restore fix
27 passed

uv run ruff check and ruff format --check are clean on both changed files. The change is behavior-preserving (the executed commands and their semantics are unchanged), so the existing 26 DesktopService tests pass unmodified. Full VNC end-to-end startup needs a host with vncserver/novnc installed and is not reproducible here; the change is a mechanical, semantics-preserving offload of already-existing calls.

How to Test

From the repo root:

uv pip install -e ./openhands-agent-server
uv run pytest tests/agent_server/test_desktop_service.py::TestDesktopService::test_start_offloads_blocking_calls_to_thread -q

The targeted test passes with this change. To confirm it actually guards the fix, revert just the source file and re-run — it goes red:

git stash push openhands-agent-server/openhands/agent_server/desktop_service.py
uv run pytest tests/agent_server/test_desktop_service.py::TestDesktopService::test_start_offloads_blocking_calls_to_thread -q   # 1 failed
git stash pop
uv run pytest tests/agent_server/test_desktop_service.py -q   # 27 passed

A full VNC end-to-end boot needs a host with vncserver/novnc installed and isn't reproducible in CI; this change is a semantics-preserving offload of already-existing subprocess calls, so the full 27-test DesktopService suite passes unmodified.

…e.start()

DesktopService.start() is async and already launches the managed noVNC process
via "await asyncio.create_subprocess_exec", but the surrounding readiness checks
and the TigerVNC launch ran synchronously on the event loop:
  - is_running() (two calls, each a pgrep subprocess),
  - the Xvnc / novnc_proxy pgrep probes,
  - the vncserver launch (which has no timeout).

Each blocks the server's event loop for the duration of the subprocess - the
vncserver spawn in particular is unbounded - stalling other concurrent work
while the desktop starts.

Dispatch each synchronous call via asyncio.to_thread, consistent with the
already-async noVNC launch. Adds a regression test asserting start() offloads
its blocking subprocess work via asyncio.to_thread.
@ly-wang19

Copy link
Copy Markdown
Contributor Author

Verified locally — these VNC startup subprocess calls block the event loop during desktop boot; offloading them keeps the agent-server responsive.

@ly-wang19
ly-wang19 marked this pull request as ready for review June 21, 2026 14:40
@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: It has been a while since there was any activity on this PR. @ly-wang19, are you still working on it? If so, please go ahead, if not then please request review, close it, or request that someone else follow up.

This comment was created by an AI agent (OpenHands) on behalf of the repository maintainers.

@all-hands-bot
all-hands-bot requested a review from enyst July 1, 2026 13:22
@all-hands-bot

Copy link
Copy Markdown
Collaborator

[Automatic Post]: I have assigned @enyst as a reviewer based on git blame information. Thanks in advance for the help!

This comment was created by an AI agent (OpenHands) on behalf of the user.

@enyst

enyst commented Jul 1, 2026

Copy link
Copy Markdown
Member

@OpenHands Take a look at this PR and the issue it's solving. You are an agent, you are free to install yourself software you need. Do a /codereview and post your feedback with gh api here.

@openhands-ai

openhands-ai Bot commented Jul 1, 2026

Copy link
Copy Markdown

I'm on it! enyst can track my progress at all-hands.dev

@enyst enyst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Good taste - This is a small, behavior-preserving fix for a real event-loop blocking problem.

The implementation keeps the existing subprocess commands and return-code handling intact while moving the blocking subprocess.run / is_running() paths out of the event loop with asyncio.to_thread. That is the right level of change: no new abstraction, no lifecycle rewrite, and no user-visible contract change.

Validation I ran locally on the PR head:

uv run pytest tests/agent_server/test_desktop_service.py::TestDesktopService::test_start_offloads_blocking_calls_to_thread -q
# 1 passed

uv run pytest tests/agent_server/test_desktop_service.py -q
# 27 passed

uv run ruff check openhands-agent-server/openhands/agent_server/desktop_service.py tests/agent_server/test_desktop_service.py
# All checks passed!

uv run ruff format --check openhands-agent-server/openhands/agent_server/desktop_service.py tests/agent_server/test_desktop_service.py
# 2 files already formatted

[RISK ASSESSMENT]

  • [Overall PR] ⚠️ Risk Assessment: 🟢 LOW

The blast radius is limited to DesktopService.start() and a focused regression test. It does not change public APIs, dependencies, security-sensitive code, or the actual desktop startup commands. The main operational caveat remains pre-existing: vncserver can still run unbounded, but it no longer blocks the agent-server event loop while it does so.

VERDICT:
✅ Worth merging: Core logic is sound and the targeted regression coverage catches the important failure mode.

KEY INSIGHT:
The PR fixes the bad async boundary without redesigning the desktop lifecycle, which is exactly the pragmatic shape this bug needs.

This review was created by an AI agent (OpenHands) on behalf of the user.

@openhands-ai

openhands-ai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Done — I reviewed PR #3810 and submitted an approval via the GitHub REST API:

#3810 (review)

Validation run locally on the PR head:

  • uv run pytest tests/agent_server/test_desktop_service.py::TestDesktopService::test_start_offloads_blocking_calls_to_thread -q → passed
  • uv run pytest tests/agent_server/test_desktop_service.py -q → 27 passed
  • uv run ruff check ... → passed
  • uv run ruff format --check ... → already formatted

Workspace is clean.

@enyst

enyst commented Jul 1, 2026

Copy link
Copy Markdown
Member

Thank you for this!

@enyst
enyst merged commit 54980f0 into OpenHands:main Jul 1, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants