Compare commits

..
1 Commits
Author SHA1 Message Date
kory 2a072f9cc9 fix: detach local sessions from request context
Release / Build & Release (push) Canceled after 0s
2026-10-02 14:18:24 +08:00
2 changed files with 34 additions and 1 deletions
+1 -1
View File
@@ -851,7 +851,7 @@ func (s *Server) processSession(w http.ResponseWriter, r *http.Request) {
} }
} }
var sess *executor.Session var sess *executor.Session
sess, err = s.LocalSessions.Start(ctx, req.SessionID, req.Command, req.Workdir, owner, req.RingCapacity) sess, err = s.LocalSessions.Start(context.Background(), req.SessionID, req.Command, req.Workdir, owner, req.RingCapacity)
if err == nil { if err == nil {
result = map[string]any{"session_id": sess.ID, "started_at": sess.StartedAt.Unix()} result = map[string]any{"session_id": sess.ID, "started_at": sess.StartedAt.Unix()}
} }
+33
View File
@@ -9,6 +9,7 @@ import (
"strings" "strings"
"sync" "sync"
"testing" "testing"
"time"
"github.com/wjyhk/vps-commander/internal/executor" "github.com/wjyhk/vps-commander/internal/executor"
"github.com/wjyhk/vps-commander/internal/storage" "github.com/wjyhk/vps-commander/internal/storage"
@@ -92,6 +93,38 @@ func TestSecurityEffectiveModeAndExecGate(t *testing.T) {
} }
} }
func TestProcessSessionLocalSnakeCaseAndLifecycle(t *testing.T) {
dir := t.TempDir()
store, err := storage.Open(filepath.Join(dir, "db.sqlite"))
if err != nil {
t.Fatal(err)
}
defer store.DB.Close()
if err := store.SetGlobalSecurityMode("high"); err != nil {
t.Fatal(err)
}
s := &Server{Exec: executor.Local{MaxOutput: 1024 * 1024}, Store: store, LocalName: "local", LocalSessions: executor.NewSessionManager(0)}
if err := s.LoadSecurityPolicySnapshot(); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodPost, "/api/v1/process/session?action=start", strings.NewReader(`{"device":"local","session_id":"session-test-001","command":"sleep 0.2; printf local-ok"}`))
w := httptest.NewRecorder()
s.processSession(w, req)
if w.Code != http.StatusOK {
t.Fatalf("start status=%d body=%s", w.Code, w.Body.String())
}
time.Sleep(400 * time.Millisecond)
req = httptest.NewRequest(http.MethodPost, "/api/v1/process/session?action=read", strings.NewReader(`{"device":"local","session_id":"session-test-001"}`))
w = httptest.NewRecorder()
s.processSession(w, req)
if w.Code != http.StatusOK {
t.Fatalf("read status=%d body=%s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), "local-ok") {
t.Fatalf("missing session output: %s", w.Body.String())
}
}
func TestEditBlockSecurityAndReplacement(t *testing.T) { func TestEditBlockSecurityAndReplacement(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
store, err := storage.Open(filepath.Join(dir, "db.sqlite")) store, err := storage.Open(filepath.Join(dir, "db.sqlite"))