ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
Release the circuit-breaker queue lock around the database write.
protonmatterb2a2f95e8c25
diff --git a/llmpool/health.go b/llmpool/health.goindex b79b26b..7f3e301 100644--- a/llmpool/health.go+++ b/llmpool/health.go@@ -48,7 +48,7 @@ type Registry struct {// persist / forget mirror state to the DB so a cooling-off window survives a// restart. Both may be nil (no DB). Both run while r.mu is held, so a later// change to the same profile cannot enqueue ahead of this one. They must not- // call back into the registry.+ // call back into the registry or wait on database I/O.persist func(id int64, st State)forget func(id int64)diff --git a/server/llmpool.go b/server/llmpool.goindex 087723a..bbb1d1c 100644--- a/server/llmpool.go+++ b/server/llmpool.go@@ -59,13 +59,16 @@ type healthOp struct {// healthSync writes circuit-breaker state from one goroutine. A newer update for// the same profile replaces one that has not been written yet, so a burst of-// state changes becomes one database call and the request path does not wait.-// save and clear must not call enqueue.+// state changes becomes one database call. The database call runs without mu,+// so enqueue does not wait on a slow write. writeMu keeps those calls in order.+// Before each call, an op that is already older than the applied or pending+// sequence is dropped. save and clear must not call enqueue.type healthSync struct {save func(db.LLMHealth) errorclear func(int64) errorretryAfter time.Duration // tests set a short delay; zero uses one secondmu sync.Mutex+ writeMu sync.Mutex // orders database calls; enqueue does not take itpending map[int64]healthOpapplied map[int64]uint64seq uint64@@ -150,10 +153,22 @@ func (h *healthSync) restore(id int64, op healthOp) {h.pending[id] = op}-func (h *healthSync) apply(id int64, op healthOp) error {+// superseded reports whether a newer op for id is already applied or waiting.+// The caller holds writeMu, so the decision and the database call stay in order.+func (h *healthSync) superseded(id int64, seq uint64) bool {h.mu.Lock()defer h.mu.Unlock()- if op.seq > 0 && h.applied[id] >= op.seq {+ if seq > 0 && h.applied[id] >= seq {+ return true+ }+ cur, ok := h.pending[id]+ return ok && cur.seq > seq+}++func (h *healthSync) apply(id int64, op healthOp) error {+ h.writeMu.Lock()+ defer h.writeMu.Unlock()+ if h.superseded(id, op.seq) {return nil}var err error@@ -172,9 +187,11 @@ func (h *healthSync) apply(id int64, op healthOp) error {if err != nil {return err}+ h.mu.Lock()if h.applied[id] < op.seq {h.applied[id] = op.seq}+ h.mu.Unlock()return nil}diff --git a/server/llmpool_health_test.go b/server/llmpool_health_test.goindex 9d14b63..c9e7a1e 100644--- a/server/llmpool_health_test.go+++ b/server/llmpool_health_test.go@@ -108,3 +108,76 @@ func TestHealthSyncRetriesFailedSave(t *testing.T) {t.Fatalf("save calls=%d, want a retry", calls)}}++func TestHealthSyncEnqueueDoesNotWaitOnSave(t *testing.T) {+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()++ started := make(chan struct{})+ release := make(chan struct{})+ later := make(chan struct{})+ h := newHealthSync(ctx,+ func(row db.LLMHealth) error {+ if row.Fails == 1 {+ close(started)+ <-release+ return nil+ }+ if row.Fails == 9 {+ close(later)+ }+ return nil+ },+ func(int64) error { return nil },+ )+ h.enqueue(4, healthOp{st: llmpool.State{Fails: 1}})+ select {+ case <-started:+ case <-time.After(2 * time.Second):+ t.Fatal("save did not start")+ }+ done := make(chan struct{})+ go func() {+ h.enqueue(4, healthOp{st: llmpool.State{Fails: 9, LastError: "later"}})+ close(done)+ }()+ select {+ case <-done:+ case <-time.After(200 * time.Millisecond):+ t.Fatal("enqueue blocked while a save was in flight")+ }+ close(release)+ select {+ case <-later:+ case <-time.After(2 * time.Second):+ t.Fatal("the later state was not written")+ }+}++func TestHealthSyncDropsStaleWrite(t *testing.T) {+ ctx, cancel := context.WithCancel(context.Background())+ defer cancel()++ writes := 0+ h := newHealthSync(ctx,+ func(db.LLMHealth) error {+ writes+++ return nil+ },+ func(int64) error { return nil },+ )+ h.mu.Lock()+ h.seq = 4+ h.applied[5] = 2+ h.pending[6] = healthOp{seq: 4, clear: true}+ h.mu.Unlock()+ if err := h.apply(5, healthOp{seq: 1, st: llmpool.State{Fails: 1}}); err != nil {+ t.Fatal(err)+ }+ if err := h.apply(6, healthOp{seq: 3, st: llmpool.State{Fails: 3}}); err != nil {+ t.Fatal(err)+ }+ if writes != 0 {+ t.Fatalf("stale writes=%d", writes)+ }+}