Skip to content

An open-source, faster Devin alternative

TypeScript

wip starttime exporation

Brian Reardonbfc34dc1dcc4
diff --git a/internal/qemu/manager.go b/internal/qemu/manager.go
index 10c9ebb2..ab8b2370 100644
--- a/internal/qemu/manager.go
+++ b/internal/qemu/manager.go
@@ -243,17 +243,29 @@ func (m *Manager) PrepareGoldenSnapshot() error {
}
log.Printf("qemu: golden VM booted, agent ready (%dms)", time.Since(t0).Milliseconds())
- // Close agent connection before migration
- agentClient.Close()
+ // Close agent connection before migration. Use a timeout because gRPC's
+ // graceful close over vsock can hang if vhost-vsock doesn't drain cleanly.
+ closeDone := make(chan struct{})
+ go func() {
+ agentClient.Close()
+ close(closeDone)
+ }()
+ select {
+ case <-closeDone:
+ case <-time.After(2 * time.Second):
+ log.Printf("qemu: golden: agent close timed out, proceeding anyway")
+ }
time.Sleep(500 * time.Millisecond)
// QMP stop + migrate
+ log.Printf("qemu: golden: sending QMP stop...")
if err := qmpClient.Stop(); err != nil {
qmpClient.Close()
cmd.Process.Kill()
cmd.Wait()
return fmt.Errorf("golden QMP stop: %w", err)
}
+ log.Printf("qemu: golden: VM stopped, starting migration...")
migrateURI := fmt.Sprintf("exec:cat > %s", memFile)
if err := qmpClient.Migrate(migrateURI); err != nil {
@@ -284,7 +296,8 @@ func (m *Manager) PrepareGoldenSnapshot() error {
os.Remove(workspaceFile)
os.Remove(qmpSockPath)
- // Compress golden mem with zstd for faster restore
+ // Compress golden mem with zstd — on EBS volumes, reading less data from disk
+ // is faster than raw I/O despite the CPU cost of decompression.
zstCmd := exec.Command("zstd", "-3", "--rm", memFile, "-o", memFile+".zst")
if out, err := zstCmd.CombinedOutput(); err != nil {
log.Printf("qemu: golden zstd compress failed (will use raw): %v (%s)", err, string(out))
@@ -399,7 +412,7 @@ func (m *Manager) createFromGolden(ctx context.Context, cfg types.SandboxConfig,
}
// Build QEMU args with -incoming to restore from golden snapshot.
- // Use zstd-compressed mem file if available (typically ~40% of raw size).
+ // Use zstd-compressed mem file if available (less EBS I/O despite CPU cost).
goldenMemZst := filepath.Join(m.goldenDir, "mem.zst")
goldenMemRaw := filepath.Join(m.goldenDir, "mem")
var incomingURI string
diff --git a/internal/qemu/qmp.go b/internal/qemu/qmp.go
index ddafa7d5..66df8156 100644
--- a/internal/qemu/qmp.go
+++ b/internal/qemu/qmp.go
@@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"net"
+ "os"
"sync"
+ "syscall"
"time"
)
@@ -223,6 +225,54 @@ func (q *QMPClient) Execute(command string, args map[string]interface{}) error {
return err
}
+// SendFd passes an open file descriptor to QEMU via the QMP getfd command.
+// QEMU receives the fd via SCM_RIGHTS and registers it under the given name.
+func (q *QMPClient) SendFd(name string, f *os.File) error {
+ q.mu.Lock()
+ defer q.mu.Unlock()
+
+ unixConn, ok := q.conn.(*net.UnixConn)
+ if !ok {
+ return fmt.Errorf("QMP connection is not a Unix socket")
+ }
+
+ // Build the getfd command
+ cmd := qmpCommand{
+ Execute: "getfd",
+ Arguments: map[string]string{"fdname": name},
+ }
+ data, _ := json.Marshal(cmd)
+ data = append(data, '\n')
+
+ // Send the command with the fd attached via SCM_RIGHTS
+ rights := syscall.UnixRights(int(f.Fd()))
+ _, _, err := unixConn.WriteMsgUnix(data, rights, nil)
+ if err != nil {
+ return fmt.Errorf("send fd via SCM_RIGHTS: %w", err)
+ }
+
+ // Read response
+ _ = q.conn.SetDeadline(time.Now().Add(10 * time.Second))
+ defer q.conn.SetDeadline(time.Time{})
+ for {
+ line, err := q.r.ReadBytes('\n')
+ if err != nil {
+ return fmt.Errorf("read getfd response: %w", err)
+ }
+ var resp qmpResponse
+ if err := json.Unmarshal(line, &resp); err != nil {
+ return fmt.Errorf("parse getfd response: %w", err)
+ }
+ if resp.Event != "" {
+ continue
+ }
+ if resp.Error != nil {
+ return fmt.Errorf("getfd error: %s: %s", resp.Error.Class, resp.Error.Desc)
+ }
+ return nil
+ }
+}
+
// Close closes the QMP connection.
func (q *QMPClient) Close() error {
if q == nil || q.conn == nil {