Просмотр исходного кода

fix(executor): return copies of running executions from the queue

GetActiveExecutionsACL returned pointers to live log entries, and the
queue and dashboard handlers read them after the lock was released while
the executor was still writing output to them. It now returns copies, as
the other log accessors already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 2 дней назад
Родитель
Сommit
aeab7faf6c
2 измененных файлов с 45 добавлено и 2 удалено
  1. 3 2
      service/internal/executor/queue.go
  2. 42 0
      service/internal/executor/queue_test.go

+ 3 - 2
service/internal/executor/queue.go

@@ -18,7 +18,8 @@ func isQueueEntryVisible(cfg *config.Config, user *authpublic.AuthenticatedUser,
 	return acl.IsAllowedLogs(cfg, user, entry.Binding.Action)
 }
 
-// GetActiveExecutionsACL returns unfinished executions the user may view in the queue.
+// GetActiveExecutionsACL returns copies of unfinished executions the user may view in the queue.
+// The executor keeps writing to running entries, so callers must not share them.
 func (e *Executor) GetActiveExecutionsACL(cfg *config.Config, user *authpublic.AuthenticatedUser) []*InternalLogEntry {
 	e.logmutex.RLock()
 	defer e.logmutex.RUnlock()
@@ -28,7 +29,7 @@ func (e *Executor) GetActiveExecutionsACL(cfg *config.Config, user *authpublic.A
 	for _, trackingID := range e.logsTrackingIdsByDate {
 		entry := e.logs[trackingID]
 		if isQueueEntryVisible(cfg, user, entry) {
-			active = append(active, entry)
+			active = append(active, cloneInternalLogEntry(entry))
 		}
 	}
 

+ 42 - 0
service/internal/executor/queue_test.go

@@ -70,3 +70,45 @@ func newQueueTestLogEntry(binding *ActionBinding, finished bool) *InternalLogEnt
 	}
 	return entry
 }
+
+func TestGetActiveExecutionsACLReturnsCopies(t *testing.T) {
+	e, cfg := testingExecutor()
+	action := &config.Action{Title: "running", Shell: "sleep 1"}
+	cfg.Actions = append(cfg.Actions, action)
+	cfg.Sanitize()
+	e.RebuildActionMap()
+
+	binding := e.FindBindingWithNoEntity(action)
+	require.NotNil(t, binding)
+
+	running := newQueueTestLogEntry(binding, false)
+	e.SetLog(running.ExecutionTrackingID, running)
+
+	stop := make(chan struct{})
+	writerDone := make(chan struct{})
+	go func() {
+		defer close(writerDone)
+		for {
+			select {
+			case <-stop:
+				return
+			default:
+				e.logmutex.Lock()
+				running.Output += "x"
+				running.ExitCode++
+				e.logmutex.Unlock()
+			}
+		}
+	}()
+
+	user := auth.UserGuest(cfg)
+	for range 200 {
+		for _, entry := range e.GetActiveExecutionsACL(cfg, user) {
+			assert.NotSame(t, running, entry, "callers must get a copy of a running entry")
+			_ = len(entry.Output) + int(entry.ExitCode)
+		}
+	}
+
+	close(stop)
+	<-writerDone
+}