Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): keep health responsive during terminal report writes (#11079)

Danila Katalshov92a4e450fb5c
diff --git a/server/internal/daemon/health_test.go b/server/internal/daemon/health_test.go
index 15c1a40677..05d30a8dc0 100644
--- a/server/internal/daemon/health_test.go
+++ b/server/internal/daemon/health_test.go
@@ -222,6 +222,53 @@ func TestHealthHandlerReportsTerminalReportQueueCountsAndBytes(t *testing.T) {
}
}
+func TestHealthHandlerRespondsWhileTerminalReportWriteHoldsQueue(t *testing.T) {
+ d := New(Config{
+ WorkspacesRoot: t.TempDir(),
+ ServerBaseURL: "https://api.example.test",
+ DaemonID: "health-terminal-report-lock",
+ }, slog.New(slog.NewTextHandler(io.Discard, nil)))
+ d.ready.Store(true)
+ if err := d.terminalReports.enqueue(terminalTaskReport{
+ kind: terminalTaskReportComplete, taskID: "pending",
+ }); err != nil {
+ t.Fatalf("enqueue pending report: %v", err)
+ }
+
+ handler := d.healthHandler(time.Now())
+ probe := func() *httptest.ResponseRecorder {
+ rec := httptest.NewRecorder()
+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/health", nil))
+ return rec
+ }
+ want := probe()
+
+ // Queue writes hold this lock across file and directory syncs.
+ d.terminalReports.mu.Lock()
+ result := make(chan *httptest.ResponseRecorder, 1)
+ go func() { result <- probe() }()
+ select {
+ case rec := <-result:
+ d.terminalReports.mu.Unlock()
+ var resp, previous HealthResponse
+ if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
+ t.Fatalf("decode health response: %v", err)
+ }
+ if err := json.Unmarshal(want.Body.Bytes(), &previous); err != nil {
+ t.Fatalf("decode previous health response: %v", err)
+ }
+ if rec.Code != http.StatusOK || resp.Status != "running" ||
+ resp.PendingTerminalReportCount != 1 || resp.PendingTerminalReportBytes != previous.PendingTerminalReportBytes {
+ t.Fatalf("health during queue write = HTTP %d %s, pending %d/%d",
+ rec.Code, resp.Status, resp.PendingTerminalReportCount, resp.PendingTerminalReportBytes)
+ }
+ case <-time.After(time.Second):
+ d.terminalReports.mu.Unlock()
+ <-result
+ t.Fatal("health waited for the terminal report queue write")
+ }
+}
+
// TestHealthHandlerReportsDeferredReload covers the "while waiting to restart,
// the reason and state are visible" criterion. When trySelfReload has confirmed
// a multica version change but the daemon was busy at the barrier check, the
diff --git a/server/internal/daemon/terminal_report_queue.go b/server/internal/daemon/terminal_report_queue.go
index 3ed234839d..b548e6b3ff 100644
--- a/server/internal/daemon/terminal_report_queue.go
+++ b/server/internal/daemon/terminal_report_queue.go
@@ -13,6 +13,7 @@ import (
"sort"
"strings"
"sync"
+ "sync/atomic"
"time"
)
@@ -89,6 +90,7 @@ type terminalReportStore struct {
namespace string
dir string
mu sync.Mutex
+ lastStats atomic.Pointer[terminalReportStoreStats]
}
func newTerminalReportStore(cfg Config) *terminalReportStore {
@@ -567,7 +569,14 @@ func (s *terminalReportStore) stats() (terminalReportStoreStats, error) {
if s == nil {
return terminalReportStoreStats{}, nil
}
- s.mu.Lock()
+ // A queue write can hold the lock while it flushes to disk. Health probes
+ // can use the last successful sample while that work is in progress.
+ if !s.mu.TryLock() {
+ if previous := s.lastStats.Load(); previous != nil {
+ return *previous, nil
+ }
+ return terminalReportStoreStats{}, nil
+ }
defer s.mu.Unlock()
var stats terminalReportStoreStats
var errs []error
@@ -581,7 +590,11 @@ func (s *terminalReportStore) stats() (terminalReportStoreStats, error) {
} else {
stats.FailedCount, stats.FailedBytes = count, bytes
}
- return stats, errors.Join(errs...)
+ if err := errors.Join(errs...); err != nil {
+ return stats, err
+ }
+ s.lastStats.Store(&stats)
+ return stats, nil
}
// otherNamespaceStats surfaces records owned by a different server/profile/