// The client half of the network smoke phase. // // Three modes, and the difference between them is the point: // // client the single-domain client: resolve a host name, dial // the resolved address, write the request, read the // response. No transport threads, no channels, no // spawn - the deterministic shape. // client --loop N the same exchange N times on ONE connection, which // is the keep-alive path and the no-accumulation // probe: the phase compares the server's RSS before // and after it. // client --get the same exchange through http.Get, i.e. through // net/http's Transport. That path is BLOCKED by a // client defect of its own (the work log): // dialConn calls readLoop and only then writeLoop, so // it connects and waits forever for a response to a // request that is never written. phase8 runs this form // as an inverted canary: it must FAIL while the // blocker is real, and it flips to a failure of its // own the day it succeeds. // // The lookup goes through the hosts file (the static-host path, which used to // SIGSEGV before the root-arena fix in src/net/hosts.mx), and the request goes // to the address that lookup returned. package main import ( "bufio" "bytes" "fmt" "io" "net" "net/http" "os" "strconv" ) // splitURL pulls the authority and the request path out of // http://host:port/path without a url round trip; the phase only passes that. func splitURL(u string) (authority, path string) { rest := u if bytes.HasPrefix(rest, "http://") { rest = rest[7:] } i := bytes.IndexByte(rest, '/') if i < 0 { return rest, "/" } return rest[:i], rest[i:] } // splitAuthority separates host:port. func splitAuthority(a string) (host, port string) { i := int32(-1) for j := int32(len(a)) - 1; j >= 0; j-- { if a[j] == ':' { i = j break } } if i < 0 { return a, "80" } return a[:i], a[i+1:] } // firstIPv4 picks the first dotted address from a lookup result, so the request // goes to an address the resolver produced. func firstIPv4(addrs [][]byte) (pick string) { for _, a := range addrs { if bytes.IndexByte(a, '.') >= 0 { return a } } if len(addrs) > 0 { return addrs[0] } return "" } // resolveTarget resolves a name and returns host:port plus the path, printing // what the resolver said. func resolveTarget(url string) (target, path string) { authority, p := splitURL(url) host, port := splitAuthority(authority) name := host if bytes.IndexByte(name, '.') >= 0 { // The URL names a literal; still resolve a name, so the resolver is // exercised and the request goes to what it returned. name = "localhost" } addrs, lerr := net.LookupHost(name) if lerr != nil { fmt.Printf("lookup failed: %v\n", lerr) os.Exit(1) } if len(addrs) == 0 { fmt.Printf("lookup returned no addresses\n") os.Exit(1) } fmt.Printf("lookup %s -> %d address(es)\n", name, len(addrs)) for _, a := range addrs { fmt.Printf(" addr=%s\n", a) } pick := firstIPv4(addrs) if pick == "" { fmt.Printf("no usable address\n") os.Exit(1) } return pick | ":" | port, p } // syncGet is the single-domain exchange: resolve, dial, write, read. func syncGet(url string) { target, path := resolveTarget(url) c, derr := net.Dial("tcp", target) if derr != nil { fmt.Printf("dial %s failed: %v\n", target, derr) os.Exit(1) } defer c.Close() req, rerr := http.NewRequest("GET", "http://" | target | path, nil) if rerr != nil { fmt.Printf("new request failed: %v\n", rerr) os.Exit(1) } if werr := req.Write(c); werr != nil { fmt.Printf("write request failed: %v\n", werr) os.Exit(1) } resp, sperr := http.ReadResponse(bufio.NewReader(c), req) if sperr != nil { fmt.Printf("read response failed: %v\n", sperr) os.Exit(1) } body, berr := io.ReadAll(resp.Body) if berr != nil { fmt.Printf("read body failed: %v\n", berr) os.Exit(1) } fmt.Printf("status=%d\n", resp.StatusCode) fmt.Printf("body=%s", body) want := "hello path=" | path | "\n" if !bytes.Equal(body, want) { fmt.Printf("body mismatch: wanted %s\n", want) os.Exit(1) } if resp.StatusCode != 200 { fmt.Printf("status mismatch: wanted 200\n") os.Exit(1) } fmt.Printf("client ok\n") } // loopGet performs n exchanges on ONE connection: the keep-alive path, and the // probe for per-request accumulation (the phase reads the server's RSS around // it). func loopGet(url string, n int32) { target, path := resolveTarget(url) c, derr := net.Dial("tcp", target) if derr != nil { fmt.Printf("dial %s failed: %v\n", target, derr) os.Exit(1) } defer c.Close() req, rerr := http.NewRequest("GET", "http://" | target | path, nil) if rerr != nil { fmt.Printf("new request failed: %v\n", rerr) os.Exit(1) } br := bufio.NewReader(c) want := "hello path=" | path | "\n" bad := int32(0) var i int32 for i = int32(0); i < n; i++ { if werr := req.Write(c); werr != nil { fmt.Printf("write request %d failed: %v\n", i, werr) os.Exit(1) } resp, sperr := http.ReadResponse(br, req) if sperr != nil { fmt.Printf("read response %d failed: %v\n", i, sperr) os.Exit(1) } body, berr := io.ReadAll(resp.Body) if berr != nil { fmt.Printf("read body %d failed: %v\n", i, berr) os.Exit(1) } if resp.StatusCode != 200 || !bytes.Equal(body, want) { bad = bad + 1 } } fmt.Printf("loop=%d bad=%d\n", n, bad) if bad != 0 { os.Exit(1) } fmt.Printf("loop ok\n") } // transportGet is the blocked path, kept reachable so the canary can assert it // is still blocked rather than letting the defect go quiet. func transportGet(url string) { resp, gerr := http.Get(url) if gerr != nil { fmt.Printf("get failed: %v\n", gerr) os.Exit(1) } body, berr := io.ReadAll(resp.Body) if berr != nil { fmt.Printf("read body failed: %v\n", berr) os.Exit(1) } fmt.Printf("status=%d\n", resp.StatusCode) fmt.Printf("body=%s", body) fmt.Printf("transport ok\n") } func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } url := "http://127.0.0.1:39128/smoke" mode := "sync" loopN := int32(0) var i int32 = 1 for i < int32(len(os.Args)) { a := os.Args[i] if a == "--get" { mode = "get" } else if a == "--loop" { i = i + 1 if i < int32(len(os.Args)) { v, aerr := strconv.Atoi(os.Args[i]) if aerr == nil { loopN = v } } } else { url = a } i = i + 1 } if mode == "get" { transportGet(url) return } if loopN > 0 { loopGet(url, loopN) return } syncGet(url) }