Skip to content

fix: guard ServerRegions/AgentDeployments slice access in agent log streaming#833

Open
j0nscalet wants to merge 1 commit intolivekit:mainfrom
j0nscalet:fix/guard-server-regions-slice
Open

fix: guard ServerRegions/AgentDeployments slice access in agent log streaming#833
j0nscalet wants to merge 1 commit intolivekit:mainfrom
j0nscalet:fix/guard-server-regions-slice

Conversation

@j0nscalet
Copy link
Copy Markdown

@j0nscalet j0nscalet commented Apr 25, 2026

Problem

On v2.16.2, lk agent create . panics immediately after a successful build/deploy if you accept the post-deploy "view logs?" prompt:

Created agent with ID [agt-xxxxxxxxxxxx]
Build completed - You can view build logs later with `lk agent logs --log-type=build`
? Agent deploying. Would you like to view logs? Yes
Tailing runtime logs...safe to exit at any time
panic: runtime error: index out of range [0] with length 0
        github.com/livekit/livekit-cli/v2/cmd/lk/agent.go:646

The agent itself deploys fine — the crash is in the post-deploy log-tailer — so it's cosmetic, but it's a jarring stack trace at the end of an otherwise successful flow.

Diagnosis

Two unguarded slice indexes in cmd/lk/agent.go:

  1. Line 646 (the panic): the region for log streaming is read from the first element of the CreateAgent response's ServerRegions slice without a length check. When the server returns an empty ServerRegions, the CLI panics.
  2. Line 956 (latent, same class): the agent logs subcommand checks that the Agents slice from ListAgents is non-empty, but then unconditionally reads AgentDeployments[0] on the first agent. An agent with zero deployments would panic the same way.

Fix

cmd/lk/agent.go:646 — before:

} else if viewLogs {
    fmt.Println("Tailing runtime logs...safe to exit at any time")
    return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, os.Stdout, resp.ServerRegions[0])
}

after:

} else if viewLogs {
    fmt.Println("Tailing runtime logs...safe to exit at any time")
    logRegion := region
    if len(resp.ServerRegions) > 0 {
        logRegion = resp.ServerRegions[0]
    }
    return agentsClient.StreamLogs(ctx, "deploy", lkConfig.Agent.ID, os.Stdout, logRegion)
}

region is already resolved earlier in createAgent and is the region the build was targeted at — a sensible fallback for log streaming.

cmd/lk/agent.go:956 — before:

if len(response.Agents) == 0 {
    return fmt.Errorf("no agent deployments found")
}

return agentsClient.StreamLogs(ctx, cmd.String("log-type"), agentID, os.Stdout, response.Agents[0].AgentDeployments[0].ServerRegion)

after:

if len(response.Agents) == 0 {
    return fmt.Errorf("no agent deployments found")
}
if len(response.Agents[0].AgentDeployments) == 0 {
    return fmt.Errorf("agent has no deployments to stream logs from")
}

return agentsClient.StreamLogs(ctx, cmd.String("log-type"), agentID, os.Stdout, response.Agents[0].AgentDeployments[0].ServerRegion)

Definition of Done

  • lk agent create . → accept "view logs?" prompt → no panic, even when the CreateAgent response has an empty ServerRegions
  • lk agent logs against an agent with zero deployments → returns a clear error instead of panicking

createAgent panics with 'index out of range [0] with length 0' when
the CreateAgent response returns an empty ServerRegions and the user
accepts the post-deploy 'view logs?' prompt. Fall back to the region
already resolved earlier in the function.

agentLogs has the same class of bug on AgentDeployments: Agents is
length-checked but AgentDeployments is not. Return a clean error
instead of panicking when an agent has no deployments.
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.

1 participant