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.
This commit is contained in:
2026-11-26 04:09:07 +00:00
parent 6438a95a76
commit 5cb01e2c4b
2 changed files with 42 additions and 1 deletions
+5 -1
View File
@@ -668,7 +668,11 @@ func (s *sharedInflight) begin(provider string) func() {
s.add(provider, epoch, "s") s.add(provider, epoch, "s")
var once sync.Once var once sync.Once
return func() { 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()
})
} }
} }
+37
View File
@@ -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) 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()
}