Loosing SSE session from the browser shouldn't break the agent run
Describe the bug Prerequisites:
Web chat UI started with adk web for a simple agent with only a function tool that hangs for 2 minutes.
Reproduction:
- Submit a request to wait for 2 minutes and wait for tool to start executing
- Refresh browser window or terminate
/run_sseSSE exchnage opened by the browser in any other way - Find the tool call in a scrambled state. The trace will mark the completion at the time of SSE session termination; the tool will never complete, and the agent will never return a response. The session is in a strange state.
Expected behavior:
Browser window refresh shouldn't affect the session in any way.
To Reproduce
Start a long-running tool, then refresh the browser window mid-execution: the execution response will never be processed, and no further processing will happen in this Session until a new message is sent.
Expected behavior Disconnection of the browser SSE session shouldn't break the running execution of the agent / tools.
Screenshots
Before refresh:
After refresh:
Could you check once you refresh the browser, the agent is still running the long running tool?
@wyf7107 I did specify what happens after browser refresh in the description:
Find the tool call in a scrambled state. The trace will mark the completion at the time of SSE session termination; the tool will never complete, and the agent will never return a response. The session is in a strange state.
So no, the agent doesn't run anything at all after browser refresh. The tool result will never be received or routed to the agent, and will never reach the session. The only thing that will wake up that session will be a user message.
If you mean whether I can attach to the debugger and see the agent's internal state, I'm not yet comfortable with ADK internals enough. I will not be able to evaluate the internal state competently.
@Spikhalskiy This is a known limitation - tool tasks spawned via asyncio.create_task() aren't tracked or cancelled when the SSE stream closes.
As a workaround, use async tools that persist progress to session state (ctx.state), so you can check task status after reconnecting.
We're tracking this for a proper fix involving task lifecycle management tied to the SSE connection.
Team is looking more into it.
Thanks!
@ankursharmas Please have a look into it.
the /run_sse endpoint has no request.is_disconnected() check, so a browser refresh causes Aclosing() to tear down run_async() mid-tool and leave the session with an open invocation.
The right fix direction is to race the agent task against a disconnect poller (request.is_disconnected()) and cancel the agent task cleanly when the client drops.
In the meantime, persisting tool progress to ctx.state remains the safest workaround so the session can recover on the next message.
Closing this out since there's been no follow-up in a few months — feel free to reopen or submit a PR, it's a well-scoped change in adk_web_server.py and happy to review it!
@surajksharma07 What kind of follow-up was expected from me if I run adk-web with out of the box wiring? The fix should be in the ADK.
Please consider reopening it as the problem is still present and is a major issue with Google ADK Web UI with default wiring provided by adk CLI.
@wyf7107 Please have a look into it once.
We run ADK behind an authenticating gateway and hit the same underlying question from the production side, so adding data in case it helps the design.
On current main the behavior is explicit: run_agent_sse creates an
asyncio.Event and sets it when the SSE generator is closed or cancelled
(except (GeneratorExit, asyncio.CancelledError) → abort_signal.set()), and
passes it to Runner.run_async(abort_signal=...). The producer also waits on
next_step_event after every event it queues, so the run advances only as fast
as the client consumes the stream.
That is a coherent choice for adk web, where a closed tab means nobody is
listening. It is the wrong one when the SSE client is an intermediary, for
example a gateway or proxy that:
- drops idle connections while a slow tool is running (no bytes are written between events), or
- retries the same request after a timeout.
In both cases the run is aborted by a transport event that says nothing about whether the user still wants the result, and the retry then starts a second run. The tool-call/response gap that this issue describes is the visible symptom; the cause is that run lifetime and connection lifetime are the same thing.
Would a maintainers-approved opt-out be acceptable, e.g. a get_fast_api_app
option (or per-request flag) to keep the invocation running after the client
disconnects, with the current abort-on-disconnect remaining the default? The
abort_signal plumbing already exists, so I expect it to be small. I am happy to
send a PR with tests if you agree on the shape.
That is a coherent choice for
adk web, where a closed tab means nobody is listening.
It does not really seem to be a coherent choice even for adk web, but a bug.
A network interruption or a browser page refresh should not kill a running agent session.
Thanks @Spikhalskiy for the report, @Javviviii2 for the gateway observations and the opt-out proposal, and @surajksharma07 and @wyf7107 for the earlier analysis.
I tested the opt-out proposal offline against ADK commit 7b07e7df7f4a35687f2a2fff079dbecb59532c0e (main, 19 Nov 2026), using an ASGI disconnect during an in-flight tool call and a no-disconnect control. No browser or API key was needed.
Unmodified ADK: the disconnect cancels the tool, and the pending tool call is closed with a synthetic error response ("Invocation was aborted by client", event error code INVOCATION_ABORTED). The real tool result is not stored. A simple follow-up message on the same session ran normally afterwards.
With an opt-in detach prototype (ADK_DETACH_ON_DISCONNECT=1, default unchanged): the tool completes and the stored tool result matches the no-disconnect control.
Retry case: if the same request is submitted while the detached run is still active, detach alone allowed two overlapping runs on the same session, and the response was stored twice. A prototype guard that returns HTTP 409 while a detached run is active prevented that overlap. In a separate test with a tool that never returns, a timeout (ADK_DETACH_TIMEOUT_S) ended the detached run, and a following request was accepted.
Limits: the guard prevents overlap only, not duplicate execution. An identical request submitted after the detached run has finished runs again. I did not test tools that react to the abort signal, or how a real gateway handles HTTP 409.
I have a patch and tests for these cases. Would a PR focused on opt-in continuation and its retry behavior be useful? The default behavior stays unchanged. Thanks again to everyone involved.