edge: first held-stream heartbeat at one tick, not two (#432)
Human
Prompt
fix the held stream's first ": ping" arriving at +2 ticks on dev.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
the tick case judged due-ness at the timer's fire time, a few microseconds before the commit's lastWrite plus Tick, so it skipped the ping and re-armed a whole Tick. It now judges against time.Now(), writes the ping when due and re-arms a full Tick from that write. The reopen backstop still runs every tick; no ping inside a passed-through answer. Test TestHeldFirstPingAtOneTick: first ping before 1.5 ticks, second about one tick later (RED at 402ms with Tick 200ms before the fix).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+51
−3
internal/edge/heldstream.go+8 −3internal/edge/heldstream_test.go+43 −0
diff --git a/internal/edge/heldstream.go b/internal/edge/heldstream.go| index 68c14487e..bccc48e3f 100644 |
| --- a/internal/edge/heldstream.go |
| +++ b/internal/edge/heldstream.go |
| @@ -510,19 +510,24 @@ func (t *heldTask) run() heldCloseReason { |
| if t.holding() { |
| t.scheduleReopen(true, 0) |
| } |
| - case now := <-tickT.C: |
| + case <-tickT.C: |
| + // Due-ness is judged against the clock now, not the timer's |
| + // fire time: a write that landed just after the timer started |
| + // (the commit) must not hide a heartbeat that is due. |
| + // |
| // No heartbeat inside a passed-through answer: its body goes to |
| // the browser byte for byte. |
| - if t.committed && !t.passThrough && now.Sub(t.lastWrite) >= t.cfg.Tick { |
| + next := time.Until(t.lastWrite.Add(t.cfg.Tick)) |
| + if next <= 0 && t.committed && !t.passThrough { |
| if err := t.write([]byte(": ping\n")); err != nil { |
| return heldCloseWriteFailed |
| } |
| + next = time.Until(t.lastWrite.Add(t.cfg.Tick)) |
| } |
| // The backstop for a lost wake signal (§3.7). |
| if t.holding() && t.e.act.HeldAdmissible(t.host) { |
| t.scheduleReopen(true, 0) |
| } |
| - next := time.Until(t.lastWrite.Add(t.cfg.Tick)) |
| if next <= 0 { |
| next = t.cfg.Tick |
| } |
diff --git a/internal/edge/heldstream_test.go b/internal/edge/heldstream_test.go| index 571b757ca..9b575509d 100644 |
| --- a/internal/edge/heldstream_test.go |
| +++ b/internal/edge/heldstream_test.go |
| @@ -229,6 +229,49 @@ func TestHeldAsleepCommitsAndPings(t *testing.T) { |
| } |
| } |
| +// The first heartbeat is due one Tick after the response headers, not two: |
| +// the commit's own write lands just after the tick timer starts, and the |
| +// first tick must not mistake that for a fresh write and skip a whole beat. |
| +func TestHeldFirstPingAtOneTick(t *testing.T) { |
| + cfg := heldTestConfig() |
| + cfg.Tick = 200 * time.Millisecond |
| + h := newHeldHarness(t, cfg, func(int, http.ResponseWriter, *http.Request) { t.Error("proxy called while asleep") }) |
| + h.act.asleep.Store(true) |
| + h.start(t) |
| + resp := h.get(t, "/live?carlos_hold=1", nil) |
| + start := time.Now() |
| + pings := make(chan time.Time, 4) |
| + go func() { |
| + br := bufio.NewReader(resp.Body) |
| + for { |
| + line, err := br.ReadString('\n') |
| + if err != nil { |
| + return |
| + } |
| + if line == ": ping\n" { |
| + pings <- time.Now() |
| + } |
| + } |
| + }() |
| + var first, second time.Time |
| + select { |
| + case first = <-pings: |
| + case <-time.After(4 * cfg.Tick): |
| + t.Fatal("no heartbeat in 4 ticks") |
| + } |
| + if d := first.Sub(start); d >= cfg.Tick*3/2 { |
| + t.Fatalf("first ping at %v, want before %v", d, cfg.Tick*3/2) |
| + } |
| + select { |
| + case second = <-pings: |
| + case <-time.After(4 * cfg.Tick): |
| + t.Fatal("no second heartbeat in 4 ticks") |
| + } |
| + if d := second.Sub(first); d < cfg.Tick/2 || d >= cfg.Tick*3/2 { |
| + t.Fatalf("second ping %v after the first, want about %v", d, cfg.Tick) |
| + } |
| +} |
| + |
| func TestHeldSeedsCursor(t *testing.T) { |
| h := newHeldHarness(t, heldTestConfig(), func(int, http.ResponseWriter, *http.Request) {}) |
| h.act.asleep.Store(true) |