Memory grows unbounded when Postgres is unreachable (RecordErrorBatch retry loop)
Summary
When Postgres is unreachable or extremely slow, sub2api accumulates host memory (observed: ~5 GiB of Shmem over a few hours under sustained pq: the database system is not yet accepting connections) without ever releasing it. The container's cgroup --memory= limit did not catch this in our setup, taking down the entire host with OOM pressure.
Environment
- Image:
weishaw/sub2api(Unraid Docker, pulled 2026-05) - Container limits:
--restart=unless-stopped --memory=1536m --memory-swap=1536m --pids-limit=512 - Host: Unraid 7.x on Linux 6.x, cgroup v2
- Postgres: running in a separate container; became slow/unresponsive due to a parallel I/O storm (parity check)
Symptoms
The following errors flooded the log continuously (every few hundred ms):
ERROR stdlog handler/ops_error_logger.go:184 [Ops] RecordErrorBatch single insert failed: context deadline exceeded
ERROR service/scheduler_snapshot_service.go:247 [Scheduler] outbox poll failed: context deadline exceeded
ERROR service/scheduler_snapshot_service.go:556 [Scheduler] rebuild failed: bucket=4:antigravity:forced reason=interval err=context deadline exceeded
ERROR service/scheduler_snapshot_service.go:224 [Scheduler] full rebuild failed: pq: the database system is not yet accepting connections
ERROR service/subscription_expiry_service.go:56 [SubscriptionExpiry] Update expired subscriptions failed: context deadline exceeded
ERROR service/idempotency_cleanup_service.go:85 [IdempotencyCleanup] cleanup failed err=context deadline exceeded
WARN msg="ops system log sink flush failed" err=context deadline exceeded batch=5
Memory growth was not visible via docker stats (showed normal ~200 MiB RSS) but /proc/meminfo:Shmem rose ~5 GiB over the period. When the container was stopped, Shmem immediately dropped by the same amount, freeing 7.5 GiB of host RAM in one syscall.
Also observed: 119 zombie curl processes accumulated under PID 1 over ~5 hours — these are the docker healthcheck (curl -f http://localhost:8080/health) invocations that timed out. The container did not have --init, so the Go binary as PID 1 never reaped them. This is a related but separable issue.
Suspected root cause
handler/ops_error_logger.go RecordErrorBatch looks like it batches failed-write entries and retries them. When the DB has been unreachable for hours, this batch (or whatever buffer it spills into) appears to grow unbounded rather than shedding old entries or applying back-pressure.
Suggested mitigations
- Bound the in-memory error batch — drop oldest when batch exceeds N entries; emit a counter for dropped entries.
- Apply circuit-breaker behavior to all DB-dependent worker loops (scheduler outbox, full rebuild, subscription expiry, idempotency cleanup) when Postgres is unreachable for > T seconds — back off exponentially and stop generating new work.
- Decouple
/healthfrom any DB query so liveness probes don't cascade-fail. A separate/readyendpoint can check DB readiness. - Document the
--initrequirement in the README example (or ship an entrypoint that PID-1-reaps), since the default healthcheck design relies on exec-spawnedcurland will leak zombies without an init process.
Repro
- Run sub2api with Postgres available; allow normal startup.
- Stop or freeze the Postgres container.
- Observe sub2api's host
Shmem(grep Shmem /proc/meminfo) and error log volume over ~30 min. Shmemgrows without bound; containermemory.currentmay underreport it depending on kernel.
Happy to provide additional logs, memory profiles, or strace output if useful.