fix(console,app): release QMP socket after each op and serialize drop paste

This commit is contained in:
千川白浪 committed 2026-12-02 06:45:01 -07:00
1 parent 9b7d5b86bd
commit 4a565edf11
4 files changed
+139 -15

No files matched your search

+6
View File
@@ -200,6 +200,7 @@ func (a *App) currentGuestToolsServer() *guesttools.Server {
}
var sendDropClickAndPaste = qmp.SendAbsoluteClickAndPaste
var defaultDropClickAndPaste = true
func (a *App) handleHostDropPaste(ctx context.Context, vmKey, name, dropID string, displayIndex int, normX, normY float64, activateClick bool, commandKey string, focusWait time.Duration) error {
if _, running := a.currentVMPID(ctx, name); !running {
@@ -209,6 +210,11 @@ func (a *App) handleHostDropPaste(ctx context.Context, vmKey, name, dropID strin
if err != nil {
return err
}
if defaultDropClickAndPaste && fmt.Sprintf("%p", sendDropClickAndPaste) == fmt.Sprintf("%p", qmp.SendAbsoluteClickAndPaste) {
if svc, svcErr := a.computerUseService(); svcErr == nil && svc != nil {
return svc.SendAbsoluteClickAndPaste(ctx, d.Directory, normX, normY, activateClick, commandKey, focusWait)
}
}
return sendDropClickAndPaste(ctx, d.Directory, normX, normY, activateClick, commandKey, focusWait)
}
+28 -15
View File
@@ -111,6 +111,17 @@ func (m *Manager) TypeText(ctx context.Context, vmDir, text string, progress fun
return done, err
}
// SendAbsoluteClickAndPaste clicks at normalized coordinates and presses Command+V
// while holding the VM's exclusive console lock.
func (m *Manager) SendAbsoluteClickAndPaste(ctx context.Context, vmDir string, normX, normY float64, activateClick bool, commandKey string, focusWait time.Duration) error {
if focusWait <= 0 {
focusWait = 120 * time.Millisecond
}
return m.withConn(ctx, vmDir, func(conn qmpSession) error {
return qmp.ClickAndPasteWithActivate(ctx, conn, normX, normY, activateClick, 550*time.Millisecond, 25*time.Millisecond, focusWait, 25*time.Millisecond, commandKey)
})
}
// SendPasteShortcut sends Command+V through the same exclusive console path.
func (m *Manager) SendPasteShortcut(ctx context.Context, vmDir string) error {
return m.withConn(ctx, vmDir, func(conn qmpSession) error {
@@ -246,25 +257,27 @@ func (m *Manager) withConn(ctx context.Context, vmDir string, operation func(qmp
return err
}
defer release()
if lock.conn == nil {
dial := m.dial
if dial == nil {
dial = func(ctx context.Context, vmDir string) (qmpSession, error) {
return qmp.Dial(ctx, "unix", qmp.SocketPath(vmDir))
}
}
conn, err := dial(ctx, vmDir)
if err != nil {
return err
}
lock.conn = conn
}
if err := operation(lock.conn); err != nil {
// QEMU's QMP Unix socket only serves one client at a time. Hold the per-VM
// semaphore across the operation, then close the socket immediately so
// suspend/migrate, blockstats, and external reims-ctl calls are never blocked.
if lock.conn != nil {
_ = lock.conn.Close()
lock.conn = nil
}
dial := m.dial
if dial == nil {
dial = func(ctx context.Context, vmDir string) (qmpSession, error) {
return qmp.Dial(ctx, "unix", qmp.SocketPath(vmDir))
}
}
conn, err := dial(ctx, vmDir)
if err != nil {
return err
}
return nil
defer func() {
_ = conn.Close()
}()
return operation(conn)
}
func (m *Manager) withVM(ctx context.Context, vmDir string, operation func() error) error {
+103
View File
@@ -0,0 +1,103 @@
package console
import (
"context"
"encoding/json"
"sync"
"sync/atomic"
"testing"
"time"
)
type fakeQMPSession struct {
mu sync.Mutex
commands []string
closed atomic.Bool
onExec func(cmd string)
}
func (f *fakeQMPSession) Execute(cmd string, _ any) error {
f.mu.Lock()
f.commands = append(f.commands, cmd)
f.mu.Unlock()
if f.onExec != nil {
f.onExec(cmd)
}
return nil
}
func (f *fakeQMPSession) Query(cmd string, _ any) (json.RawMessage, error) {
return json.RawMessage(`[]`), f.Execute(cmd, nil)
}
func (f *fakeQMPSession) Screendump(string) (string, error) {
return "ppm", f.Execute("screendump", nil)
}
func (f *fakeQMPSession) Close() error {
f.closed.Store(true)
return nil
}
func TestManagerReleasesConnectionAfterOperation(t *testing.T) {
m := NewManager()
var sessions []*fakeQMPSession
m.dial = func(context.Context, string) (qmpSession, error) {
s := &fakeQMPSession{}
sessions = append(sessions, s)
return s, nil
}
dir := t.TempDir()
if _, err := m.Screendump(context.Background(), dir, "frame.ppm"); err != nil {
t.Fatalf("Screendump: %v", err)
}
if len(sessions) != 1 || !sessions[0].closed.Load() {
t.Fatalf("expected QMP connection to close immediately after Screendump, sessions=%d closed=%v",
len(sessions), len(sessions) > 0 && sessions[0].closed.Load())
}
if err := m.SendAbsoluteClickAndPaste(context.Background(), dir, 0.5, 0.5, false, "meta_l", 5*time.Millisecond); err != nil {
t.Fatalf("SendAbsoluteClickAndPaste: %v", err)
}
if len(sessions) != 2 || !sessions[1].closed.Load() {
t.Fatalf("expected second QMP connection to close immediately after SendAbsoluteClickAndPaste")
}
}
func TestManagerSerializesSameVMOperations(t *testing.T) {
m := NewManager()
var active atomic.Int32
var maxActive atomic.Int32
m.dial = func(context.Context, string) (qmpSession, error) {
return &fakeQMPSession{
onExec: func(string) {
cur := active.Add(1)
for {
prev := maxActive.Load()
if cur <= prev || maxActive.CompareAndSwap(prev, cur) {
break
}
}
time.Sleep(15 * time.Millisecond)
active.Add(-1)
},
}, nil
}
dir := t.TempDir()
var wg sync.WaitGroup
for range 4 {
wg.Add(1)
go func() {
defer wg.Done()
if _, err := m.Screendump(context.Background(), dir, "frame.ppm"); err != nil {
t.Errorf("Screendump: %v", err)
}
}()
}
wg.Wait()
if got := maxActive.Load(); got != 1 {
t.Fatalf("expected max 1 concurrent QMP operation per VM, got %d", got)
}
}
+2
View File
@@ -2,6 +2,7 @@ package builtin
import (
"context"
"time"
"github.com/Nwflower/reims-go/internal/console"
"github.com/Nwflower/reims-go/internal/plugin"
@@ -15,6 +16,7 @@ type ComputerUseService interface {
Forget(ctx context.Context, vmDir string) error
Close()
SendPasteShortcut(ctx context.Context, vmDir string) error
SendAbsoluteClickAndPaste(ctx context.Context, vmDir string, normX, normY float64, activateClick bool, commandKey string, focusWait time.Duration) error
PointerAvailable(ctx context.Context, vmDir string) (bool, error)
MouseMove(ctx context.Context, vmDir string, x, y, width, height int) error
MouseButton(ctx context.Context, vmDir, button string, down bool) error