box: an outside stop refuses only a row with copies (#511 R27)
Review round 3 scope correction to 649f0cd0. An outside stop of the app (StoppedOutside) now ends the dial hold, or refuses a request, only for a replica set with more than one copy (edge.ManyCopies: FleetBoxes, Copies above 1, a valid projection), where another box's copy can serve. A single-copy row keeps the old behaviour: it holds up to 13 s for the activator to restart its app and is not refused on an outside stop. The drain (the box going away) still applies to every row. The edge builds an activator-owned row's app socket proxy with the row's ManyCopies; genRoundTripper marks such requests on their context, since the dial hold runs in a pool shared by the host's generation. refuseUnreachedApp and the activator's serveWakeErrorBeforeApp count an outside stop only for such rows. Witnesses: TestSingleCopyDialHoldWaitsForRestart (single-copy row, app stopped outside, a POST in the real dial hold, the app restarted inside it: box-00 serves it; unscoped, the hold ends and box-01 serves it); TestReplicaDialHoldEndsAndRefuses now on a real two-copy box row; TestServeRefusesBeforeAppRead covers one copy and two.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/activator/activator.go+6 −2internal/activator/tunnel_refuse_test.go+23 −10internal/edge/edge.go+1 −1internal/edge/genproxy.go+45 −16internal/edge/replica_drain_e2e_test.go+46 −3internal/edge/tunnelrefuse.go+11 −0
Intent
<task-notification> <task-id>a41c5fc02dcfea0c5</task-id> <tool-use-id>toolu_01DxcAngiHi4m8gPJu3iTbaN</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/72128d15-58b8-452f-9f7e-8e864e8854d2/tasks/a41c5fc02dcfea0c5.output</output-file> <status>completed</status> <summary>Agent "Fix drill-3 hub routing gaps" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>- **Status: DONE_WITH_CONCERNS.** Tip 4008b5c3, not pushed. - **Commits:** - 29ec028f fixes the `TestTwoBoxFleetEndToEnd` CI failure. My round-1 rewrite of step (b) asserted the fall-over to box-a before the hub had read box-b's wire end. The request was written onto the dying connection, so R22 rightly answered 502 and sent it nowhere else. The test now waits for the hub to see the wire end. `fleetProxy` also falls back to another box when the request provably never reached the lost box (unlabelled rows only). - 649f0cd0 is the P1. The app dial hold (`dialGenUntil`, and `waitServingGen`) now ends at once when a fleet stream's drain closes, and within one dial step (100 ms) when its app was stopped from outside. The request is then refused with REFUSED_STREAM if no app byte was written. Also Fable's minor: a stream the hub already reset is no longer kept in the refusal map. - 58764118 is Astra's minor: every stream above a GOAWAY's last-stream-id now counts as refused, whatever the GOAWAY's code. The comment is corrected and the test covers stream 1 and a later stream. - 4008b5c3 re-proposes the census: no unit changed, seal still 52cb3166, `make census-card` clean. - **Witnesses:** through the real `genRoundTripper` and dial path, a POST and a GET waiting in the hold are served by the survivor in under 2 s, triggered by either the drain or an outside stop. With the change switched off, both time out about 9.7 s after the trigger. The negative case holds: once the POST is written to the app, the box answers 502 and nothing is refused or repeated. - **Tests:** `TestTwoBoxFleetEndToEnd` passed 20 runs, and with the R22, R24 and R26 witnesses 3 runs. Targeted edge, tunnelclient, host (including `TestGroupNameClassCensus`), activator and region HA tests, grouproll, the three cross-compiles and the census tests all pass, as does a targeted race run. - **Concerns:** - An outside stop now ends the hold for single-copy rows too, so they get a 503 with Retry-After instead of waiting up to 13 s for a restart. This is as ruled. - The new `fleetProxy` fallback has no deterministic witness of its own; it rests on the same never-reached proof as R22. - The second GOAWAY message is matched by its prefix, so re-run these tests on any x/net or Go bump. - Astra's census review is still owed. - **Report:** /home/paulca/amadan.net/carlos/platform/.claude/worktrees/stateless-replicas-4/.superpowers/sdd/2026-11-01-stateless-replicas-4-deploy-and-drill/task-drill3-report.md</result> <usage><subagent_tokens>732104</subagent_tokens><tool_uses>25</tool_uses><duration_ms>1884836</duration_ms></usage> </task-notification>
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index 5c3dad3d3..5aabc1319 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -6642,7 +6642,8 @@ func (a *Service) serve(w http.ResponseWriter, r *http.Request, rt registry.Rout |
| // serveWakeErrorBeforeApp is serveWakeError for a failure in serve before |
| // its proxy runs, so before any app has read the request (#511 R27). On a |
| // fleet tunnel stream (tunnelclient.Refuse) of a box whose tunnel drain has |
| -// begun (its shutdown has started), or for a host whose app last ended |
| +// begun (its shutdown has started), or, for a route with more than one |
| +// copy (edge.ManyCopies, review round 3), a host whose app last ended |
| // without the activator asking it to (StoppedOutside: systemd's shutdown, |
| // a hand stop, a crash), the stream is refused unprocessed instead of |
| // answered, and the hub sends the request to another copy whatever its |
| @@ -6656,7 +6657,10 @@ func (a *Service) serve(w http.ResponseWriter, r *http.Request, rt registry.Rout |
| // written to the app. serveWakeError's other caller, ServeHTTP's duty |
| // wrapper, may follow a serve that already proxied, so it never refuses. |
| func (a *Service) serveWakeErrorBeforeApp(w http.ResponseWriter, r *http.Request, rt registry.Route, err error) bool { |
| - if !errors.Is(err, context.Canceled) && edge.RefuseBeforeApp(r, a.StoppedOutside(rt.Host)) { |
| + // An outside stop counts only for a route with more than one copy |
| + // (edge.ManyCopies, review round 3): a single-copy row is answered as |
| + // before, unless the box is draining. |
| + if !errors.Is(err, context.Canceled) && edge.RefuseBeforeApp(r, a.StoppedOutside(rt.Host) && edge.ManyCopies(rt)) { |
| return true |
| } |
| return a.serveWakeError(w, r, rt, err) |
diff --git a/internal/activator/tunnel_refuse_test.go b/internal/activator/tunnel_refuse_test.go| index 4b4d6f6bc..a42e144ee 100644 |
| --- a/internal/activator/tunnel_refuse_test.go |
| +++ b/internal/activator/tunnel_refuse_test.go |
| @@ -42,7 +42,15 @@ type refuseBox struct { |
| appReads atomic.Int32 |
| } |
| -func newRefuseBox(t *testing.T) *refuseBox { |
| +// replicaRoute is testRoute as one copy of a two-copy replica set. |
| +func replicaRoute(host string) registry.Route { |
| + rt := testRoute(host) |
| + rt.Region, rt.Stateless = "fleet/pi", true |
| + rt.FleetBoxes, rt.Copies, rt.ReplicaRevision, rt.ReplicaIncarnation = `["box-1","box-2"]`, 2, 1, "life" |
| + return rt |
| +} |
| + |
| +func newRefuseBox(t *testing.T, rt registry.Route) *refuseBox { |
| t.Helper() |
| x := &refuseBox{b: &stoppedOutsideBackend{fakeBackend: &fakeBackend{}}, drain: make(chan struct{}), entered: make(chan struct{}, 4)} |
| reg, err := registry.Open(filepath.Join(t.TempDir(), "registry.db")) |
| @@ -68,7 +76,6 @@ func newRefuseBox(t *testing.T) *refuseBox { |
| conns <- cc |
| })) |
| t.Cleanup(hub.Close) |
| - rt := testRoute("shop.example.com") |
| app := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { x.appReads.Add(1); io.WriteString(w, "app") }) |
| handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| x.entered <- struct{}{} |
| @@ -94,22 +101,28 @@ func newRefuseBox(t *testing.T) *refuseBox { |
| // TestServeRefusesBeforeAppRead (#511 R27) drives the real activator's |
| // serve: a fleet tunnel stream waits in awaitStartup, then the activator |
| // stops, so serve fails before its proxy runs (errStartupStopped, drill 3's |
| -// box-2 answer). Once the box's tunnel drain has begun, or when the app |
| -// last ended outside the activator, the stream is refused with |
| -// REFUSED_STREAM and the app never read it; otherwise, and off a tunnel, |
| -// it is answered 503 as before. |
| +// box-2 answer). Once the box's tunnel drain has begun (any row), or when |
| +// the app last ended outside the activator on a route with more than one |
| +// copy, the stream is refused with REFUSED_STREAM and the app never read |
| +// it; otherwise, a single-copy row stopped outside included (review round |
| +// 3), and off a tunnel, it is answered 503 as before. |
| func TestServeRefusesBeforeAppRead(t *testing.T) { |
| + single := testRoute("shop.example.com") |
| + set := replicaRoute("shop.example.com") |
| for _, c := range []struct { |
| name string |
| + rt registry.Route |
| drain, outside bool |
| wantRefusedCode bool |
| }{ |
| - {"shutdown begun", true, false, true}, |
| - {"app stopped outside the activator", false, true, true}, |
| - {"neither", false, false, false}, |
| + {"shutdown begun, one copy", single, true, false, true}, |
| + {"shutdown begun, two copies", set, true, false, true}, |
| + {"app stopped outside, two copies", set, false, true, true}, |
| + {"app stopped outside, one copy", single, false, true, false}, |
| + {"neither", set, false, false, false}, |
| } { |
| t.Run(c.name, func(t *testing.T) { |
| - x := newRefuseBox(t) |
| + x := newRefuseBox(t, c.rt) |
| x.b.stopped.Store(c.outside) |
| type result struct { |
| resp *http.Response |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index fdfa6367d..30362fe26 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -1224,7 +1224,7 @@ func (e *Edge) Reload() error { |
| // (hib); viaTunnel is false in this arm by construction. |
| var p *httputil.ReverseProxy |
| if e.gens != nil && t.ActivatorOwned() { |
| - p, genManaged = appSocketProxy(t.Host, e.gens, e.genPools), true |
| + p, genManaged = appSocketProxyFor(t.Host, e.gens, e.genPools, ManyCopies(t)), true |
| } else { |
| p = unixProxy(t.Socket) |
| } |
diff --git a/internal/edge/genproxy.go b/internal/edge/genproxy.go| index 395ec7e09..5d71cb27a 100644 |
| --- a/internal/edge/genproxy.go |
| +++ b/internal/edge/genproxy.go |
| @@ -253,8 +253,16 @@ type genRoundTripper struct { |
| host string |
| src GenSource |
| pools *GenPools |
| + // replicated: the route has more than one copy (ManyCopies), so an |
| + // outside stop of this box's app may end the dial hold (holdEnded). |
| + replicated bool |
| } |
| +// manyCopiesKey marks a request genRoundTripper sends for a route with |
| +// more than one copy, for the dial hold, which runs in a pool shared by |
| +// every route of the host's generation (holdEnded). |
| +type manyCopiesKey struct{} |
| + |
| // genRetries bounds how often one request is sent again after its |
| // generation moved under it. |
| const genRetries = 2 |
| @@ -274,6 +282,9 @@ func (t *genRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| // generation moved leaves the body untouched, and the request goes again |
| // on the current generation's pool, a streaming POST with no GetBody |
| // included. |
| + if t.replicated { |
| + req = req.WithContext(context.WithValue(req.Context(), manyCopiesKey{}, true)) |
| + } |
| var hb *heldBody |
| if req.Body != nil && req.Body != http.NoBody { |
| hb = &heldBody{rc: req.Body} |
| @@ -376,15 +387,21 @@ func waitServingGen(ctx context.Context, src GenSource, host string) (GenInfo, b |
| // holdEnded reports whether a hold waiting for host's app (dialGenUntil's |
| // dial retries, waitServingGen's wait for a generation) must end now |
| -// rather than at localDialHold (#511 R27, review round 2): ctx is a fleet |
| -// tunnel stream nothing has been written to, and its box's tunnel drain |
| -// has begun or host's app was stopped from outside the activator |
| -// (src's StoppedOutside). The hold ends at once on the drain (both holds |
| -// also wait on its signal) and within one step on an outside stop. The |
| -// request then fails with nothing written to the app, and |
| -// refuseUnreachedApp refuses it unprocessed, so the hub serves it from |
| -// another copy: drill 3's box exited about 1.15 s after its SIGTERM, well |
| -// inside the 13 s hold. Any other request holds as before. |
| +// rather than at localDialHold (#511 R27, review rounds 2 and 3): ctx is a |
| +// fleet tunnel stream nothing has been written to, and |
| +// - its box's tunnel drain has begun, for any row: the box is going |
| +// away (both holds also wait on the drain's signal, so this ends them |
| +// at once); or |
| +// - host's app was stopped from outside the activator (src's |
| +// StoppedOutside), only for a route with more than one copy |
| +// (manyCopiesKey), where another copy can serve; within one step. A |
| +// single-copy row keeps the whole hold, so an app the activator |
| +// restarts inside it still serves the request. |
| +// |
| +// The request then fails with nothing written to the app, and |
| +// refuseUnreachedApp refuses it unprocessed: drill 3's box exited about |
| +// 1.15 s after its SIGTERM, well inside the 13 s hold. Any other request |
| +// holds as before. |
| func holdEnded(ctx context.Context, src GenSource, host string) bool { |
| if !tunnelclient.Refusable(ctx) { |
| return false |
| @@ -392,6 +409,9 @@ func holdEnded(ctx context.Context, src GenSource, host string) bool { |
| if tunnelclient.Draining(ctx) { |
| return true |
| } |
| + if many, _ := ctx.Value(manyCopiesKey{}).(bool); !many { |
| + return false |
| + } |
| so, ok := src.(interface{ StoppedOutside(string) bool }) |
| return ok && so.StoppedOutside(host) |
| } |
| @@ -438,6 +458,13 @@ func (b *heldBody) release() { _ = b.rc.Close() } |
| // generations on: the same Rewrite, Host preservation, upgrade pass-through |
| // and error handler, over genRoundTripper instead of one fixed socket. |
| func appSocketProxy(host string, src GenSource, pools *GenPools) *httputil.ReverseProxy { |
| + return appSocketProxyFor(host, src, pools, false) |
| +} |
| + |
| +// appSocketProxyFor is appSocketProxy for a route with more than one copy |
| +// when replicated (ManyCopies): an outside stop of this box's app may then |
| +// end the dial hold and refuse the request (#511 R27, review round 3). |
| +func appSocketProxyFor(host string, src GenSource, pools *GenPools, replicated bool) *httputil.ReverseProxy { |
| return &httputil.ReverseProxy{ |
| Rewrite: func(pr *httputil.ProxyRequest) { |
| setForwarded(pr) |
| @@ -446,9 +473,9 @@ func appSocketProxy(host string, src GenSource, pools *GenPools) *httputil.Rever |
| pr.Out.Host = pr.In.Host // backend sees the real hostname |
| scrubInboundPurgeHeader(pr) |
| }, |
| - Transport: &genRoundTripper{host: host, src: src, pools: pools}, |
| + Transport: &genRoundTripper{host: host, src: src, pools: pools, replicated: replicated}, |
| ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { |
| - if refuseUnreachedApp(r, host, src, err) { |
| + if refuseUnreachedApp(r, host, src, replicated, err) { |
| return |
| } |
| defaultProxyError(proxyCause)(w, r, err) |
| @@ -474,16 +501,18 @@ func (e *appNotReachedError) Unwrap() error { return e.error } |
| // refuseUnreachedApp refuses r's fleet tunnel stream unprocessed (#511 |
| // R27), instead of the proxy's 502, when the app never got the request |
| // (appNotReachedError) and either the box's shutdown has begun (its |
| -// tunnel drain) or the host's app last ended without the activator asking |
| -// it to (src's StoppedOutside). The hub then sends the request to another |
| -// copy. Anything else is answered as before. |
| -func refuseUnreachedApp(r *http.Request, host string, src GenSource, err error) bool { |
| +// tunnel drain), or, for a route with more than one copy (replicated), |
| +// the host's app last ended without the activator asking it to (src's |
| +// StoppedOutside). The hub then sends the request to another copy, or, |
| +// for a single-copy row, answers its 503. Anything else is answered as |
| +// before. |
| +func refuseUnreachedApp(r *http.Request, host string, src GenSource, replicated bool, err error) bool { |
| var nr *appNotReachedError |
| if !errors.As(err, &nr) { |
| return false |
| } |
| so, _ := src.(interface{ StoppedOutside(string) bool }) |
| - return RefuseBeforeApp(r, so != nil && so.StoppedOutside(host)) |
| + return RefuseBeforeApp(r, replicated && so != nil && so.StoppedOutside(host)) |
| } |
| // AppSocketProxy is appSocketProxy for callers outside this package: the |
diff --git a/internal/edge/replica_drain_e2e_test.go b/internal/edge/replica_drain_e2e_test.go| index f3af50a7b..f04d62db4 100644 |
| --- a/internal/edge/replica_drain_e2e_test.go |
| +++ b/internal/edge/replica_drain_e2e_test.go |
| @@ -82,9 +82,15 @@ func newReplicaDrainBox(t *testing.T, hubURL, label, token string, drains bool, |
| // An instance with generations on: the edge reaches it through |
| // appSocketProxy, the real genRoundTripper and dial hold. |
| sock := filepath.Join(genSockDir(t), "g1.sock") |
| + genAct.sock, genAct.handler = sock, appHandler |
| genAct.app = startGenApp(t, sock, appHandler) |
| genAct.switchTo("shop.example.com", GenInfo{Socket: sock, N: 1, Build: "v1"}) |
| - if err := reg.Upsert(registry.Route{Host: "shop.example.com", Kind: "instance", Status: "ready", Account: "bab", App: "shop", Stateless: true, Socket: sock}); err != nil { |
| + row := registry.Route{Host: "shop.example.com", Kind: "instance", Status: "ready", Account: "bab", App: "shop", Stateless: true, Socket: sock} |
| + if !genAct.singleCopy { |
| + // The box's own row for a two-copy set (ManyCopies). |
| + row.Region, row.FleetBoxes, row.Copies, row.ReplicaRevision, row.ReplicaIncarnation = "fleet/pi", `["box-00","box-01"]`, 2, 1, "life" |
| + } |
| + if err := reg.Upsert(row); err != nil { |
| t.Fatal(err) |
| } |
| e, err = New(reg, genAct) |
| @@ -733,8 +739,17 @@ func TestReplicaGapRequestsServedBySurvivor(t *testing.T) { |
| // source it embeds; and it answers StoppedOutside as the test says. |
| type replicaGenActivator struct { |
| *fakeGens |
| - app *genApp |
| - outside atomic.Bool |
| + app *genApp |
| + sock string |
| + handler http.Handler |
| + singleCopy bool // box-00's row is a single-copy row, not a two-copy set |
| + outside atomic.Bool |
| +} |
| + |
| +// restartApp is the activator starting the app again on its socket. |
| +func (a *replicaGenActivator) restartApp(t *testing.T) { |
| + t.Helper() |
| + a.app = startGenApp(t, a.sock, a.handler) |
| } |
| func newReplicaGenActivator() *replicaGenActivator { |
| @@ -835,3 +850,31 @@ func TestReplicaDialWrittenIsNotRefused(t *testing.T) { |
| } |
| t.Fatal("no POST reached box-00") |
| } |
| + |
| +// TestSingleCopyDialHoldWaitsForRestart (#511 R27, review round 3): on a |
| +// single-copy row an outside stop does not end the dial hold or refuse |
| +// the request: no other copy could serve it. box-00's app is stopped from |
| +// outside, a POST waits in the real dial hold, the app is started again |
| +// inside the hold, and box-00 serves the POST: no refusal, no 503. |
| +func TestSingleCopyDialHoldWaitsForRestart(t *testing.T) { |
| + genAct := newReplicaGenActivator() |
| + genAct.singleCopy = true |
| + r := newReplicaDrainRig(t, drainRigOpts{drains: true, genAct: genAct}) |
| + b0 := r.boxes[0] |
| + genAct.app.stop() |
| + genAct.outside.Store(true) |
| + base := b0.reached.Load() |
| + held := parkOn(t, r.url, "POST", "/", "kept", func() bool { return b0.reached.Load() >= base+1 }) |
| + time.Sleep(500 * time.Millisecond) |
| + select { |
| + case res := <-held: |
| + t.Fatalf("an outside stop ended a single-copy row's hold: %d %q", res.code, res.got) |
| + default: |
| + } |
| + genAct.restartApp(t) |
| + res := <-held |
| + // TestSingleCopyDialHoldWaitsForRestart |
| + if res.code != 200 || res.got != "box-00 POST kept" { |
| + t.Fatalf("a single-copy row's request across the app's restart: %d %q, want box-00's answer", res.code, res.got) |
| + } |
| +} |
diff --git a/internal/edge/tunnelrefuse.go b/internal/edge/tunnelrefuse.go| index 78d51bd79..5de894b22 100644 |
| --- a/internal/edge/tunnelrefuse.go |
| +++ b/internal/edge/tunnelrefuse.go |
| @@ -3,9 +3,20 @@ package edge |
| import ( |
| "net/http" |
| + "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/tunnelclient" |
| ) |
| +// ManyCopies reports whether rt, as this box holds it, is a replica set |
| +// with more than one copy: another box's copy can serve what this one |
| +// refuses (#511 R27, review round 3). Only then may an outside stop of the |
| +// app refuse a request (the stoppedOutside argument of RefuseBeforeApp, |
| +// and the dial hold's early end); a single-copy row keeps waiting for the |
| +// activator to restart its app. The box's drain applies to every row. |
| +func ManyCopies(rt registry.Route) bool { |
| + return rt.FleetBoxes != "" && rt.Copies > 1 && rt.ValidReplicaProjection() |
| +} |
| + |
| // RefuseBeforeApp is #511 R27's one rule. It refuses r's fleet tunnel |
| // stream unprocessed (RST_STREAM REFUSED_STREAM, tunnelclient.Refuse), |
| // instead of answering it, when the box's shutdown has begun (its tunnel |