From e65030ab3a7fec031d0697ab4c517cb5015dfba3 Mon Sep 17 00:00:00 2001 From: Graham Neubig <398875+neubig@users.noreply.github.com> Date: Mon, 15 Jun 2026 10:02:22 -0400 Subject: [PATCH] Avoid blocking async ACP restart on secret lookup Co-authored-by: openhands --- .../openhands/sdk/agent/acp_agent.py | 16 ++++- tests/sdk/agent/test_acp_agent.py | 67 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/openhands-sdk/openhands/sdk/agent/acp_agent.py b/openhands-sdk/openhands/sdk/agent/acp_agent.py index 8382946c4f8..6ddf6e666fb 100644 --- a/openhands-sdk/openhands/sdk/agent/acp_agent.py +++ b/openhands-sdk/openhands/sdk/agent/acp_agent.py @@ -2653,6 +2653,20 @@ def _restart_session_after_drain_timeout( self.init_state(state, on_event=on_event) self._restart_session_on_next_turn = False + async def _arestart_session_after_drain_timeout( + self, + state: ConversationState, + on_event: ConversationCallbackType, + ) -> None: + """Restart ACP without blocking the async conversation loop.""" + # Restart calls init_state(), which may synchronously resolve LookupSecret + # values through HTTP loopback to this same agent server. Keep it off the + # server loop for the same reason LocalConversation.arun() offloads the + # initial _ensure_agent_ready() path. + await asyncio.to_thread( + self._restart_session_after_drain_timeout, state, on_event + ) + def _request_session_cancel(self) -> None: """Ask the ACP server to cancel the active session prompt.""" if self._conn is None or self._executor is None or self._session_id is None: @@ -3158,7 +3172,7 @@ async def astep( if self._restart_session_on_next_turn: # If restart initialization fails, let the conversation transition # to ERROR rather than reusing an ambiguous ACP session. - self._restart_session_after_drain_timeout(state, on_event) + await self._arestart_session_after_drain_timeout(state, on_event) prompt_blocks: list[Any] | None = None if prompt_message is not None: diff --git a/tests/sdk/agent/test_acp_agent.py b/tests/sdk/agent/test_acp_agent.py index 934e43842f0..b7785b6dfd3 100644 --- a/tests/sdk/agent/test_acp_agent.py +++ b/tests/sdk/agent/test_acp_agent.py @@ -2065,6 +2065,73 @@ def _capture_event(event): conversation.state.execution_status == ConversationExecutionStatus.FINISHED ) + def test_astep_restarts_session_off_caller_loop(self, tmp_path): + """Restart init can synchronously resolve loopback LookupSecret values. + + ``astep`` must keep that restart work off the caller loop so the + agent-server can serve those loopback HTTP requests instead of waiting + for each secret lookup to time out. + """ + from openhands.sdk.utils.async_executor import AsyncExecutor + + agent = _make_agent() + conversation = self._make_conversation_with_message(tmp_path) + emitted: list = [] + + mock_client = _OpenHandsACPBridge() + mock_client.get_turn_usage_update = MagicMock(return_value=object()) + agent._client = mock_client + agent._conn = MagicMock() + agent._session_id = "test-session" + agent._restart_session_on_next_turn = True + + async def _fake_prompt(prompt_blocks, session_id): # noqa: ARG001 + mock_client.accumulated_text.append("answer") + return None + + agent._conn.prompt = _fake_prompt + + executor = AsyncExecutor() + + async def _run_restart() -> None: + caller_loop = asyncio.get_running_loop() + caller_thread_id = threading.get_ident() + restart_entered = asyncio.Event() + release_restart = threading.Event() + restart_thread_ids: list[int] = [] + + def _blocking_restart(self, state, on_event): # noqa: ARG001 + assert self is agent + restart_thread_ids.append(threading.get_ident()) + caller_loop.call_soon_threadsafe(restart_entered.set) + assert release_restart.wait(5.0) + agent._restart_session_on_next_turn = False + + with patch.object( + ACPAgent, + "_restart_session_after_drain_timeout", + new=_blocking_restart, + ): + task = asyncio.create_task( + agent.astep(conversation, on_event=emitted.append) + ) + await asyncio.wait_for(restart_entered.wait(), timeout=5.0) + assert len(restart_thread_ids) == 1 + assert restart_thread_ids[0] != caller_thread_id + release_restart.set() + await asyncio.wait_for(task, timeout=5.0) + + try: + agent._executor = executor + asyncio.run(_run_restart()) + finally: + executor.close() + + assert ( + conversation.state.execution_status == ConversationExecutionStatus.FINISHED + ) + assert emitted + def test_astep_active_prompt_survives_idle_window(self, tmp_path): """End-to-end via the real portal: an actively-streaming prompt that runs well past ``acp_prompt_timeout`` finalizes normally.