phase8_netsmoke.sh raw

   1  #!/bin/bash
   2  # Phase 8: the net stack end to end, over real sockets, on the channel handler
   3  # path.
   4  #
   5  # The shell owns the socket, the framing and the keep-alive loop; a handler is a
   6  # pure stream function that receives decoded request messages and sends response
   7  # messages. Both dispatch policies are exercised, because the messages are the
   8  # same in both and that is the point: spawning is a scheduling choice.
   9  #
  10  #   inline   every handler runs in the connection's own domain, no spawn at all
  11  #            - the deterministic mode, asserted by repeated identical requests
  12  #   spawned  a fixed pool of handler routines pulls from its queue, sized from
  13  #            the measured cost of a spawn (spawncost fixture)
  14  #
  15  # A registered route must serve: curl (a foreign client) and the Moxie client
  16  # both assert the handler's body, and the client resolves a host name first
  17  # (net.LookupHost over the hosts file, the path that SIGSEGVed before the
  18  # root-arena fix in src/net/hosts.mx).
  19  set -euo pipefail
  20  SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  21  ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
  22  export MOXIEROOT="$ROOT"
  23  source "$SCRIPT_DIR/util.sh"
  24  
  25  echo ""
  26  echo "=== Phase 8: network smoke (channel handler path) ==="
  27  
  28  td="$(tempdir "netsmoke")"; rm -rf "$td"; mkdir -p "$td"
  29  
  30  build_ok=1
  31  "$MOXIE" build -o "$td/server" "$SCRIPT_DIR/netsmoke/server" >"$td/server-build.log" 2>&1 || build_ok=0
  32  "$MOXIE" build -o "$td/client" "$SCRIPT_DIR/netsmoke/client" >"$td/client-build.log" 2>&1 || build_ok=0
  33  "$MOXIE" build -o "$td/spawncost" "$SCRIPT_DIR/netsmoke/spawncost" >"$td/spawncost-build.log" 2>&1 || build_ok=0
  34  "$MOXIE" build -o "$td/muxmatch" "$SCRIPT_DIR/netsmoke/muxmatch" >"$td/muxmatch-build.log" 2>&1 || build_ok=0
  35  "$MOXIE" build -o "$td/spawnbytes" "$SCRIPT_DIR/netsmoke/spawnbytes" >"$td/spawnbytes-build.log" 2>&1 || build_ok=0
  36  
  37  if [ "$build_ok" = 0 ]; then
  38  	log_fail "netsmoke:build" "$(grep -m1 -E 'error|^; ' "$td/server-build.log" "$td/client-build.log" "$td/spawncost-build.log" "$td/muxmatch-build.log" "$td/spawnbytes-build.log" 2>/dev/null | head -1)"
  39  	rm -rf "$td"
  40  	exit 0
  41  fi
  42  log_pass "netsmoke:build" "server, client, spawn-cost, mux-match and spawn-bytes fixtures built"
  43  
  44  # The classic ServeMux's matching, every registration shape. The smoke server
  45  # below serves through the CHANNEL path, whose router is a plain table, so this
  46  # is the only unit that would notice a routing tree that stores nothing: the bug
  47  # this pins made every route answer 404 (the node registration returned was a
  48  # copy of the node the tree stored).
  49  if "$td/muxmatch" >"$td/muxmatch.out" 2>&1; then
  50  	log_pass "netsmoke:mux-match" "$(tail -1 "$td/muxmatch.out")"
  51  else
  52  	log_fail "netsmoke:mux-match" "$(tail -3 "$td/muxmatch.out" 2>/dev/null)"
  53  fi
  54  
  55  # A []byte built at run time must cross a spawn channel byte-exact. This is the
  56  # boundary's own unit: the smoke server below never sends a slice across, it
  57  # packs an int32 stream, so nothing else here would notice an address being
  58  # handed to a forked domain.
  59  if "$td/spawnbytes" >"$td/spawnbytes.out" 2>&1; then
  60  	log_pass "netsmoke:spawn-bytes" "$(tail -1 "$td/spawnbytes.out")"
  61  else
  62  	log_fail "netsmoke:spawn-bytes" "$(tail -3 "$td/spawnbytes.out" 2>/dev/null)"
  63  fi
  64  
  65  # The pool size is a bound, not a guess: this is what a spawn costs here.
  66  if "$td/spawncost" >"$td/spawncost.out" 2>&1; then
  67  	log_pass "netsmoke:spawn-cost" "$(cat "$td/spawncost.out")"
  68  else
  69  	log_fail "netsmoke:spawn-cost" "$(tail -2 "$td/spawncost.out" 2>/dev/null)"
  70  fi
  71  
  72  # The box runs a resolver and other services, so a fixed port is checked rather
  73  # than assumed free.
  74  free_port() {
  75  	local p
  76  	for p in 39128 39129 39130 39131 39132; do
  77  		if ! ss -ltn 2>/dev/null | awk '{print $4}' | grep -qE "[:.]$p\$"; then
  78  			echo "$p"
  79  			return 0
  80  		fi
  81  	done
  82  	return 0
  83  }
  84  
  85  run_mode() {
  86  	local mode="$1" port pid listening=0 out1 out2 out3
  87  	port="$(free_port)"
  88  	if [ -z "$port" ]; then
  89  		log_fail "netsmoke:port-$mode" "no free port in 39128-39132"
  90  		return 0
  91  	fi
  92  	"$td/server" "127.0.0.1:$port" "$mode" >"$td/server-$mode.out" 2>&1 &
  93  	pid=$!
  94  	for _ in $(seq 1 50); do
  95  		if ! kill -0 "$pid" 2>/dev/null; then
  96  			break
  97  		fi
  98  		if ss -ltn 2>/dev/null | awk '{print $4}' | grep -qE "[:.]$port\$"; then
  99  			listening=1
 100  			break
 101  		fi
 102  		sleep 0.2
 103  	done
 104  	if [ "$listening" = 0 ]; then
 105  		log_fail "netsmoke:listen-$mode" "$(tail -3 "$td/server-$mode.out" 2>/dev/null)"
 106  		kill "$pid" 2>/dev/null || true
 107  		wait "$pid" 2>/dev/null || true
 108  		return 0
 109  	fi
 110  	log_pass "netsmoke:listen-$mode" "listening on 127.0.0.1:$port"
 111  
 112  	# 8a: a foreign client sees the route the fixture registered.
 113  	out1="$(curl -s -m 15 "http://127.0.0.1:$port/smoke" 2>&1 || true)"
 114  	if [ "$out1" = "hello path=/smoke" ]; then
 115  		log_pass "netsmoke:curl-$mode" "200 with the handler body"
 116  	else
 117  		log_fail "netsmoke:curl-$mode" "curl got: $out1"
 118  	fi
 119  
 120  	# Keep-alive: two requests on ONE connection. curl reuses the connection
 121  	# when it is given two URLs, so this is the shell's per-connection loop and
 122  	# not two accept cycles.
 123  	ka="$(curl -s -m 15 "http://127.0.0.1:$port/one" "http://127.0.0.1:$port/two" 2>&1 || true)"
 124  	want_ka="hello path=/one
 125  hello path=/two"
 126  	if [ "$ka" = "$want_ka" ]; then
 127  		log_pass "netsmoke:keepalive-$mode" "two requests on one connection"
 128  	else
 129  		log_fail "netsmoke:keepalive-$mode" "got: [$ka]"
 130  	fi
 131  
 132  	# The single-domain mode is the deterministic one: the same request must
 133  	# produce byte-identical responses, every time.
 134  	out2="$(curl -s -m 15 "http://127.0.0.1:$port/smoke" 2>&1 || true)"
 135  	out3="$(curl -s -m 15 "http://127.0.0.1:$port/smoke" 2>&1 || true)"
 136  	# Compared to each other AND to the expected body: three identical EMPTY
 137  	# responses used to pass this unit, which is how a spawned mode that
 138  	# answered nothing was recorded green (round 279).
 139  	if [ "$out1" = "hello path=/smoke" ] && [ "$out1" = "$out2" ] && [ "$out2" = "$out3" ]; then
 140  		log_pass "netsmoke:repeatable-$mode" "three identical requests, identical responses"
 141  	else
 142  		log_fail "netsmoke:repeatable-$mode" "responses differ: [$out1] [$out2] [$out3]"
 143  	fi
 144  
 145  	# 8b: the Moxie client resolves a name and completes a real request THROUGH
 146  	# net/http's Transport (--get). This is the unit that exercises the client
 147  	# stack's connection handling, not just the single-domain exchange: the
 148  	# transport is single-threaded (this domain has no goroutine and no task
 149  	# primitive), so one call dials, writes, reads and materialises the body.
 150  	# The distinction the failure message keeps is resolver-vs-transport: an
 151  	# address with no status means the name resolved and the exchange did not.
 152  	crc=0
 153  	timeout 20 "$td/client" --get "http://127.0.0.1:$port/smoke" >"$td/client-$mode.out" 2>&1 || crc=$?
 154  	if [ "$crc" = 0 ] && grep -q 'transport ok' "$td/client-$mode.out" 2>/dev/null; then
 155  		log_pass "netsmoke:client-$mode" "$(grep -c 'addr=' "$td/client-$mode.out" 2>/dev/null || true) address(es), status 200, body matched, transport ok"
 156  	elif grep -q 'addr=' "$td/client-$mode.out" 2>/dev/null && ! grep -q 'status=' "$td/client-$mode.out" 2>/dev/null; then
 157  		log_fail "netsmoke:client-$mode" "resolver ok, request never completed (rc=$crc): the single-threaded transport did not complete the exchange"
 158  	else
 159  		log_fail "netsmoke:client-$mode" "rc=$crc: $(tail -3 "$td/client-$mode.out" 2>/dev/null)"
 160  	fi
 161  
 162  	# No accumulation: the FIRST 400 exchanges of the process, in two runs of
 163  	# 200 on one connection each, with the server's own RSS report between them.
 164  	# There is deliberately no warm-up run before the baseline: a one-time cost
 165  	# that lands in the first few hundred requests (an arena chaining an
 166  	# overflow region, a mapping being faulted) is retention as far as a
 167  	# long-lived server is concerned, so the strictest window is the honest one.
 168  	# The window is why the worker's decode had to move inside its request
 169  	# arena: outside, the worker grew 615B a request and chained a 64MB region,
 170  	# which read as +2096kB here while a warmed-up loop read +0.
 171  	s0="$(curl -s -m 10 "http://127.0.0.1:$port/stats" 2>/dev/null)"
 172  	mid_rss="$(printf '%s\n' "$s0" | sed -n 's/^rss_kb=//p' | head -1)"
 173  	lrc=0
 174  	timeout 90 "$td/client" --loop 200 "http://127.0.0.1:$port/smoke" >"$td/loop1-$mode.out" 2>&1 || lrc=$?
 175  	if [ "$lrc" != 0 ]; then
 176  		log_fail "netsmoke:no-accumulation-$mode" "$(tail -3 "$td/loop1-$mode.out" 2>/dev/null)"
 177  	else
 178  		timeout 90 "$td/client" --loop 200 "http://127.0.0.1:$port/smoke" >"$td/loop2-$mode.out" 2>&1 || lrc=$?
 179  		s1="$(curl -s -m 10 "http://127.0.0.1:$port/stats" 2>/dev/null)"
 180  		post_rss="$(printf '%s\n' "$s1" | sed -n 's/^rss_kb=//p' | head -1)"
 181  		if [ "$lrc" != 0 ]; then
 182  			log_fail "netsmoke:no-accumulation-$mode" "$(tail -3 "$td/loop2-$mode.out" 2>/dev/null)"
 183  		elif [ -z "$mid_rss" ] || [ -z "$post_rss" ]; then
 184  			log_fail "netsmoke:no-accumulation-$mode" "no rss (base=[$mid_rss] post=[$post_rss])"
 185  		else
 186  			growth=$((post_rss - mid_rss))
 187  			if [ "$growth" -lt 2048 ]; then
 188  				log_pass "netsmoke:no-accumulation-$mode" "400 exchanges in 2 runs: rss ${mid_rss}kB -> ${post_rss}kB (+${growth}kB)"
 189  			else
 190  				log_fail "netsmoke:no-accumulation-$mode" "requests retain memory: steady-state rss +${growth}kB over 400 exchanges (${mid_rss} -> ${post_rss}kB, ~$((growth * 1024 / 400))B/request) - the request arena is entered and reset per request, so this is the runtime's retention, not the shell's (the work log)"
 191  			fi
 192  		fi
 193  	fi
 194  
 195  	if [ "$mode" = "inline" ]; then
 196  		# The unit above IS the Transport path. This one keeps the single-domain
 197  		# exchange (LookupHost/Dial/Write/ReadResponse with no Transport)
 198  		# alive beside it, so a transport regression and a socket regression
 199  		# cannot be mistaken for each other.
 200  		src=0
 201  		timeout 20 "$td/client" "http://127.0.0.1:$port/smoke" >"$td/client-sync.out" 2>&1 || src=$?
 202  		if [ "$src" = 0 ]; then
 203  			log_pass "netsmoke:client-sync" "single-domain exchange (LookupHost/Dial/Write/ReadResponse) ok"
 204  		else
 205  			log_fail "netsmoke:client-sync" "rc=$src: $(tail -3 "$td/client-sync.out" 2>/dev/null)"
 206  		fi
 207  	fi
 208  
 209  	kill "$pid" 2>/dev/null || true
 210  	wait "$pid" 2>/dev/null || true
 211  }
 212  
 213  run_mode inline
 214  run_mode spawned
 215  
 216  rm -rf "$td"
 217  
 218  # A phase that cannot go red is not a gate: this script's log showed two FAIL
 219  # verdicts (the client transport blocker, which is its own defect) while the
 220  # process exited 0, so the driver recorded a green phase over a red log. Exit
 221  # non-zero when anything failed, the same way phase 3 does.
 222  #
 223  # The count is taken before the test, not inside it: `grep -c` prints 0 AND
 224  # exits 1 when nothing matches, so `$(grep -c ... || echo 0)` produced two lines
 225  # and every run ended with "[: 0\n0: integer expected" on stderr - noise from a
 226  # gate, which is how a real failure message gets ignored. The `|| true` inside
 227  # the substitution is what keeps `set -e` from treating grep's no-match exit as
 228  # the phase failing: the first version of this fix aborted the script (all 17
 229  # checks had PASSed and the phase went red), which the gate caught.
 230  fails=0
 231  if [ -f "$STATUSFILE" ]; then
 232  	fails="$(grep -c '^FAIL' "$STATUSFILE" 2>/dev/null || true)"
 233  fi
 234  if [ "$fails" -gt 0 ]; then
 235  	exit 1
 236  fi
 237