From e323f9b1f2ddd6db3b8d7697e821cb94c87b95eb Mon Sep 17 00:00:00 2001 From: Vitalii Valkov Date: Thu, 5 Nov 2026 17:12:46 +0100 Subject: [PATCH 1/2] Read the inbox when the notification stream reopens A message published while the stream was down was announced to nobody, and nothing read the inbox until the next message came. The first stream has nothing before it: the daemon's initial read covers that. --- internal/subscriber/subscriber.go | 20 +++++++++--- internal/subscriber/subscriber_test.go | 42 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/internal/subscriber/subscriber.go b/internal/subscriber/subscriber.go index 2c9eca6..456b08a 100644 --- a/internal/subscriber/subscriber.go +++ b/internal/subscriber/subscriber.go @@ -39,6 +39,7 @@ func New(client notificationsClient, threadID string) *Subscriber { func (s *Subscriber) Run(ctx context.Context) error { s.startOnce.Do(func() { close(s.started) }) backoff := time.Second + reopened := false for { if ctx.Err() != nil { return ctx.Err() @@ -61,6 +62,13 @@ func (s *Subscriber) Run(ctx context.Context) error { } s.readyOnce.Do(func() { close(s.ready) }) backoff = time.Second + // Whatever was published while the stream was down was announced to + // nobody. The first stream has nothing before it: the daemon's initial + // read covers that. + if reopened { + s.signalWake() + } + reopened = true for { resp, err := stream.Recv() @@ -101,14 +109,18 @@ func (s *Subscriber) Run(ctx context.Context) error { if s.threadID != "" && (!ok || payloadThreadID != s.threadID) { continue } - select { - case s.wake <- struct{}{}: - default: - } + s.signalWake() } } } +func (s *Subscriber) signalWake() { + select { + case s.wake <- struct{}{}: + default: + } +} + // waitOut sleeps out a failure's backoff under the attempt's context and // returns the backoff the next failure gets: doubled, or back at the start when // Resubscribe cut the wait short, because a wake is not another failure. The diff --git a/internal/subscriber/subscriber_test.go b/internal/subscriber/subscriber_test.go index f57bfb2..855271f 100644 --- a/internal/subscriber/subscriber_test.go +++ b/internal/subscriber/subscriber_test.go @@ -278,6 +278,13 @@ func TestSubscriberResubscribeReopensStream(t *testing.T) { t.Fatal("expected the first stream to be dropped") } + // The reopening itself asks for an inbox read; after that the stream + // carries messages as the first one did. + select { + case <-sub.Wake(): + case <-time.After(200 * time.Millisecond): + t.Fatal("expected a wake for the reopening") + } streams[1].responses <- makeMessageCreated("1f5a4e5c-350a-4f3a-8dda-0ef049b89d09") select { case <-sub.Wake(): @@ -289,6 +296,41 @@ func TestSubscriberResubscribeReopensStream(t *testing.T) { waitForRun(t, done) } +// A message published while the stream was down was announced to nobody, so a +// stream that reopens asks for an inbox read. The first stream has nothing +// before it and does not. +func TestSubscriberReopenedStreamAsksForAnInboxRead(t *testing.T) { + client := &wakeNotifications{} + sub := New(client, "") + + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { + done <- sub.Run(ctx) + }() + + select { + case <-sub.Ready(): + case <-time.After(200 * time.Millisecond): + t.Fatal("expected the first stream") + } + select { + case <-sub.Wake(): + t.Fatal("expected no wake for the first stream") + case <-time.After(50 * time.Millisecond): + } + + sub.Resubscribe() + select { + case <-sub.Wake(): + case <-time.After(500 * time.Millisecond): + t.Fatal("expected a wake for the reopened stream") + } + + cancel() + waitForRun(t, done) +} + // A wake can land while the subscriber is waiting out a failure. The wait is // for the failure, and the wake is not one: Resubscribe cuts it short and the // stream is opened at once. From 369068a9ab486d322fa50c1d91ae288cb590205d Mon Sep 17 00:00:00 2001 From: Vitalii Valkov Date: Thu, 5 Nov 2026 17:12:47 +0100 Subject: [PATCH 2/2] Tell a wake from its repeats by the sleep between them A wake within 20 seconds of the last was taken for a repeat of it, measured on the wall clock. A fast turn gets there: the workload wakes, answers and hibernates in seconds, and the next message wakes it again inside the window. That wake was dropped, so nothing read the inbox and the message sat unread until another one arrived. Every wake has a sleep behind it, so that is the test now: the signs of one wake have none between them however far apart they come, and two wakes have one however close. The keepalive's clock check uses the same floor, so a sleep shorter than a tick is seen too when both signals go missing. --- internal/daemon/keepalive.go | 2 +- internal/daemon/wake.go | 30 +++++++++++++++------------ internal/daemon/wake_test.go | 39 +++++++++++++++++++++++++++++------- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/internal/daemon/keepalive.go b/internal/daemon/keepalive.go index a09f80f..06ff9a7 100644 --- a/internal/daemon/keepalive.go +++ b/internal/daemon/keepalive.go @@ -76,7 +76,7 @@ func (d *Daemon) runKeepalive(ctx context.Context, wake <-chan os.Signal) { case <-wake: woken("signal") case <-ticker.C: - if jump := watch.tick(); jump > interval { + if jump := watch.tick(); jump >= wakeSleepFloor { log.Printf("wall clock moved %s further than monotonic time between ticks", jump) woken("clock") } diff --git a/internal/daemon/wake.go b/internal/daemon/wake.go index 8e44cf1..a261610 100644 --- a/internal/daemon/wake.go +++ b/internal/daemon/wake.go @@ -13,14 +13,11 @@ import "time" // length of the sleep. The signal makes the wake prompt; the clock makes it // certain. -// wakeRepeatWindow is how long after a handled wake another sign of one counts -// as a repeat of it rather than a wake of its own. The runner's second signal -// follows its first by a few seconds and the clock is read once a tick; two -// ticks cover both with room for a late tick. It is measured on the wall -// clock, which unlike the monotonic one moves across a sleep, so two wakes -// with a sleep between them are never taken for one however close together -// they are in running time. -const wakeRepeatWindow = 2 * workloadKeepaliveInterval +// wakeSleepFloor is how far the wall clock has to get ahead of the monotonic one +// to mean a sleep. It clears a clock's ordinary corrections and stays under the +// shortest sleep there is: freezing a guest, snapshotting it and restoring it +// take seconds. +const wakeSleepFloor = time.Second // clockReading is one look at both clocks: the wall clock, which the runner // sets, and the monotonic clock, which sleeps through a pause. @@ -45,7 +42,8 @@ func readClocks() clockReading { type wakeWatch struct { clocks func() clockReading last clockReading - handled time.Time + handled clockReading + woken bool } func newWakeWatch(clocks func() clockReading) *wakeWatch { @@ -66,14 +64,20 @@ func (w *wakeWatch) tick() time.Duration { } // admit reports whether a sign of a wake is a wake of its own rather than a -// repeat of the last one handled, and counts it as handled when it is. +// repeat of the last one handled, and counts it as handled when it is. Every +// wake has a sleep behind it, and that is the only thing that tells them apart: +// the signs of one wake have none between them however far apart they come, and +// two wakes have one however close together, since a turn can start, end and +// hibernate again inside a few seconds. A wall clock stepped back says nothing +// about a sleep, and an extra inbox read costs little, so it counts as a wake. func (w *wakeWatch) admit() bool { now := w.clocks() - if !w.handled.IsZero() { - if since := now.wall.Sub(w.handled); since >= 0 && since < wakeRepeatWindow { + if w.woken { + apart := now.wall.Sub(w.handled.wall) - (now.mono - w.handled.mono) + if apart.Abs() < wakeSleepFloor { return false } } - w.handled = now.wall + w.woken, w.handled = true, now return true } diff --git a/internal/daemon/wake_test.go b/internal/daemon/wake_test.go index 173fa22..2a8eb6c 100644 --- a/internal/daemon/wake_test.go +++ b/internal/daemon/wake_test.go @@ -61,10 +61,7 @@ func TestWakeWatchAdmitFoldsRepeatsOfAWake(t *testing.T) { clockReading{wall: wallAt(0), mono: 0}, clockReading{wall: wallAt(time.Hour), mono: time.Second}, clockReading{wall: wallAt(time.Hour + 3*time.Second), mono: 4 * time.Second}, - clockReading{wall: wallAt(time.Hour + wakeRepeatWindow - time.Millisecond), mono: 5 * time.Second}, - clockReading{wall: wallAt(time.Hour + wakeRepeatWindow), mono: 6 * time.Second}, - clockReading{wall: wallAt(time.Hour + wakeRepeatWindow + time.Second), mono: 7 * time.Second}, - clockReading{wall: wallAt(time.Minute), mono: 8 * time.Second}, + clockReading{wall: wallAt(time.Hour + time.Minute), mono: 61 * time.Second}, )) if !watch.admit() { t.Fatal("expected the first wake admitted") @@ -73,14 +70,41 @@ func TestWakeWatchAdmitFoldsRepeatsOfAWake(t *testing.T) { t.Fatal("expected the signal's repeat folded into the wake") } if watch.admit() { - t.Fatal("expected a repeat at the edge of the window folded into the wake") + t.Fatal("expected a sign with no sleep behind it folded into the wake, however late") } +} + +// A turn can start, end and hibernate again in seconds, so the next wake can +// follow closely on the wall clock. The sleep between them is what makes it a +// wake of its own, and missing it would leave the message that caused it unread. +func TestWakeWatchAdmitsAWakeSoonAfterTheLast(t *testing.T) { + watch := newWakeWatch(scriptedClocks( + clockReading{wall: wallAt(0), mono: 0}, + clockReading{wall: wallAt(time.Hour), mono: time.Second}, + // Five seconds of running, then a sleep of five. + clockReading{wall: wallAt(time.Hour + 10*time.Second), mono: 6 * time.Second}, + clockReading{wall: wallAt(time.Hour + 13*time.Second), mono: 9 * time.Second}, + )) if !watch.admit() { - t.Fatal("expected a wake at the end of the window admitted") + t.Fatal("expected the first wake admitted") + } + if !watch.admit() { + t.Fatal("expected a wake ten seconds after the last admitted, a sleep lying between them") } if watch.admit() { t.Fatal("expected a repeat of the second wake folded into it") } +} + +func TestWakeWatchAdmitsAWakeBehindAClockSteppedBack(t *testing.T) { + watch := newWakeWatch(scriptedClocks( + clockReading{wall: wallAt(0), mono: 0}, + clockReading{wall: wallAt(time.Hour), mono: time.Second}, + clockReading{wall: wallAt(time.Minute), mono: 2 * time.Second}, + )) + if !watch.admit() { + t.Fatal("expected the first wake admitted") + } if !watch.admit() { t.Fatal("expected a wake behind a wall clock stepped back admitted") } @@ -325,7 +349,8 @@ func TestRunClockJumpWakes(t *testing.T) { t.Fatalf("expected one resubscribe for the jump, got %d", got) } - clocks.sleep(time.Hour) + // A sleep shorter than a tick is a sleep all the same. + clocks.sleep(wakeSleepFloor) waitForSyncCalls(t, errCh, consumer.Calls, 3, "the clock jumped again") if got := subscriber.resubscribed.Load(); got != 2 { t.Fatalf("expected a second resubscribe for a second sleep, got %d", got)