From 5cb01e2c4bf293660779aabb2dbe2f1934cd794c Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 26 Nov 2026 04:08:11 +0000 Subject: [PATCH] fix(shared-state): restart the loop when an in-flight request ends A request that outlived the idle window ended after the loop had stopped. Its finish delta then stayed queued with no loop to send it. The end function now touches the loop. --- shared_state.go | 6 +++++- shared_state_review_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/shared_state.go b/shared_state.go index 26bb2e7..fa72ae9 100644 --- a/shared_state.go +++ b/shared_state.go @@ -668,7 +668,11 @@ func (s *sharedInflight) begin(provider string) func() { s.add(provider, epoch, "s") var once sync.Once return func() { - once.Do(func() { s.add(provider, epoch, "f") }) + once.Do(func() { + s.add(provider, epoch, "f") + // An idle-stopped loop must restart, or this delta is never flushed. + s.loop.touch() + }) } } diff --git a/shared_state_review_test.go b/shared_state_review_test.go index c8bb712..91129c9 100644 --- a/shared_state_review_test.go +++ b/shared_state_review_test.go @@ -257,3 +257,40 @@ func TestSharedInflightNewDeltasSurviveAfterAmbiguousDrop(t *testing.T) { t.Errorf("started total = %d, want 1 (the dropped batch is gone, the new delta lands once)", got) } } + +func waitLoop(t *testing.T, l *sharedStateLoop, running bool) { + t.Helper() + deadline := time.Now().Add(2 * time.Second) + for time.Now().Before(deadline) { + l.mu.Lock() + got := l.running + l.mu.Unlock() + if got == running { + return + } + time.Sleep(time.Millisecond) + } + t.Fatalf("loop running never became %v", running) +} + +func TestSharedInflightEndRestartsIdleStoppedLoop(t *testing.T) { + store, clk := sharedTestEnv() + l := newSharedStateLoop(store, nil, nil) + l.nowFn = clk.now + l.every = 5 * time.Millisecond + in := newSharedInflight(l, []string{"p"}, 30*time.Minute) + end := in.begin("p") + waitLoop(t, l, true) + clk.advance(sharedStateIdleStop + time.Second) + waitLoop(t, l, false) + end() + waitLoop(t, l, true) + deadline := time.Now().Add(2 * time.Second) + for store.writesTo(":f") == 0 && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + } + if store.writesTo(":f") != 1 { + t.Error("finish delta stranded after the loop idle-stopped") + } + l.close() +}