Skip to content

An open-source, faster Devin alternative

TypeScript

grpc back off fix, cache drop

Brian Reardon6f6724a02cc8
diff --git a/internal/firecracker/agent_client.go b/internal/firecracker/agent_client.go
index dba4956d..158cb238 100644
--- a/internal/firecracker/agent_client.go
+++ b/internal/firecracker/agent_client.go
@@ -10,6 +10,7 @@ import (
pb "github.com/opensandbox/opensandbox/proto/agent"
"google.golang.org/grpc"
+ "google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials/insecure"
)
@@ -34,6 +35,14 @@ func NewAgentClient(vsockPath string) (*AgentClient, error) {
"passthrough:///vsock",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
+ grpc.WithConnectParams(grpc.ConnectParams{
+ Backoff: backoff.Config{
+ BaseDelay: 10 * time.Millisecond,
+ Multiplier: 1.6,
+ Jitter: 0.2,
+ MaxDelay: 1 * time.Second,
+ },
+ }),
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
return dialVsock(ctx, vsockPath, 1024)
}),
diff --git a/internal/firecracker/manager.go b/internal/firecracker/manager.go
index e604ccba..fde046f7 100644
--- a/internal/firecracker/manager.go
+++ b/internal/firecracker/manager.go
@@ -434,13 +434,21 @@ func (m *Manager) createWithID(ctx context.Context, id string, cfg types.Sandbox
// waitForAgent polls the agent via gRPC until it responds or times out.
func (m *Manager) waitForAgent(ctx context.Context, vsockPath string, timeout time.Duration) (*AgentClient, error) {
- deadline := time.Now().Add(timeout)
+ t0 := time.Now()
+ deadline := t0.Add(timeout)
var lastErr error
+ attempts := 0
for time.Now().Before(deadline) {
+ attempts++
+ tAttempt := time.Now()
client, err := NewAgentClient(vsockPath)
if err != nil {
lastErr = err
+ if attempts <= 3 || attempts%10 == 0 {
+ log.Printf("firecracker: waitForAgent: attempt %d dial failed (%dms): %v",
+ attempts, time.Since(tAttempt).Milliseconds(), err)
+ }
time.Sleep(50 * time.Millisecond)
continue
}
@@ -451,14 +459,20 @@ func (m *Manager) waitForAgent(ctx context.Context, vsockPath string, timeout ti
if err != nil {
client.Close()
lastErr = err
+ if attempts <= 3 || attempts%10 == 0 {
+ log.Printf("firecracker: waitForAgent: attempt %d ping failed (%dms): %v",
+ attempts, time.Since(tAttempt).Milliseconds(), err)
+ }
time.Sleep(50 * time.Millisecond)
continue
}
+ log.Printf("firecracker: waitForAgent: connected on attempt %d (%dms total)",
+ attempts, time.Since(t0).Milliseconds())
return client, nil
}
- return nil, fmt.Errorf("agent not ready after %v: %v", timeout, lastErr)
+ return nil, fmt.Errorf("agent not ready after %v (%d attempts): %v", timeout, attempts, lastErr)
}
// Get returns sandbox info by ID.
diff --git a/internal/firecracker/snapshot.go b/internal/firecracker/snapshot.go
index 15fbdcbc..a362fac5 100644
--- a/internal/firecracker/snapshot.go
+++ b/internal/firecracker/snapshot.go
@@ -1179,25 +1179,22 @@ func (m *Manager) warmRestoreFromCheckpoint(ctx context.Context, vm *VMInstance,
sandboxDir := vm.sandboxDir
endAt := vm.EndAt
- // Step 1: Close agent on current VM
+ // Step 1: Close agent + kill VM + clean up
if vm.agent != nil {
vm.agent.Close()
vm.agent = nil
}
-
- // Step 2: Kill the current Firecracker process
if vm.cmd != nil && vm.cmd.Process != nil {
_ = vm.cmd.Process.Kill()
_ = vm.cmd.Wait()
}
-
- // Step 3: Clean up sockets
_ = os.Remove(vm.vsockPath)
if vm.apiSockPath != "" {
_ = os.Remove(vm.apiSockPath)
}
+ log.Printf("firecracker: warmRestore %s: killed (%dms)", sandboxID, time.Since(t0).Milliseconds())
- // Step 4: Reflink copy checkpoint drives to sandbox dir (same absolute paths baked into vmstate)
+ // Step 2: Reflink copy checkpoint drives to sandbox dir (same absolute paths baked into vmstate)
rootfsPath := filepath.Join(sandboxDir, "rootfs.ext4")
workspacePath := filepath.Join(sandboxDir, "workspace.ext4")
if err := copyFileReflink(filepath.Join(cacheDir, "rootfs.ext4"), rootfsPath); err != nil {
@@ -1206,8 +1203,9 @@ func (m *Manager) warmRestoreFromCheckpoint(ctx context.Context, vm *VMInstance,
if err := copyFileReflink(filepath.Join(cacheDir, "workspace.ext4"), workspacePath); err != nil {
return fmt.Errorf("copy checkpoint workspace: %w", err)
}
+ log.Printf("firecracker: warmRestore %s: drives copied (%dms)", sandboxID, time.Since(t0).Milliseconds())
- // Step 5: Release old network and reclaim the same TAP name (baked into vmstate)
+ // Step 3: Release old network and reclaim the same TAP name (baked into vmstate)
if vm.network != nil {
RemoveDNAT(vm.network)
DeleteTAP(vm.network.TAPName)
@@ -1241,7 +1239,9 @@ func (m *Manager) warmRestoreFromCheckpoint(ctx context.Context, vm *VMInstance,
return fmt.Errorf("add DNAT: %w", err)
}
- // Step 6: Start fresh Firecracker process (snapshot mode — no config needed)
+ log.Printf("firecracker: warmRestore %s: network ready (%dms)", sandboxID, time.Since(t0).Milliseconds())
+
+ // Step 4: Start fresh Firecracker process (snapshot mode — no config needed)
apiSockPath := filepath.Join(sandboxDir, "firecracker.sock")
vsockPath := meta.VsockPath
_ = os.Remove(apiSockPath)
@@ -1272,7 +1272,9 @@ func (m *Manager) warmRestoreFromCheckpoint(ctx context.Context, vm *VMInstance,
return fmt.Errorf("wait for API socket: %w", err)
}
- // Step 7: Load snapshot — VM resumes exactly where it was paused during checkpoint
+ log.Printf("firecracker: warmRestore %s: FC started (%dms)", sandboxID, time.Since(t0).Milliseconds())
+
+ // Step 5: Load snapshot — VM resumes exactly where it was paused during checkpoint
if err := fcClient.LoadSnapshot(vmstateFile, memFile, true); err != nil {
cmd.Process.Kill()
cmd.Wait()
@@ -1282,7 +1284,7 @@ func (m *Manager) warmRestoreFromCheckpoint(ctx context.Context, vm *VMInstance,
log.Printf("firecracker: RestoreFromCheckpoint %s/%s: snapshot loaded (%dms)", sandboxID, checkpointID, time.Since(t0).Milliseconds())
- // Step 8: Wait for agent (should be near-instant — agent was running when snapshot was taken)
+ // Step 6: Wait for agent (should be near-instant — agent was running when snapshot was taken)
agentClient, err := m.waitForAgent(context.Background(), vsockPath, 10*time.Second)
if err != nil {
cmd.Process.Kill()