carlos / platform Public

edge: upgrade re-checks select as their request did (#186)

reserveUpgrade and sweepUpgrades judged an admitted tunnel upgrade by
lookupUpstreamPath(host, path), which knows nothing of a case 3 target:
an upgrade admitted for a target whose front this box does not serve was
refused at reservation, or swept at the next reload. Both now re-select
through upgradeSelection: selectUpstream over the admitting request's
context (pin, door, hop marks), kept on the reservation for the sweep.
The upgrade stays while its front holds and is swept when the front's
published owner no longer matches the claim.

lookupUpstreamPath has no production caller left; the selection guard's
lookupNonRequestSites drops the two upgrade functions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 269e277dc6a6fe70503a4c0981290aab1c7635a2 parent eba0430
5 files changed, +71 −11
  • internal/edge/edge.go +3 −2
  • internal/edge/linkpin_test.go +0 −2
  • internal/edge/path_across_e2e_test.go +43 −0
  • internal/edge/pathhop.go +12 −0
  • internal/edge/upgrades.go +13 −7
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index bc3cffef5..b9479bdd7 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -1890,8 +1890,9 @@ func (e *Edge) lookupUpstream(host string) (upstream, bool) {
// lookupUpstreamPath resolves a hostname and path on this box alone: pathMatch's
// up/ok over the current map (pathselect.go holds the matcher and its
// reasoning). Never a request's selection, which is selectUpstream's
-// (TestSelectUpstreamIsTheOnlySite): its callers ask whether a host and path
-// still map where they did.
+// (TestSelectUpstreamIsTheOnlySite). No production code calls it: the
+// upgrade re-checks select as their request did (upgradeSelection); tests use
+// it to read the local map.
func (e *Edge) lookupUpstreamPath(host, urlPath string) (upstream, bool) {
up, ok, _, _, _ := e.pathMatch(e.current(), host, urlPath)
return up, ok
diff --git a/internal/edge/linkpin_test.go b/internal/edge/linkpin_test.go
index 7dafdc3e9..fcfb94f4c 100644
--- a/internal/edge/linkpin_test.go
+++ b/internal/edge/linkpin_test.go
@@ -32,8 +32,6 @@ import (
var lookupNonRequestSites = map[string]string{
"httpFallbackHandler": "the :80 listener asks only whether this box serves a hostname before redirecting it to https; it never dispatches",
"demoTrafficAllowed": "re-reads the row of a demo route already selected, by that route's own host",
- "reserveUpgrade": "the hub's re-check that an admitted tunnel upgrade's host still maps to its tenancy",
- "sweepUpgrades": "the reload sweep that closes tunnel upgrades whose host no longer maps to their tenancy",
}
// The selection functions (pathselect.go, path routes across boxes #186)
diff --git a/internal/edge/path_across_e2e_test.go b/internal/edge/path_across_e2e_test.go
index 31521da44..9a17369be 100644
--- a/internal/edge/path_across_e2e_test.go
+++ b/internal/edge/path_across_e2e_test.go
@@ -15,6 +15,7 @@ package edge
import (
"context"
+ "errors"
"fmt"
"io"
"net"
@@ -730,3 +731,45 @@ func TestPathAcrossPageCacheExcludesCase3(t *testing.T) {
wantCache(t, s.get("/p"), "miss")
wantCache(t, s.get("/p"), "hit")
}
+
+// An upgrade admitted for a case 3 target is reserved, and kept by the
+// reload sweep, against the selection for its request; once the front's
+// published owner no longer matches the claim, the sweep closes it.
+func TestPathAcrossUpgradeCase3Reserved(t *testing.T) {
+ const front, target = "hub.tunnel.example", "t.example.com"
+ _, hub, _ := forwardHubAndBox(t, []registry.Route{
+ {Host: target, Kind: "instance", Status: "ready", Tunnel: "t1", Account: fwdOther, App: "blog"},
+ }, nil, namedHandler("box-1"))
+ remotePathRow(t, hub.reg, front, acrossPrefix, target, fwdOther, "hub")
+ if err := hub.Reload(); err != nil {
+ t.Fatal(err)
+ }
+ store := meshTestStore(t)
+ m := newHomeMap(store, "annie")
+ putPathFile(t, m, "betty", "2.2.2.2", map[string]string{front: frontRow(fwdOther, "hub")})
+ refreshHomeMap(t, m)
+ hub.homes.Store(m)
+ tc := hub.tunnels.channel("home/box-1")
+ if tc == nil {
+ t.Fatal("home/box-1 has no channel connection")
+ }
+ ten := tenancy{ArmTunnel, fwdOther, "blog", "", "t1", ""}
+ r, _, err := hub.reserveUpgrade(context.Background(), upgradeAdmission{Host: front, Path: "/acme/cable", Tenancy: ten}, tc, 10)
+ if err != nil {
+ t.Fatalf("reserve for a case 3 target: %v, want admitted", err)
+ }
+ defer r.release(relBody)
+ hub.sweepUpgrades()
+ if n := heldFor(hub, front); n != 1 {
+ t.Fatalf("after a sweep with nothing changed: %d held, want 1", n)
+ }
+ putPathFile(t, m, "betty", "2.2.2.2", map[string]string{front: frontRow(fwdOwner, "other")})
+ refreshHomeMap(t, m)
+ hub.sweepUpgrades()
+ if n := heldFor(hub, front); n != 0 {
+ t.Fatalf("after the front's owner changed: %d held, want 0", n)
+ }
+ if _, _, err := hub.reserveUpgrade(context.Background(), upgradeAdmission{Host: front, Path: "/acme/cable", Tenancy: ten}, tc, 10); !errors.Is(err, errTenancyStale) {
+ t.Fatalf("reserve after the owner changed: %v, want errTenancyStale", err)
+ }
+}
diff --git a/internal/edge/pathhop.go b/internal/edge/pathhop.go
index 54bf06ead..b3775bb6a 100644
--- a/internal/edge/pathhop.go
+++ b/internal/edge/pathhop.go
@@ -6,6 +6,7 @@ package edge
// respect but its wire Host and MAC, which stay H's.
import (
+ "context"
"fmt"
"net/http"
"strings"
@@ -167,3 +168,14 @@ func (e *Edge) serveNameOwner(w http.ResponseWriter, r *http.Request, host strin
}
return false
}
+
+// upgradeSelection is the selection an admitted upgrade is re-checked against
+// (reserveUpgrade, sweepUpgrades): host and urlPath selected as the request
+// that admitted it was, from that request's context (its pin, door caller,
+// verified hop and door hop marks), so a target selected for a front this
+// box does not serve (case 3) still maps to the tenancy it was admitted
+// under while its front holds. A Remote selection is no upstream here.
+func (e *Edge) upgradeSelection(ctx context.Context, host, urlPath string) (upstream, bool) {
+ sel := e.selectUpstream(new(http.Request).WithContext(ctx), host, urlPath)
+ return sel.Up, sel.OK
+}
diff --git a/internal/edge/upgrades.go b/internal/edge/upgrades.go
index f8b64fa11..aa675a780 100644
--- a/internal/edge/upgrades.go
+++ b/internal/edge/upgrades.go
@@ -114,6 +114,9 @@ type upgradeReservation struct {
path string
ten tenancy
tc *tunnelConn
+ // sel is the admitting request's context: the sweep selects host and
+ // path again as that request was selected (upgradeSelection).
+ sel context.Context
cancel context.CancelFunc // the stream's context
once sync.Once
@@ -195,8 +198,10 @@ func (o *openUpgrades) remove(r *upgradeReservation) {
// reserveUpgrade takes a reservation for an admitted upgrade on channel
// connection tc, or refuses it. Under the registry's lock, the current map
-// must still serve a's host and path for a's tenancy (else errTenancyStale)
-// and tc must have fewer than capN upgrades open (else errTunnelBusy).
+// must still serve a's host and path for a's tenancy (else errTenancyStale),
+// selected as the inbound request was (upgradeSelection: a case 3 path
+// target keeps its upgrade while its front holds), and tc must have fewer
+// than capN upgrades open (else errTunnelBusy).
// Nothing is held after a refusal.
//
// It returns the reservation and the stream's context, which release
@@ -209,7 +214,7 @@ func (e *Edge) reserveUpgrade(inbound context.Context, a upgradeAdmission, tc *t
o.beforeReserve()
}
o.mu.Lock()
- up, ok := e.lookupUpstreamPath(a.Host, a.Path)
+ up, ok := e.upgradeSelection(inbound, a.Host, a.Path)
if !ok || a.Tenancy.Arm == ArmNone || up.tenancy != a.Tenancy {
o.mu.Unlock()
return nil, nil, errTenancyStale
@@ -219,7 +224,7 @@ func (e *Edge) reserveUpgrade(inbound context.Context, a upgradeAdmission, tc *t
return nil, nil, errTunnelBusy
}
ctx, cancel := context.WithCancel(inbound)
- r := &upgradeReservation{reg: o, host: a.Host, path: a.Path, ten: a.Tenancy, tc: tc, cancel: cancel}
+ r := &upgradeReservation{reg: o, host: a.Host, path: a.Path, ten: a.Tenancy, tc: tc, sel: inbound, cancel: cancel}
if o.byHost == nil {
o.byHost = make(map[string]map[*upgradeReservation]struct{})
}
@@ -249,8 +254,9 @@ func (e *Edge) reserveUpgrade(inbound context.Context, a upgradeAdmission, tc *t
}
// sweepUpgrades closes every open upgrade the current map no longer serves
-// for the tenancy it was admitted under (spec §4.6.1): its host and path no
-// longer resolve, or resolve to an upstream with another tenancy. Reload
+// for the tenancy it was admitted under (spec §4.6.1): its host and path,
+// selected as the admitting request was (upgradeSelection), no longer
+// resolve, or resolve to an upstream with another tenancy. Reload
// calls it right after its swap, under reloadMu, so it judges against the
// map that Reload published.
//
@@ -265,7 +271,7 @@ func (e *Edge) sweepUpgrades() {
o.mu.Lock()
for host, set := range o.byHost {
for r := range set {
- if up, ok := e.lookupUpstreamPath(r.host, r.path); ok && up.tenancy == r.ten {
+ if up, ok := e.upgradeSelection(r.sel, r.host, r.path); ok && up.tenancy == r.ten {
continue
}
delete(set, r)