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
latestimage tag withpullPolicy: Never
phase 1: external review identified 3 problems
- probes on :3001 — metrics server is single-threaded.
/metricsscrapes block/_health, kubelet kills pod (exit 137) - stack sizes inconsistent — some threads 2 MiB, some 16 MiB default
- 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-deploytimed 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
- debug build works — zero stack overflows, zero crashes, stable operation
- ReleaseSafe overflows at both 4 MiB and 8 MiB thread stacks
- ReleaseFast has a real memory safety bug — double free after consumer disconnect
- the overflow is in subscriber threads doing TLS handshakes to PDS hosts
nm --size-sortshowscrypto.tls.Client.initis 48,619 bytes of machine code in ReleaseSafeobjdumpshows the largest single stack frame (sub rsp) is ~4,072 bytes- the overflow comes from deep call chains of moderate frames, not one giant frame
- k256 (secp256k1 for AT Protocol signatures) is not involved — TLS uses stdlib secp256r1/x25519
what we now know (2026-03-05 investigation)
- confirmed OOM kill, not stack overflow — dmesg shows
CONSTRAINT_MEMCG,anon-rss: ~3,030 MB - 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
- 2,750 threads × 3.9 MiB = ~10.7 GiB — far beyond any reasonable container limit
- cause: aggressive inlining flattens deep TLS/crypto/CBOR call chains into giant stack frames that touch more memory pages
- throttled startup doesn't help — the per-thread RSS is steady-state, not a transient spike
- Go relay uses goroutines (~few KB initial stack, grows as needed) — no comparable problem
- background startup thread is still useful — decouples HTTP server readiness from worker spawning
what we still don't know
- whether the double-free in ReleaseFast is related to the same code path or something else entirely
- exact breakdown of the 3.9 MiB per thread — how much is stack pages vs thread-local heap
- whether
-fno-inlineornoinlineannotations 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:
- thread pool (f0c7baf) — reader threads no longer do validation, DB persist, or broadcast
- 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 restored6925fa0— probe consolidation, build_info metric1d80d84— metrics server tryLock fix (HEAD at time of 4 MiB stack reduction)
what to do next
investigate (not in prod)
- figure out why
VmHWM/RssAnon/mallinfometrics silently fail - 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.
- investigate the double-free in ReleaseFast — where is the consumer lifecycle bug?
- try
noinlineon hot TLS/crypto paths to reduce ReleaseSafe per-thread RSS - consider filing a zig issue about
tls.Client.initstack 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)
- record current image:
kubectl get deploy -n zlay zlay -o jsonpath='{.spec.template.spec.containers[0].image}' - record current image digest:
ctr -n k8s.io images ls | grep <tag> - build new image, import to containerd
- if changing probe paths: update code AND helm values, deploy image first, then helm
- verify pod 1/1, check
/_healthz, check/metricsforbuild_info - if anything fails:
kubectl set image deployment/zlay -n zlay main=<previous image>
mistakes made during this incident
- presented theories as facts — claimed ReleaseSafe stack inflation was understood and 8 MiB fixed it, when it didn't
- didn't verify long enough — declared success after seeing a pod running for seconds, not minutes
- ran blocking kubectl commands that hung and wasted time
- changed probe paths without changing image atomically — helm deployed new probe config against old image, pod never became ready
- speculated about crypto code without evidence — blamed "crypto safety checks" without knowing which function was involved
- multiple experiments in flight — changed stack sizes, optimization levels, and code simultaneously