carlos / platform Public

edge: whole-branch minors: the registration lock guard, a quiet forward fixture, a lower bound named (#471 Task 9)

- tunnel_publish_guard_test.go: an AST guard (reload_guard_test.go's
  pattern) pinning that ServeHTTP's dynamic-credential re-check and its
  h.conns publish sit inside one TunnelHub.mu hold, with no unlock between
  them but on the refusal branch that returns, and that SetDynamicAuth
  reads and replaces dynamicAuth inside one hold, the only writer of it.
  Mutations fail it: re-check moved before the Lock, an unlock/relock
  before the publish, a nested unlock that carries on, the swap moved
  after the Unlock.
- fleetforward_test.go: forwardHubAndBox writes its hub rows once the hub
  is armed. New's startup Reload runs before EnableTunnels, so it could not
  be the hub and logged a "not the hub" refusal for every forward row;
  setting the environment earlier alone does not help, since Reload only
  consults HubArmed when tunnels are enabled.
- tunnel_ws_load_e2e_test.go: the kernel's share is a lower bound, so the
  failure message says "at least".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev aa744f82cd5d145bf3ed7a05237b49e8be994e60 parent abf5811
3 files changed, +220 −10
  • internal/edge/fleetforward_test.go +13 −9
  • internal/edge/tunnel_publish_guard_test.go +206 −0
  • internal/edge/tunnel_ws_load_e2e_test.go +1 −1
diff --git a/internal/edge/fleetforward_test.go b/internal/edge/fleetforward_test.go
index 547aa74d1..fd31f18b4 100644
--- a/internal/edge/fleetforward_test.go
+++ b/internal/edge/fleetforward_test.go
@@ -77,15 +77,6 @@ func forwardHubAndBox(t *testing.T, hubRows, boxRows []registry.Route, boxHandle
t.Fatalf("AddFleetBox(%s/%s): %v", b.Fleet, b.Label, err)
}
}
- for _, rt := range hubRows {
- put := hubReg.Upsert
- if rt.IsForward() {
- put = hubReg.PutForward
- }
- if err := put(rt); err != nil {
- t.Fatalf("hub row %s: %v", rt.Host, err)
- }
- }
hub, err := New(hubReg, nil)
if err != nil {
t.Fatalf("New(hub): %v", err)
@@ -100,6 +91,19 @@ func forwardHubAndBox(t *testing.T, hubRows, boxRows []registry.Route, boxHandle
t.Setenv("CARLOS_TUNNEL_HOST", u.Hostname())
t.Setenv("CARLOS_FLEET", "")
hub.EnableTunnels(NewTunnelHub(u.Hostname(), map[string]string{"t1": fwdT1Tok}))
+ // The rows go in once the hub is armed, so the first Reload to see them
+ // is the one below. New's startup Reload runs before EnableTunnels, on
+ // an edge that cannot be the hub yet; had the rows been there, it would
+ // have logged a refusal for every forward row.
+ for _, rt := range hubRows {
+ put := hubReg.Upsert
+ if rt.IsForward() {
+ put = hubReg.PutForward
+ }
+ if err := put(rt); err != nil {
+ t.Fatalf("hub row %s: %v", rt.Host, err)
+ }
+ }
if err := hub.Reload(); err != nil {
t.Fatalf("Reload(hub): %v", err)
}
diff --git a/internal/edge/tunnel_publish_guard_test.go b/internal/edge/tunnel_publish_guard_test.go
new file mode 100644
index 000000000..1872f4244
--- /dev/null
+++ b/internal/edge/tunnel_publish_guard_test.go
@@ -0,0 +1,206 @@
+package edge
+
+// Tunnel WebSockets (#471) Task 4: a dynamic channel's registration checks
+// its credential again as it publishes the connection, and the check and
+// the publish sit inside one TunnelHub.mu hold; SetDynamicAuth replaces the
+// credential map under that same mutex. Between them, a publish and a
+// replacement exclude each other, so a detach can never leave a revoked box
+// with a live channel. A source check over tunnel.go, in the manner of
+// reload_guard_test.go, so moving the re-check out of the lock, unlocking
+// between it and the publish, or swapping the map outside the lock fails
+// here by name.
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "testing"
+)
+
+// tunnelHubFuncs parses tunnel.go and returns its methods by funcName.
+func tunnelHubFuncs(t *testing.T) (*token.FileSet, map[string]*ast.FuncDecl) {
+ t.Helper()
+ fset := token.NewFileSet()
+ af, err := parser.ParseFile(fset, "tunnel.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ fns := map[string]*ast.FuncDecl{}
+ for _, d := range af.Decls {
+ if fd, ok := d.(*ast.FuncDecl); ok && fd.Body != nil {
+ fns[funcName(fd)] = fd
+ }
+ }
+ return fset, fns
+}
+
+// lockedSpan finds the first top-level `h.mu.Lock()` statement in body and
+// the first top-level `h.mu.Unlock()` after it (len(body) when the lock is
+// never released by a top-level statement). lock is -1 when there is none.
+func lockedSpan(body []ast.Stmt) (lock, unlock int) {
+ lock, unlock = -1, len(body)
+ for i, s := range body {
+ switch callSel(s) {
+ case "h.mu.Lock":
+ if lock < 0 {
+ lock = i
+ }
+ case "h.mu.Unlock":
+ if lock >= 0 && i > lock && unlock == len(body) {
+ unlock = i
+ }
+ }
+ }
+ return lock, unlock
+}
+
+// unlocksOnlyToLeave reports every h.mu.Unlock() call inside n that is not
+// a statement of a block whose last statement is a return: the refusal
+// branch may drop the lock on its way out, but nothing may drop it and
+// carry on toward the publish.
+func unlocksOnlyToLeave(t *testing.T, fset *token.FileSet, what string, n ast.Node) {
+ t.Helper()
+ allowed := map[ast.Stmt]bool{}
+ ast.Inspect(n, func(n ast.Node) bool {
+ b, ok := n.(*ast.BlockStmt)
+ if !ok || len(b.List) == 0 {
+ return true
+ }
+ if _, ret := b.List[len(b.List)-1].(*ast.ReturnStmt); !ret {
+ return true
+ }
+ for _, s := range b.List {
+ if callSel(s) == "h.mu.Unlock" {
+ allowed[s] = true
+ }
+ }
+ return true
+ })
+ ast.Inspect(n, func(n ast.Node) bool {
+ switch v := n.(type) {
+ case *ast.FuncLit:
+ return false // a closure's unlock runs elsewhere; none is expected
+ case ast.Stmt:
+ if callSel(v) == "h.mu.Unlock" && !allowed[v] {
+ t.Errorf("%s: h.mu.Unlock() at %s between the lock and the publish, not on a branch that returns", what, fset.Position(v.Pos()))
+ }
+ }
+ return true
+ })
+}
+
+// isConnsInsert reports whether s is `h.conns[...] = ...`.
+func isConnsInsert(s ast.Stmt) bool {
+ as, ok := s.(*ast.AssignStmt)
+ if !ok || as.Tok != token.ASSIGN || len(as.Lhs) != 1 {
+ return false
+ }
+ ix, ok := as.Lhs[0].(*ast.IndexExpr)
+ return ok && selChain(ix.X) == "h.conns"
+}
+
+func TestTunnelRegistrationRecheckAndPublishShareOneLock(t *testing.T) {
+ fset, fns := tunnelHubFuncs(t)
+ serve := fns["(*TunnelHub).ServeHTTP"]
+ if serve == nil {
+ t.Fatal("no (*TunnelHub).ServeHTTP in tunnel.go")
+ }
+ body := serve.Body.List
+
+ // Exactly one publish, as a statement of ServeHTTP's own body.
+ insert, inserts := -1, 0
+ for i, s := range body {
+ if isConnsInsert(s) {
+ insert = i
+ }
+ }
+ ast.Inspect(serve.Body, func(n ast.Node) bool {
+ if s, ok := n.(ast.Stmt); ok && isConnsInsert(s) {
+ inserts++
+ }
+ return true
+ })
+ if insert < 0 || inserts != 1 {
+ t.Fatalf("ServeHTTP has %d h.conns[...] assignment(s) (one a statement of its body: %v); want exactly one, a statement of the body", inserts, insert >= 0)
+ }
+
+ lock, unlock := lockedSpan(body)
+ if lock < 0 {
+ t.Fatal("ServeHTTP does not call h.mu.Lock() as a statement of its body")
+ }
+ if !(lock < insert && insert < unlock) {
+ t.Fatalf("ServeHTTP's publish (statement %d, %s) is not inside its h.mu hold (Lock at statement %d, first Unlock at %d)",
+ insert, fset.Position(body[insert].Pos()), lock, unlock)
+ }
+
+ // The re-check: ServeHTTP calls verifyDynamicLocked once, and that call
+ // sits between the Lock and the publish.
+ var rechecks []token.Pos
+ ast.Inspect(serve.Body, func(n ast.Node) bool {
+ if c, ok := n.(*ast.CallExpr); ok && selChain(c.Fun) == "h.verifyDynamicLocked" {
+ rechecks = append(rechecks, c.Pos())
+ }
+ return true
+ })
+ if len(rechecks) != 1 {
+ t.Fatalf("ServeHTTP calls h.verifyDynamicLocked %d times, want once (the re-check under h.mu)", len(rechecks))
+ }
+ if p := rechecks[0]; p < body[lock].End() || p > body[insert].Pos() {
+ t.Fatalf("ServeHTTP's credential re-check at %s is not between its h.mu.Lock() (%s) and its publish (%s)",
+ fset.Position(p), fset.Position(body[lock].Pos()), fset.Position(body[insert].Pos()))
+ }
+
+ // No unlock between the Lock and the publish, but on a branch that
+ // returns (the refusal).
+ for _, s := range body[lock+1 : insert] {
+ unlocksOnlyToLeave(t, fset, "ServeHTTP", s)
+ }
+
+ // SetDynamicAuth: the map is replaced, and the old one read, inside
+ // one h.mu hold; nothing else in tunnel.go assigns h.dynamicAuth.
+ set := fns["(*TunnelHub).SetDynamicAuth"]
+ if set == nil {
+ t.Fatal("no (*TunnelHub).SetDynamicAuth in tunnel.go")
+ }
+ sb := set.Body.List
+ slock, sunlock := lockedSpan(sb)
+ if slock < 0 {
+ t.Fatal("SetDynamicAuth does not call h.mu.Lock() as a statement of its body")
+ }
+ replace, readOld := -1, -1
+ for i, s := range sb {
+ as, ok := s.(*ast.AssignStmt)
+ if !ok || len(as.Lhs) != 1 || len(as.Rhs) != 1 {
+ continue
+ }
+ if as.Tok == token.ASSIGN && selChain(as.Lhs[0]) == "h.dynamicAuth" {
+ replace = i
+ }
+ if selChain(as.Rhs[0]) == "h.dynamicAuth" {
+ readOld = i
+ }
+ }
+ if replace < 0 {
+ t.Fatal("SetDynamicAuth does not assign h.dynamicAuth as a statement of its body")
+ }
+ if !(slock < replace && replace < sunlock) {
+ t.Fatalf("SetDynamicAuth replaces h.dynamicAuth (statement %d) outside its h.mu hold (Lock at %d, Unlock at %d)", replace, slock, sunlock)
+ }
+ if !(slock < readOld && readOld < sunlock) {
+ t.Fatalf("SetDynamicAuth reads the old h.dynamicAuth (statement %d) outside the hold that replaces it (Lock at %d, Unlock at %d)", readOld, slock, sunlock)
+ }
+ for name, fd := range fns {
+ ast.Inspect(fd.Body, func(n ast.Node) bool {
+ as, ok := n.(*ast.AssignStmt)
+ if !ok {
+ return true
+ }
+ for _, l := range as.Lhs {
+ if sel, ok := l.(*ast.SelectorExpr); ok && sel.Sel.Name == "dynamicAuth" && name != "(*TunnelHub).SetDynamicAuth" {
+ t.Errorf("%s assigns %s at %s; only SetDynamicAuth may, under h.mu", name, selChain(sel), fset.Position(l.Pos()))
+ }
+ }
+ return true
+ })
+ }
+}
diff --git a/internal/edge/tunnel_ws_load_e2e_test.go b/internal/edge/tunnel_ws_load_e2e_test.go
index a878a7608..e1b2b9b42 100644
--- a/internal/edge/tunnel_ws_load_e2e_test.go
+++ b/internal/edge/tunnel_ws_load_e2e_test.go
@@ -382,7 +382,7 @@ func TestTunnelWebSocketMaximumLoad(t *testing.T) {
r.unsent, r.unread = kernelQueues(t, s, socks[i].h.conn.(*tls.Conn))
r.held = processHeld(r.n, r.unsent, r.unread)
if r.held > bound {
- t.Fatalf("socket %d: %d bytes accepted, at most %d of them in the kernel's queues (%d unsent, %d unread), so the processes hold %d; want at most StreamWindow + 4 MiB = %d",
+ t.Fatalf("socket %d: %d bytes accepted, at least %d of them in the kernel's queues (%d unsent, %d unread), so the processes hold %d; want at most StreamWindow + 4 MiB = %d",
i+1, r.n, r.n-r.held, r.unsent, r.unread, r.held, bound)
}
lo, hi, total = min(lo, r.held), max(hi, r.held), total+r.n