Skip to content

Worker memory during large imports

Why this document exists

A full WordPress import used to make the forge host unreachable. Roughly fifteen minutes into a run the box stopped answering SSH and HTTP, and only a reboot recovered it. Every conventional signal looked healthy at the time:

  • CPU near idle (~15% of 8 cores) — processes waiting, not computing
  • EC2 instance and system status checks green
  • no OOM kill in the kernel log — the box thrashes and stops scheduling first
  • disk queue and DB connections normal

Because of that, four separate hypotheses were investigated and rejected before the cause was found. Each was a real problem and was fixed on its own merits, but none of them was the wedge:

candidate outcome
EBS IOPS starvation real (gp2/8 GB = 100 baseline IOPS, queue depth 5.86); root volume moved to gp3/3000
DB connection pooling real; bounded explicitly (was SQLAlchemy's default 5+10 per process across 8 workers)
Celery task payload size not the cause — article task params measure ~220 bytes
decode megapixels capped at 24M

The cause was worker RSS. A vitals recorder left running on the host caught two Celery processes holding 3.4 GB and 4.5 GB, and one later at 6.1 GB, against a decode ceiling that should cap a single image near 100 MB.

The lesson worth keeping: on this host, "no errors and rising row counts" carries almost no information about health. The wedge was only ever visible by sampling process RSS on the box.

What bounds memory now

  • worker_max_memory_per_child — recycles a forked child once its RSS crosses the ceiling. Note this applies to children only; it never touches the parent.
  • worker_max_tasks_per_child — belt-and-braces bound for growth that never trips the memory check.
  • worker_prefetch_multiplier = 1 — the parent buffers prefetched messages for the whole pool across 14 queues, and the default multiplier scales that with concurrency. The parent is the process nothing recycles.
  • _MAX_PIXELS (24M) — bounds the decoded surface.
  • _MAX_SOURCE_BYTES (40 MB) — bounds the compressed bytes, which are resident before any pixel guard applies.
  • Image.draft() — decodes a JPEG at a reduced scale that still covers the target rather than decoding full-size and discarding, cutting peak memory on the common case by roughly an order of magnitude.

Profiling

Allocation profiling is opt-in and off by default (tracemalloc roughly doubles allocation cost):

docker exec -e FORGE_PROFILE_IMAGE_MEMORY=1 cms-ai-forge_forge-worker_1 ...

It logs the top retained allocation sites plus the PID and the process role (MainProcess vs ForkPoolWorker-N). The role matters: growth attributed to MainProcess is growth that worker_max_memory_per_child cannot reclaim.

The host watchdog (removed)

While the cause was unknown, a systemd unit forge-memwatch restarted the worker whenever available memory fell below 2.5 GB, plus a forge-vitals recorder sampling RSS every 10s. They kept the host alive but were never a fix:

  • forge-memwatch fired 63 times during one import
  • each restart drops in-flight work: an article whose image fan-out is interrupted loses its chord, so finalize_article_assets never runs and the row publishes with uncopied images
  • 4 of 3,219 rows needed manual repair for exactly that reason

Both units are removed. If you find them running on a host, they are leftovers:

sudo systemctl stop forge-memwatch forge-vitals
sudo rm -f /usr/local/bin/forge-vitals.sh /tmp/rec2.sh /tmp/memwatch.sh

The interrupted-fan-out damage is now guarded independently of memory by sweep_unfinalized_articles (see retry_beat), which finalizes any article that published without its media merge ever running.

Verifying a change here

Import a few hundred posts with no watchdog running and sample the host:

ps -eo rss,comm --sort=-rss | head -3

The largest celery RSS must stay bounded and must not trend upward across the run. Before this work the same workload reached 3.4–6.1 GB.