How to Fix WorkerLostError: Worker exited prematurely (OOM killed or SIGKILL) (Celery AI Workers)

Quick Answer: The Celery worker process was abruptly terminated by the operating system, most commonly due to running out of memory (OOM Killer) while processing heavy AI payloads or models. The fastest fix is to monitor memory usage during task execution, lower concurrency limits, or increase container/node memory limits.

What Causes This Error

Step-by-Step Fixes

Fix 1: Fix 1: Reduce Worker Concurrency

Check your current Celery worker startup command for the concurrency flag (-c).,Lower the concurrency value (e.g., set `-c 1` or `-c 2`) to ensure each worker process has access to sufficient RAM.,Restart the Celery worker service with the updated configuration.

Fix 2: Fix 2: Increase Container or Host Memory Limits

Review the memory allocation limits in your deployment configuration (Docker, Kubernetes, AWS ECS, etc.).,Scale up the container memory limit or move the AI worker to a host instance type with more RAM.,Redeploy the Celery AI workers with the new resource allocations.

Fix 3: Fix 3: Implement Task Memory Management and Offloading

Optimize AI model loading by sharing model instances or using lazy loading where appropriate.,Explicitly clear GPU and CPU memory caches (e.g., `torch.cuda.empty_cache()`) at the end of heavy task execution.,Batch incoming large datasets into smaller chunks to prevent memory spikes.

Advanced Fixes

Advanced Fix 1: Advanced: Configure Celery Max Tasks Per Child

Add the `--max-tasks-per-child` flag to your Celery worker command (e.g., `--max-tasks-per-child=50`).,This forces the worker process to recycle after a set number of tasks, effectively preventing slow memory leaks from crashing the system permanently.

Advanced Fix 2: Advanced: Kernel-Level OOM and System Log Analysis

Inspect system logs using `dmesg -T | grep -i oom` or check Kubernetes pod termination reasons via `kubectl describe pod` to confirm OOM kills.,Tune system swap space or cgroup memory thresholds if running on bare metal infrastructure.

FAQs

Q: How do I know if my Celery worker was specifically killed by the OOM killer?

A: You can check system logs using `dmesg` on Linux. Look for entries like 'Out of memory: Kill process [PID] (python) score [X] or sacrifice child'.

Q: Why does this error happen more frequently with AI and Machine Learning workloads?

A: AI workloads often involve loading massive weights, tensors, and datasets into RAM/VRAM. A sudden spike during matrix multiplication or batch processing can easily exhaust available system memory, triggering an immediate OS kill signal.