carlos / platform Public

activator tests: wait for the wake's publication (#1252)

TestAWorkerFallbackPublishesOnlyWhenReleased assumed the wake's v1 was
published by the time WakeWorker returned. It is not: WakeWorker returns
when e.ready closes, and the wake goroutine publishes after afterWake. A
replacement that installs first makes v1 out of date, so v1 is dropped
and only v2 is recorded, which is the CI failure.

This is the test's assumption, not a production ordering bug.
publishWorkerServing checks the incarnation and delivers under servingMu,
and the respawn raises workerInc before it publishes, so a late v1 is
either delivered before v2 or dropped. servingWriter keeps that order
for the registry write.

Both tests that replace or respawn straight after a wake now wait for
the wake's publication. TestALateWakePublicationNeverOverwritesItsReplacement
pins the real interleaving: it holds servingMu across the wake and B's
install, and fails with the incarnation check removed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev ff7fffc3a515854205fc624b8d4e9891d84e3faf parent 5048247
1 file changed, +61 −0
  • internal/activator/workerswap_test.go +61 −0
diff --git a/internal/activator/workerswap_test.go b/internal/activator/workerswap_test.go
index d6a3a8332..8d7caa677 100644
--- a/internal/activator/workerswap_test.go
+++ b/internal/activator/workerswap_test.go
@@ -583,6 +583,9 @@ func TestAnOlderWorkerPublicationIsDropped(t *testing.T) {
if err := a.WakeWorker(alwaysOnWorkerRoute(host)); err != nil {
t.Fatal(err)
}
+ // WakeWorker returns when e.ready closes, before the wake publishes. A
+ // respawn that got in first would rightly drop that publication.
+ waitFor(t, "the wake's publication", func() bool { mu.Lock(); defer mu.Unlock(); return len(got) == 1 })
a.mu.Lock()
e := a.entries[host]
firstID, firstInc := e.workerServing, e.workerInc
@@ -598,6 +601,53 @@ func TestAnOlderWorkerPublicationIsDropped(t *testing.T) {
}
}
+// Spec §7.9 (#1252): the wake publishes after WakeWorker returns, so a
+// replacement can install its child first. Holding servingMu keeps the wake's
+// v1 undelivered until B's v2 is installed, the interleaving CI hit. However
+// the two then race for the lock, v1 is out of date and only v2 is delivered,
+// so a late wake can never overwrite the replacement's serving identity.
+func TestALateWakePublicationNeverOverwritesItsReplacement(t *testing.T) {
+ shrinkFirstWake(t, 55*time.Second, 25*time.Second, 50*time.Millisecond)
+ a, _, _, _, _ := stagedActivator(t)
+ var mu sync.Mutex
+ var got []string
+ a.OnWorkerServing = func(_ string, id release.StagedIdentity) {
+ mu.Lock()
+ got = append(got, id.Version)
+ mu.Unlock()
+ }
+ host := "bot.x.internal"
+ a.servingMu.Lock()
+ locked := true
+ defer func() {
+ if locked {
+ a.servingMu.Unlock()
+ }
+ }()
+ if err := a.WakeWorker(alwaysOnWorkerRoute(host)); err != nil {
+ t.Fatal(err)
+ }
+ a.mu.Lock()
+ e, inc0 := a.entries[host], a.entries[host].workerInc
+ a.mu.Unlock()
+ results := make(chan SwapResult, 1)
+ go func() {
+ results <- a.ReplaceWorker(context.Background(), workerAttempt(CauseRelease, ModeOverlap, fakeIdentity("v2")), nil, one)
+ }()
+ waitFor(t, "B's install", func() bool { a.mu.Lock(); defer a.mu.Unlock(); return e.workerInc > inc0 })
+ a.servingMu.Unlock()
+ locked = false
+ if res := <-results; res.Outcome != SwapDone {
+ t.Fatalf("ReplaceWorker = %+v", res)
+ }
+ waitBackground(t, a) // the wake's goroutine, and with it its publication, has finished
+ mu.Lock()
+ defer mu.Unlock()
+ if !slices.Equal(got, []string{"v2"}) {
+ t.Fatalf("published %v, want only the replacement's v2", got)
+ }
+}
+
// Spec §7.9 (P5 review finding 8): a child exec'd from an external live-link
// target has no captured version. Its publication still goes out, empty,
// so the row stops naming the build the previous child ran. Skipping empty
@@ -693,11 +743,15 @@ func TestAWorkerFallbackPublishesOnlyWhenReleased(t *testing.T) {
var mu sync.Mutex
var got []string
block := make(chan struct{})
+ wakePublished := make(chan struct{})
a.OnWorkerServing = func(_ string, id release.StagedIdentity) {
mu.Lock()
got = append(got, id.Version)
n := len(got)
mu.Unlock()
+ if n == 1 {
+ close(wakePublished)
+ }
if n > 2 {
<-block // a slow registry write for the fallback's identity (the wake's v1, then B's v2, come first)
}
@@ -706,6 +760,13 @@ func TestAWorkerFallbackPublishesOnlyWhenReleased(t *testing.T) {
if err := a.WakeWorker(alwaysOnWorkerRoute(host)); err != nil {
t.Fatal(err)
}
+ // WakeWorker returns when e.ready closes, and the wake publishes v1 after
+ // that. Replacing the worker first would drop v1 as out of date (#1252).
+ select {
+ case <-wakePublished:
+ case <-time.After(5 * time.Second):
+ t.Fatal("the wake never published its identity")
+ }
good := fakeIdentity("v1")
go func() {
waitForWorkerStarts(t, b, 2)