فهرست منبع

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

James Read 2 روز پیش
والد
کامیت
3532f87f84
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
+}