Repository navigation
fix(runner): drop nil parts from live requests - #1795
Conversation
wojtas000
left a comment
There was a problem hiding this comment.
Two test cases are missing before this can merge. The fix in runnerLiveSession.Send doesn't need to change.
I reverted the fix and kept your test. Three cases panic at runner/runner.go:838 on main, and nil after function response fails on the forwarded content. With the fix, all four pass under -race. The root module suite also passes. I ran it in a sandbox with no network, and telemetry fails there on main too. Breaking the copy, either by dropping slices.Clone or by writing through *req.Content, makes the test fail as it should.
Two other edits to the new branch leave the runner and adkrest suites green, so the test doesn't pin what they would break. The inline comments have the details.
Separately, and not caused by this PR: an SDK caller sending LiveRequest{RealtimeInput: (*genai.Blob)(nil)} still panics the sender goroutine at internal/llminternal/base_flow.go:610-612. The REST handler always builds a non-nil blob, so /run_live can't trigger it. It's worth a separate issue if you're interested.
|
Thanks for the review. The three inline comments are addressed in a7d6967; see each reply for details. I'll file a separate issue for the typed nil |
wojtas000
left a comment
There was a problem hiding this comment.
Thanks, all three cases are in. I ran the same three edits to Send against a7d6967 merged onto current main, and each one now fails its new case: reversing the kept parts fails nil between texts, dropping RealtimeInput fails nil with realtime input, and copying every request fails no nil. The runner, adkrest and llminternal suites pass under -race.
* fix(runner): preserve buffered live events at stream end (#1547) (cherry picked from commit 6576191) * fix(llminternal): classify a dropped live connection by type, not error text (#1603) (cherry picked from commit ec1b74c) * fix(runner): drop nil parts from live requests (#1795) (cherry picked from commit 893687b)
Linked issue
1. Link the issue this change is for.
2. Describe the change:
Problem:
/run_livedecodes a null part in a text frame as a nil element and passes the decoded content torunnerLiveSession.Sendas is.Sendforwards the request to the agent's live session and then iterates over the parts looking for function responses. Forwarding hands the request to the base_flow sender goroutine without reading the parts. Two goroutines read the parts, and neither checks for nil: the goroutine that reads client frames and callsSendpanics atrunner/runner.go:838unless a function response comes before the nil part; the base_flow sender goroutine panics atinternal/llminternal/googlellm/live_connection.go:102when the first part is nil, and at:105when the first part is a function response and a later part is nil; when the first part is neither nil nor a function response, the sender passes the content to genai at:131and panics inside genai on a failed type assertion. Neither goroutine recovers, so the whole process exits. The process exits the same way when an SDK caller passes a request with a nil part toSendon the session returned byRunner.RunLive.Solution:
runnerLiveSession.Senddrops nil parts before forwarding, and both the request forwarded to the agent and the event written to the session use a copy of theContentwith the nil parts removed. Only a request that contains a nil part is copied, and the caller'sContentis left unchanged. Both/run_liveand SDK callers go throughrunnerLiveSession.Send. Atinternal/llminternal/base_flow.go:394-396, the live flow returns an error unless the context carriesrunconfig.RunConfig.Live.runconfigis an internal package, and outside testsLiveis set only inRunner.RunLive, atrunner/runner.go:910. A caller outside a live run therefore cannot start the live flow except through the runner.adk-python validates each frame as a
LiveRequest. When a frame with a null part fails validation, adk-python logs an error and stops reading client frames; the connection stays open and the process keeps running. Seesrc/google/adk/cli/api_server.py:2423-2439. This PR takes a different approach: it drops the nil part instead of rejecting the frame. A nil part carries no content, so dropping it loses no data. #1552 noted that nil parts can come from content an SDK user builds by hand or a client decodes from the network; it asked that thePartsaccessors ininternal/utilsskip nil parts instead of dereferencing them, and #1556 made them do so. Other places in the repo do the same:internal/llminternal/contents_processor.go:247-250drops nil parts when building the model request, andinternal/llminternal/googlellm/live_connection.go:66-68skips them when the live connection sends history. #1769 also drops null parts from/runand/run_sserequests.Behavior change
What behaves differently for someone already on the current release?
When a
/run_livetext frame contains a null part, or a request that an SDK caller passes toSendcontains a nil part, the process no longer exits. The nil part is dropped before the request is forwarded or written to the session, and the remaining parts are handled as before: if any of them is a function response, the request is not written to the session; otherwise it is written as one user event. When every part is nil, what remains is an emptyContent, the same as sending"parts":[]today: nothing is written to the session, the live session ends withempty content, a/run_liveclient receives close code 1011, and an SDK caller receives theempty contenterror from the event iterator. Requests without nil parts behave as before.If the session history already contains a nil part, for example because a message with a null part was sent earlier through
/run, opening a/run_liveconnection still triggers a panic atinternal/llminternal/contents_processor.go:872, in the goroutine started atinternal/llminternal/base_flow.go:401, and the process exits as well. That path is out of scope for this PR and is fixed by #1769: with #1769's change tocontents_processor.goapplied on top of this PR, a test against a local stub Live endpoint shows that the process no longer exits.Testing Plan
Unit Tests:
With your source change reverted and your tests kept, which test fails?
TestRunner_RunLive_SendDropsNilParts. When the test runs as a whole, its first case,nil before text, panics atrunner/runner.go:838and the test binary aborts. When the cases run one at a time,nil before text,nil after text, andonly nileach panic at:838, andnil after function responsefails its assertion because the forwarded content still contains a nil part. Thereq.Content != nilcheck is covered by existing adkrest live tests such asTestRunLiveHandler_ForwardsBinaryAudio,TestRunLiveHandler_ForwardsRealtimeInputVariants,TestRunLiveHandlerEnforcesMessageSizeLimit, andTestRunLiveHandler_KeepsResponsiveClientThroughSlowSend. These tests send frames without content through the real runner, and with the check removed they panic atrunner/runner.go:829on this branch.On Linux under WSL,
go test -shuffle=on ./...passes with go1.26.6. On Windows, every test fromgo test -race -shuffle=on ./runner/... ./server/adkrest/... ./internal/llminternal/... ./agent/workflowagents/...passes exceptTestRunnerCompactsAfterInterval,TestCompactionOverlapWidensTheStoredRange, andTestRunnerTailRetentionCompactsMidInvocationinrunner/compaction_test.go, which also fail intermittently on main. These tests do not callRunLive, so they do not reach the changed code.golangci-lint runreports no issues.Manual End-to-End (E2E) Tests:
examples/bidiwas built unmodified, once without the fix and once with it, using the example's own model,gemini-3.1-flash-live-preview, on the Gemini API backend. The steps are the same as in the issue's Steps to Reproduce. Each scenario starts a new server process, opens one WebSocket session, and sends one text frame in the same format as the example frontendstatic/js/app.js; only the parts differ. The log below is printed by a script that runs these scenarios and is not part of this PR:client:is what the client received,server:is the state of the server process, and the lines underserver:list each panic the server printed, where the panic happened, and where its goroutine was started.Before the fix, all three frames with a null part make the process exit with code 2. For each of them, both the goroutine that reads client frames and the base_flow sender goroutine can panic, and how many panics are printed before the process exits depends on scheduling; in this run,
[text, null]printed one from each goroutine, and[null, text]and[null]printed only the sender goroutine's. After the fix,[null, text]and[text, null]get a reply, as[text]does;[null]and[]both end with close code 1011 andempty content; and the process keeps running in all five scenarios.Log
Checklist