diff --git a/go.mod b/go.mod index 1d23ba4..596b33f 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.25.0 require ( github.com/creack/pty v1.1.24 github.com/gin-gonic/gin v1.12.0 + github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 gopkg.in/yaml.v3 v3.0.1 ) @@ -21,7 +22,6 @@ require ( github.com/go-playground/validator/v10 v10.30.3 // indirect github.com/goccy/go-json v0.10.6 // indirect github.com/goccy/go-yaml v1.19.2 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect diff --git a/internal/process/manager.go b/internal/process/manager.go index 92c4da7..cd91f85 100644 --- a/internal/process/manager.go +++ b/internal/process/manager.go @@ -32,6 +32,8 @@ type LocalManager struct { args []string mu sync.Mutex sessions map[string]*Session + exitedMu sync.Mutex + exited map[string]struct{} } // NewManager creates a LocalManager with the given command. @@ -41,6 +43,7 @@ func NewManager(command string, args []string) *LocalManager { command: normalizeCommand(command), args: args, sessions: make(map[string]*Session), + exited: make(map[string]struct{}), } } @@ -50,8 +53,11 @@ func (m *LocalManager) Start(workspaceID string, workspaceRoot string) error { m.mu.Lock() defer m.mu.Unlock() - if sess, ok := m.sessions[workspaceID]; ok && sess.Cmd.Process != nil { - if sess.Cmd.ProcessState == nil { + if _, ok := m.sessions[workspaceID]; ok { + m.exitedMu.Lock() + _, exited := m.exited[workspaceID] + m.exitedMu.Unlock() + if !exited { return util.New(util.CodeConflict, "process already running") } } @@ -96,6 +102,10 @@ func (m *LocalManager) Start(workspaceID string, workspaceRoot string) error { } m.sessions[workspaceID] = sess + m.exitedMu.Lock() + delete(m.exited, workspaceID) + m.exitedMu.Unlock() + outputDone := make(chan struct{}) go m.captureOutput(sess, stdoutR, outputDone) go m.waitExit(sess, outputDone) @@ -138,6 +148,10 @@ func (m *LocalManager) waitExit(sess *Session, outputDone <-chan struct{}) { _ = sess.Cmd.Wait() <-outputDone + m.exitedMu.Lock() + m.exited[sess.WorkspaceID] = struct{}{} + m.exitedMu.Unlock() + sess.mu.Lock() defer sess.mu.Unlock() @@ -173,17 +187,26 @@ func (m *LocalManager) Stop(workspaceID string) error { } delete(m.sessions, workspaceID) + m.exitedMu.Lock() + delete(m.exited, workspaceID) + m.exitedMu.Unlock() return nil } // Restart stops (if running) then starts the process. func (m *LocalManager) Restart(workspaceID string, workspaceRoot string) error { m.mu.Lock() - if sess, ok := m.sessions[workspaceID]; ok && sess.Cmd.Process != nil { - if sess.Cmd.ProcessState == nil { + if sess, ok := m.sessions[workspaceID]; ok { + m.exitedMu.Lock() + _, exited := m.exited[workspaceID] + m.exitedMu.Unlock() + if !exited && sess.Cmd.Process != nil { _ = sess.Cmd.Process.Kill() - delete(m.sessions, workspaceID) } + delete(m.sessions, workspaceID) + m.exitedMu.Lock() + delete(m.exited, workspaceID) + m.exitedMu.Unlock() } m.mu.Unlock() @@ -193,14 +216,17 @@ func (m *LocalManager) Restart(workspaceID string, workspaceRoot string) error { // Status returns the current process status for the workspace. func (m *LocalManager) Status(workspaceID string) Status { m.mu.Lock() - defer m.mu.Unlock() - sess, ok := m.sessions[workspaceID] + m.mu.Unlock() + if !ok || sess.Cmd.Process == nil { return Status{WorkspaceID: workspaceID, Running: false} } - if sess.Cmd.ProcessState != nil { + m.exitedMu.Lock() + _, exited := m.exited[workspaceID] + m.exitedMu.Unlock() + if exited { return Status{WorkspaceID: workspaceID, Running: false} } @@ -225,10 +251,14 @@ func (m *LocalManager) Subscribe(workspaceID string) (Subscription, error) { sess.mu.Lock() sub := newSubscription(sess) sess.Subscribers[sub] = struct{}{} - if sess.Cmd.ProcessState != nil { + sess.mu.Unlock() + + m.exitedMu.Lock() + _, exited := m.exited[workspaceID] + m.exitedMu.Unlock() + if exited { sub.closeChan() } - sess.mu.Unlock() return sub, nil }