runner: a nil-yielding agent makes Run and RunLive uninterruptible
🔴 Required Information
Describe the Bug:
#1417 added if event == nil { continue } to Runner.Run (runner/runner.go:721) and Runner.RunLive (:939). The guard skips the event without calling yield, so the consumer's loop body is never entered. A consumer cannot break out, because break works by yield returning false and yield is never called. Neither loop consults ctx either.
So an agent that yields nils in a loop spins forever inside Run, ignoring both context cancellation and the consumer's break.
Steps to Reproduce:
- Save the test below as
runner/nilflood_test.go. - Run
go test ./runner/ -run TestRun_NilFloodIsInterruptible -timeout 60s. - It fails after three seconds.
func TestRun_NilFloodIsInterruptible(t *testing.T) {
var yielded atomic.Int64
a, err := agent.New(agent.Config{
Name: "nil_flood",
Run: func(agent.InvocationContext) iter.Seq2[*session.Event, error] {
return func(yield func(*session.Event, error) bool) {
for {
yielded.Add(1)
if !yield(nil, nil) {
return
}
}
}
},
})
if err != nil {
t.Fatalf("agent.New: %v", err)
}
r, err := New(Config{
AppName: "app",
Agent: a,
SessionService: session.InMemoryService(),
AutoCreateSession: true,
})
if err != nil {
t.Fatalf("New: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancelled before Run is called
var bodyRuns atomic.Int64
done := make(chan struct{})
go func() {
defer close(done)
msg := &genai.Content{Parts: []*genai.Part{{Text: "hi"}}}
for range r.Run(ctx, "u", "s", msg, agent.RunConfig{}) {
bodyRuns.Add(1)
break
}
}()
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatalf("Run did not return 3s after ctx cancellation: consumer body ran %d times, agent yielded %d nils",
bodyRuns.Load(), yielded.Load())
}
}
Expected Behavior:
Run returns when the context is cancelled, and the consumer gets at least one turn so its break can take effect.
Observed Behavior:
--- FAIL: TestRun_NilFloodIsInterruptible (3.00s)
nilflood_test.go:60: Run did not return 3s after ctx cancellation: consumer body ran 0 times, agent yielded 160306865 nils
The context was already cancelled before Run was called. Run still absorbed 160 million nils in three seconds and never gave the consumer a single iteration.
Environment Details:
- ADK Library Version:
mainat ffddb2a6 - OS: Linux
- Go Version: go1.26.6
Model Information: N/A, this is runner control flow.
🟡 Optional Information
Regression:
No. Before #1417 the same agent panicked on its first nil, so nothing that used to work has stopped working. The trigger is a compile-time agent supplied by the application author rather than anything a caller can send, so severity is low. It is worth fixing because it is an unbounded loop in a ctx-taking library function that ignores ctx.
Suggested fix:
This is contained in the lines #1417 already touched. It fixes the repro above and leaves go test ./... green.
if event == nil {
+ log.Printf("adk: agent %q yielded a nil event; skipping it", r.rootAgent.Name())
+ if err := ctx.Err(); err != nil {
+ yield(nil, err)
+ return
+ }
continue
}
The same three lines apply at :939 in RunLive, with agentToRun.Name() in place of r.rootAgent.Name().
The log line is separate from the hang. Today the skip is silent, so an agent author whose agent yields nil sees a run with zero events, no error and nothing in the log. runner.go reports anomalies of this kind in six other places, the nearest at :650.
Two test gaps from the same change:
TestRunner_NilEventYieldedDoesNotPanicdoes not pin what happens after a nil. Changing:722fromcontinuetobreak, and separately toreturn, leaves./runner/green. In production either would silently truncate the rest of the run. Yielding a real event after the nil and asserting it arrives closes this.- The
RunLiveguard at:939has no coverage. Deleting those three lines outright also leaves./runner/green.live_runner_test.goalready has the harness for one case.
Thanks for filing this issue. I reproduced the nil-event flood in both Runner.Run and Runner.RunLive.
I opened PR #1479 with a focused fix:
- Check
ctx.Err()when a nil event is received. - Return the cancellation error when the context is canceled.
- Continue skipping nil events while the context is still active.
- Add regression tests for both
RunandRunLive. - Verify that a valid event after a nil event is still delivered.
The change keeps the behavior introduced by #1417: nil events are not forwarded to downstream consumers, while preventing an unbounded loop after context cancellation.
Please review PR #1479 and let me know if the cancellation and error-propagation behavior should be adjusted.