# zlay stability incident — 2026-03-04 ## timeline ### starting state - zlay running at zlay.waow.tech, ~2,780 PDS hosts connected - ReleaseSafe binary, 2 MiB thread stacks (some threads), 16 MiB default - probes on :3001 (single-threaded metrics server) - mutable `latest` image tag with `pullPolicy: Never` ### phase 1: external review identified 3 problems 1. **probes on :3001** — metrics server is single-threaded. `/metrics` scrapes block `/_health`, kubelet kills pod (exit 137) 2. **stack sizes inconsistent** — some threads 2 MiB, some 16 MiB default 3. **mutable image tags** — `latest` + `pullPolicy: Never`, no way to know what's running or roll back ### phase 2: fixes applied - moved probes to :3000 (concurrent websocket server) - unified all thread stacks to 4 MiB - switched to git SHA image tags + `kubectl set image` ### phase 3: stack overflow traces appeared - after deploying with 4 MiB stacks + ReleaseSafe, pod emitted millions of `???:?:?: 0x...` lines - zero actual log output, process non-functional - pod kept running (probes on :3000 still passed) but completely useless ### phase 4: experiments (what we tried) | experiment | result | |---|---| | reboot server (libc6 updated, `System restart required`) | no effect on overflow | | debug build (no `-Doptimize`) | **worked** — zero overflow, clean logs | | ReleaseFast (`-Doptimize=ReleaseFast`) | zero overflow, but `double free or corruption (out)` crash after consumer disconnect | | ReleaseSafe + 8 MiB stacks | appeared to work briefly, then overflowed again on next deploy | | ReleaseSafe + 8 MiB stacks (second attempt, rollback) | overflowed immediately, 0/1 Running with restarts | ### phase 5: stabilized on debug build - debug build (`debug-8a0fb1f`) deployed — 1/1 Running, 0 restarts, metrics working - this is the current running state ### phase 6: code changes for observability (committed, not yet deployed) - `/_healthz` (trivial liveness, no DB) and `/_readyz` (DB dependency check) - `build_info{git_sha,optimize}` canary metric - all thread stack sizes consolidated to `main.default_stack_size` - helm values updated: liveness on `/_healthz`, readiness on `/_readyz`, raised tolerances ### phase 7: helm deploy failed - `just zlay-deploy` timed out because helm restarted the pod with new probe paths (`/_healthz`) but the running image (`debug-8a0fb1f`) doesn't have that endpoint yet - the new image build (`debug-6925fa0`) was still compiling during the helm upgrade - **lesson: never change probe paths and image in separate operations** ## what we know for certain 1. **debug build works** — zero stack overflows, zero crashes, stable operation 2. **ReleaseSafe overflows** at both 4 MiB and 8 MiB thread stacks 3. **ReleaseFast has a real memory safety bug** — double free after consumer disconnect 4. the overflow is in subscriber threads doing TLS handshakes to PDS hosts 5. `nm --size-sort` shows `crypto.tls.Client.init` is 48,619 bytes of machine code in ReleaseSafe 6. `objdump` shows the largest single stack frame (`sub rsp`) is ~4,072 bytes 7. the overflow comes from **deep call chains of moderate frames**, not one giant frame 8. k256 (secp256k1 for AT Protocol signatures) is **not involved** — TLS uses stdlib secp256r1/x25519 ## what we now know (2026-03-05 investigation) 1. **confirmed OOM kill, not stack overflow** — dmesg shows `CONSTRAINT_MEMCG`, `anon-rss: ~3,030 MB` 2. **ReleaseSafe uses ~3.9 MiB RSS per thread** vs ~0.4 MiB in debug (10x difference) - measured: ~875 threads spawned, 4 GiB RSS → OOM - debug: 2,671 threads, 1.6 GiB RSS → stable 3. **2,750 threads × 3.9 MiB = ~10.7 GiB** — far beyond any reasonable container limit 4. **cause: aggressive inlining** flattens deep TLS/crypto/CBOR call chains into giant stack frames that touch more memory pages 5. **throttled startup doesn't help** — the per-thread RSS is steady-state, not a transient spike 6. **Go relay uses goroutines** (~few KB initial stack, grows as needed) — no comparable problem 7. **background startup thread is still useful** — decouples HTTP server readiness from worker spawning ## what we still don't know 1. **whether the double-free in ReleaseFast is related** to the same code path or something else entirely 2. **exact breakdown** of the 3.9 MiB per thread — how much is stack pages vs thread-local heap 3. **whether `-fno-inline` or `noinline` annotations** could reduce per-thread RSS enough ## current state (2026-03-05) - **optimize**: ReleaseSafe — deployed successfully, ~1.1 GiB RSS at ~2,255 hosts - **thread stacks**: 8 MiB (supports ReleaseSafe handshake path; only touched pages count as RSS) - **probes**: liveness `/_healthz:3000`, readiness `/_readyz:3000` (helm synced) - **helm**: values applied via `helm upgrade`, no longer drifted from kubectl patches - **reconnect cronjob**: deployed — reconciles PDS host list every 4 hours - **grafana dashboard**: configmap synced from `deploy/zlay-dashboard.json` - **metrics**: working — RSS, threads, disk, counters, caches all emitting ## ReleaseSafe readiness analysis (2026-03-05) two changes since the incident should fix the per-thread RSS problem: 1. **thread pool** (f0c7baf) — reader threads no longer do validation, DB persist, or broadcast 2. **migration queue dedup** (37fa194) — eliminated unbounded heap growth ### symbol-level evidence cross-compiled x86_64-linux-gnu binaries, compared via `nm --size-sort` and `objdump`: **binary sizes**: Debug 313M, ReleaseSafe 143M, ReleaseSmall 8M **reader thread stack frames (ReleaseSafe)** — the per-frame hot path: | function | stack frame | |---|---| | `proto.Reader.read` | 360 bytes | | `Io.Reader.fillUnbuffered` | 48 bytes | | `tls.Client.readVec` | 24 bytes | | `tls.Client.readIndirect` | 2,264 bytes | | **total read chain** | **~2.7 KiB** | **TLS handshake (once per connection, not per frame)**: | function | stack frame | |---|---| | `tls.Client.init` | 84 KiB (0x15058) | | `tls.Client.KeyShare.init` | 48 KiB (0xc208) | | **total handshake** | **~134 KiB** | the largest static stack frame in ReleaseSafe outside of RocksDB C++ init is 4,072 bytes (`crypto.Certificate.rsa.encrypt`). all RocksDB frames (53 KiB, 41 KiB) are in C++ global init, not on reader threads. ### why 3.9 MiB/thread should not recur the old architecture had reader threads doing: TLS read → CBOR decode → ECDSA verify → DB persist → broadcast. each step involved heap allocations and deep call stacks. the new architecture: TLS read → CBOR header peek → dupe → queue to pool. the pool's 16 workers do all the heavy lifting on shared threads, not per-PDS threads. ### ReleaseSmall as fallback ReleaseSmall produces an 8M binary (vs 143M ReleaseSafe) by avoiding inlining entirely. however, it **disables runtime safety checks** — same risk category as ReleaseFast, which has a known double-free. only use if ReleaseSafe RSS is still too high. ## notable commits see `git log` for full history. key milestones: - `952de0c` — first 4 MiB stack attempt (ReleaseSafe, overflowed) - `8a0fb1f` — prometheus metrics restored - `6925fa0` — probe consolidation, build_info metric - `1d80d84` — metrics server tryLock fix (HEAD at time of 4 MiB stack reduction) ## what to do next ### investigate (not in prod) 1. figure out why `VmHWM`/`RssAnon`/`mallinfo` metrics silently fail 2. **thread pool architecture** — **done** (commit f0c7baf). reader threads (one per PDS) are now lightweight; heavy work (CBOR decode, validation, persist, broadcast) runs on a shared frame pool (default 16 workers). this doesn't reduce thread count but enables a path to ReleaseSafe — reader stacks no longer need space for crypto/DB call chains. 3. investigate the double-free in ReleaseFast — where is the consumer lifecycle bug? 4. try `noinline` on hot TLS/crypto paths to reduce ReleaseSafe per-thread RSS 5. consider filing a zig issue about `tls.Client.init` stack usage in ReleaseSafe ### process - **no concurrent config/code changes during a deploy** - **probe paths and image must change atomically** (same deploy) - **always record image digest + git SHA** before deploying - **debug build is fine for now** — binary is larger and slower but correct - **12-16h baseline run** with fixed config before any new experiments ## deploy checklist (for future deploys) 1. record current image: `kubectl get deploy -n zlay zlay -o jsonpath='{.spec.template.spec.containers[0].image}'` 2. record current image digest: `ctr -n k8s.io images ls | grep ` 3. build new image, import to containerd 4. if changing probe paths: update code AND helm values, deploy image first, then helm 5. verify pod 1/1, check `/_healthz`, check `/metrics` for `build_info` 6. if anything fails: `kubectl set image deployment/zlay -n zlay main=` ## mistakes made during this incident 1. **presented theories as facts** — claimed ReleaseSafe stack inflation was understood and 8 MiB fixed it, when it didn't 2. **didn't verify long enough** — declared success after seeing a pod running for seconds, not minutes 3. **ran blocking kubectl commands** that hung and wasted time 4. **changed probe paths without changing image atomically** — helm deployed new probe config against old image, pod never became ready 5. **speculated about crypto code without evidence** — blamed "crypto safety checks" without knowing which function was involved 6. **multiple experiments in flight** — changed stack sizes, optimization levels, and code simultaneously