#!/usr/bin/env bash set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) app="zds-repro-$(date +%s)" region="${ZDS_REPRO_REGION:-ord}" out="${ZDS_REPRO_RESULTS:-$root/bench/results/$app}" image=registry.fly.io/zds-pds@sha256:1fe3cf3c3a1dda09cade713163206eec35b4e05a6880b1226d8f1df197e9acdd driver_image=ghcr.io/astral-sh/uv@sha256:847b5e690018bc6b9d97a0848da65f721b785f1e78d9c7067b8947c7010b2718 mode="${ZDS_REPRO_MODE:-pressure}" if [[ "$mode" != pressure && "$mode" != half-close ]]; then printf 'ZDS_REPRO_MODE must be pressure or half-close\n' >&2 exit 2 fi target="" created=false mkdir -p "$out" cleanup() { status=$? trap - EXIT if [[ -n "$target" ]]; then flyctl ssh console --app "$app" --machine "$target" -C 'cat /data/repro-resources.log' > "$out/resources.log" 2> "$out/resources.stderr" || true flyctl logs --app "$app" --no-tail --json > "$out/logs.json" 2> "$out/logs.stderr" || true fi if [[ "$created" == true ]]; then if ! flyctl apps destroy "$app" --yes; then printf 'cleanup failed: destroy disposable app %s\n' "$app" >&2 status=1 fi fi printf 'reproduction results: %s\n' "$out" exit "$status" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM flyctl apps create "$app" --org personal --yes created=true flyctl ips allocate-v4 --app "$app" --shared --yes flyctl volumes create repro_data --app "$app" --region "$region" --size 1 --scheduled-snapshots=false --yes --json > "$out/volume.json" volume=$(jq -r 'if type == "array" then .[0].id else .id end' "$out/volume.json") flyctl machine run "$image" --app "$app" --name target --region "$region" \ --vm-cpu-kind shared --vm-cpus 4 --vm-memory 1024 --restart no --autostop=off \ --volume "$volume:/data" --port 443:2583/tcp:http:tls \ --file-local "/app/target.sh=$root/tools/repro-export-target.sh" --entrypoint /bin/bash \ --env ZDS_HOST=0.0.0.0 --env ZDS_PORT=2583 --env ZDS_DB=/data/zds.sqlite3 \ --env ZDS_BLOBSTORE_PATH=/data/blobs --env "ZDS_PUBLIC_URL=https://$app.fly.dev" \ --env "ZDS_SERVER_DID=did:web:$app.fly.dev" --env ZDS_CRAWLERS= \ --env ZDS_INVITE_REQUIRED=true --env ZDS_JWT_SECRET=disposable-repro-only \ --env ZDS_BLOB_UPLOAD_LIMIT=100000000 --detach /app/target.sh target=$(flyctl machines list --app "$app" --json | jq -er '.[] | select(.name == "target") | .id') flyctl machine run "$driver_image" --app "$app" --name driver --region "$region" \ --vm-cpu-kind shared --vm-cpus 4 --vm-memory 1024 --restart no \ --file-local "/app/repro.py=$root/tools/repro-export-pressure.py" --entrypoint /bin/sleep --detach 86400 driver=$(flyctl machines list --app "$app" --json | jq -er '.[] | select(.name == "driver") | .id') flyctl ssh console --app "$app" --machine "$driver" \ -C "uv run /app/repro.py https://$app.fly.dev --seed --records ${ZDS_REPRO_RECORDS:-50000}" | tee "$out/seed.jsonl" flyctl machine update "$target" --app "$app" --vm-cpu-kind shared --vm-cpus 1 --vm-memory 1024 --yes flyctl machines list --app "$app" --json > "$out/machines.json" if [[ "$mode" == half-close ]]; then zig cc -target x86_64-linux-musl -O2 -Wall -Wextra -Werror \ "$root/tools/repro-export-half-close.c" -o "$out/half-close" flyctl ssh sftp put "$out/half-close" /app/half-close --mode 0755 --app "$app" --machine "$target" flyctl ssh console --app "$app" --machine "$driver" \ -C "uv run /app/repro.py https://$app.fly.dev --seconds 110 --exporters 0" > "$out/probes.jsonl" & probe_pid=$! flyctl ssh console --app "$app" --machine "$target" \ -C 'sh -c "date -u; /app/half-close 0 20; date -u; sleep 10; /app/half-close 1 75; date -u; sleep 10"' | tee "$out/half-close.log" wait "$probe_pid" printf 'single-connection reproduction captured; compare CPU samples with phase timestamps\n' exit 0 fi flyctl ssh console --app "$app" --machine "$driver" \ -C "uv run /app/repro.py https://$app.fly.dev --seconds ${ZDS_REPRO_CONTROL_SECONDS:-60} --interval 10" | tee "$out/single.jsonl" flyctl ssh console --app "$app" --machine "$driver" \ -C "uv run /app/repro.py https://$app.fly.dev --seconds ${ZDS_REPRO_SECONDS:-360} --interval 10 --exporters 4 --homepage-interval 30" | tee "$out/overlap.jsonl" flyctl ssh console --app "$app" --machine "$driver" \ -C "uv run /app/repro.py https://$app.fly.dev --seconds 60 --exporters 0 --homepage-interval 30" | tee "$out/recovery.jsonl" jq -e -s 'all(.[]; if (.phase == "read_summary" or .phase == "health_summary") then .failures == 0 else true end)' "$out/single.jsonl" >/dev/null if ! jq -e -s 'any(.[]; .phase == "read_summary" and .failures > 0) and any(.[]; .phase == "health_summary" and .failures > 0)' "$out/overlap.jsonl" >/dev/null; then printf 'request-stall reproduction did not occur; inspect the saved measurements\n' >&2 exit 1 fi printf 'reproduced: record reads and health checks failed under overlapping exports\n'