An open-source, faster Devin alternative
TypeScript
fixes for wake to bad network on agent upgrade
Brian Reardonea3df17136b7
diff --git a/.github/workflows/build-worker-ami.yml b/.github/workflows/build-worker-ami.ymlindex dc1beafd..a16e0963 100644--- a/.github/workflows/build-worker-ami.yml+++ b/.github/workflows/build-worker-ami.yml@@ -64,12 +64,23 @@ jobs:VERSION=$(git rev-parse --short HEAD)echo "VERSION=$VERSION" >> $GITHUB_ENV+ # Agent version bumps only when actual agent code changes. Using the+ # repo-level HEAD SHA would mark every AMI build as a new agent, causing+ # spurious in-place upgrades on every rolling replace — a known source+ # of fragility during wake (keepalive misses during 12MB transfer poison+ # the virtio-serial gRPC connection).+ AGENT_VERSION=$(git log -1 --pretty=format:%h -- cmd/agent internal/agent proto/agent)+ if [ -z "$AGENT_VERSION" ]; then+ AGENT_VERSION=$VERSION+ fi+ echo "AGENT_VERSION=$AGENT_VERSION"+CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \- -ldflags "-X main.WorkerVersion=$VERSION -X main.AgentVersion=$VERSION" \+ -ldflags "-X main.WorkerVersion=$VERSION -X main.AgentVersion=$AGENT_VERSION" \-o bin/opensandbox-worker ./cmd/worker/CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \- -ldflags "-X main.Version=$VERSION" \+ -ldflags "-X main.Version=$AGENT_VERSION" \-o bin/osb-agent ./cmd/agent/- name: Package rootfs contextdiff --git a/internal/qemu/manager.go b/internal/qemu/manager.goindex 97168f8d..5851f3e1 100644--- a/internal/qemu/manager.go+++ b/internal/qemu/manager.go@@ -3036,6 +3036,35 @@ func (m *Manager) RecoverLocalSandboxes() []LocalRecovery {return recoveries}+// waitAgentStable polls the agent with tight-deadline Ping calls until three+// consecutive pings succeed quickly — indicating the guest is past its+// post-resume disk I/O / scheduler thrash and can sustain the keepalive cadence+// without dropping ACKs mid-transfer. Returns false if stability isn't reached+// before the deadline; caller should defer the upgrade rather than risk+// poisoning vm.agent.+func (m *Manager) waitAgentStable(vm *VMInstance, timeout time.Duration) bool {+ if vm.agent == nil {+ return false+ }+ deadline := time.Now().Add(timeout)+ consecutiveOK := 0+ for time.Now().Before(deadline) {+ pingCtx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)+ _, err := vm.agent.Ping(pingCtx)+ cancel()+ if err == nil {+ consecutiveOK+++ if consecutiveOK >= 3 {+ return true+ }+ } else {+ consecutiveOK = 0+ }+ time.Sleep(500 * time.Millisecond)+ }+ return false+}+// upgradeAgentIfNeeded checks the agent version inside a running VM and// hot-upgrades it if the version doesn't match. Transfers the binary in// 256KB chunks, then tells the agent to re-exec. Blocks until complete.@@ -3056,6 +3085,17 @@ func (m *Manager) upgradeAgentIfNeeded(ctx context.Context, vm *VMInstance) {}log.Printf("qemu: agent %s: version mismatch (agent=%s, expected=%s), upgrading", vm.ID, agentVersion, m.cfg.AgentVersion)+ // Wait for agent to be stably responsive before starting the 12MB transfer.+ // Right after wake/fork, guest disk I/O and scheduler recovery can stretch+ // individual RPCs to hundreds of milliseconds — long enough for gRPC+ // keepalive to miss its ACK and tear down the connection mid-chunk.+ // Since agent upgrades only fire on real code changes (rare), a multi-second+ // stability wait here is cheap compared to a poisoned connection.+ if !m.waitAgentStable(vm, 30*time.Second) {+ log.Printf("qemu: agent %s: stability wait timed out, deferring upgrade (agent will keep running old version)", vm.ID)+ return+ }+agentData, err := os.ReadFile(m.cfg.AgentBinaryPath)if err != nil {log.Printf("qemu: agent %s: upgrade failed (read binary): %v", vm.ID, err)@@ -3081,13 +3121,19 @@ func (m *Manager) upgradeAgentIfNeeded(ctx context.Context, vm *VMInstance) {chunkCancel()if err != nil {log.Printf("qemu: agent %s: upgrade aborted (chunk at %d): %v", vm.ID, offset, err)- cleanCtx, cleanCancel := context.WithTimeout(context.Background(), 5*time.Second)- vm.agent.Exec(cleanCtx, &pb.ExecRequest{- Command: "/bin/sh",- Args: []string{"-c", fmt.Sprintf("rm -f %s %s.chunk", tmpPath, tmpPath)},- RunAsRoot: true,- })- cleanCancel()+ // The gRPC connection is now in a degraded state — the chunk write+ // timed out or got a keepalive failure, leaving a poisoned conn.+ // Reconnect so subsequent callers get a working client instead of+ // "use of closed network connection" on every RPC.+ _ = vm.agent.Close()+ newAgent, reconnErr := m.waitForAgentSocket(context.Background(), vm.agentSockPath, 15*time.Second)+ if reconnErr != nil {+ log.Printf("qemu: agent %s: reconnect after upgrade-abort failed: %v (marking agent unavailable)", vm.ID, reconnErr)+ vm.agent = nil+ return+ }+ vm.agent = newAgent+ log.Printf("qemu: agent %s: reconnected after upgrade-abort", vm.ID)return}diff --git a/internal/qemu/snapshot.go b/internal/qemu/snapshot.goindex aabfe868..70c6e1c2 100644--- a/internal/qemu/snapshot.go+++ b/internal/qemu/snapshot.go@@ -527,7 +527,11 @@ func (m *Manager) doWake(ctx context.Context, sandboxID, checkpointKey string, c}vm.agent = agentClient- m.upgradeAgentIfNeeded(context.Background(), vm)+ // Agent upgrade runs in the background — a running (even older-version)+ // agent is fine for callers, and pushing a 12MB binary over virtio-serial+ // during resume thrash can poison the gRPC connection via keepalive miss.+ // If upgrade fails, upgradeAgentIfNeeded's reconnect logic restores vm.agent.+ go m.upgradeAgentIfNeeded(context.Background(), vm)m.mu.Lock()m.vms[sandboxID] = vm