Skip to content

When using acks_late, a MemoryError will block the worker pool completely #10462

Closed

Description

@caspervdw

Checklist

  • I have verified that the issue exists against the main branch of Celery.
  • This has already been asked to the discussions forum first.
  • I have read the relevant section in the
    contribution guide
    on reporting bugs.
  • I have checked the issues list
    for similar or identical bug reports.
  • I have checked the pull requests list
    for existing proposed fixes.
  • I have checked the commit log
    to find out if the bug was already fixed in the main branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).
  • I have tried to reproduce the issue with pytest-celery and added the reproduction script below.

Mandatory Debugging Information

  • I have included the output of celery -A proj report in the issue.
    (if you are not able to do this, then at least specify the Celery
    version affected).
  • I have verified that the issue exists against the main branch of Celery.
  • I have included the contents of pip freeze in the issue.
  • I have included all the versions of all the external dependencies required
    to reproduce this bug.

Optional Debugging Information

  • I have tried reproducing the issue on more than one Python version
    and/or implementation.
  • I have tried reproducing the issue on more than one message broker and/or
    result backend.
  • I have tried reproducing the issue on more than one version of the message
    broker and/or result backend.
  • I have tried reproducing the issue on more than one operating system.
  • I have tried reproducing the issue on more than one workers pool.
  • I have tried reproducing the issue with autoscaling, retries,
    ETA/Countdown & rate limits disabled.
  • I have tried reproducing the issue after downgrading
    and/or upgrading Celery and its dependencies.

Related Issues and Possible Duplicates

Related Issues

  • None

Possible Duplicates

  • None

Environment & Settings

Celery version: 5.6.3

celery report Output:

software -> celery:5.6.3 (recovery) kombu:5.6.2 py:3.12.3
            billiard:4.2.4 py-amqp:5.3.1
platform -> system:Linux arch:64bit, ELF
            kernel version:6.8.0-90-generic imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:amqp results:redis://:***


CELERY_BEAT_SCHEDULER: 'django_celery_beat.schedulers.DatabaseScheduler'
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP: True
CELERY_BROKER_TRANSPORT_OPTIONS: {'socket_timeout': 10}
CELERY_BROKER_URL: 'amqp://***/'
CELERY_RESULT_BACKEND: 'redis://:***'
CELERY_RESULT_BACKEND_TRANSPORT_OPTIONS: {'result_chord_ordered': False}
CELERY_RESULT_EXPIRES: 604800
CELERY_TASK_ACKS_LATE: True
CELERY_TASK_DEFAULT_PRIORITY: 0
CELERY_TASK_DEFAULT_QUEUE: 'long'
CELERY_TASK_QUEUE_MAX_PRIORITY: 10
CELERY_TASK_STORE_ERRORS_EVEN_IF_IGNORED: False
CELERY_TASK_TIME_LIMIT: None
CELERY_TASK_TRACK_STARTED: True
CELERY_WORKER_PREFETCH_MULTIPLIER: 1

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: Unknown (probably every Python 3.x version will reproduce)
  • Minimal Celery Version: Unknown (tested with latest)
  • Minimal Kombu Version: Unknown
  • Minimal Broker Version: RabbitMQ 3.x (any version with server-side basic_qos)
  • Minimal Result Backend Version: N/A
  • Minimal OS and/or Kernel Version: Linux with vm.overcommit_memory=0 (heuristic mode) to trigger MemoryError without OOM kill
  • Minimal Broker Client Version: Unknown
  • Minimal Result Backend Client Version: N/A or Unknown

Python Packages

pip freeze Output:

amqp==5.3.1
billiard==4.2.4
celery==5.6.3
kombu==5.6.2
numpy==1.26.4
vine==5.1.0

Other Dependencies

Details

  • RabbitMQ broker (AMQP transport)
  • Linux with vm.overcommit_memory=0
  • Workers started with prefork pool

Minimally Reproducible Test Case

Details

from celery import Celery
import time

app = Celery(
    "reproduce",
    broker="amqp://user:pass@localhost:5672/vhost",
    backend="rpc://",
)
app.conf.update(
    task_acks_late=True,
    worker_prefetch_multiplier=1,
    task_default_queue="reproduce",
)


@app.task
def oom_task():
    """Raise MemoryError to trigger the bug.

    In production this comes from numpy/the kernel denying a too-large
    allocation.  For the reproducing we just raise it directly. The code path
    is identical.
    """
    raise MemoryError("simulated allocation failure")


@app.task
def ping_task():
    """Harmless task used to prove the worker is blocked."""
    return "pong"


oom_task.delay()
time.sleep(5)
ping_task.delay()

# Stable result in RabbitMQ: 1 message not acked, 1 message in queue. Worker is blocked.

Expected Behavior

When a task raises MemoryError (or a subclass like numpy's _ArrayMemoryError) and task_acks_late=True, the task should:

  1. Be marked as FAILURE in the result backend
  2. Have its message acknowledged or rejected with requeue=False
  3. Allow the worker to continue processing other tasks normally

This should behave identically to any other Exception subclass failing a task.

Actual Behavior

The message is never acknowledged, creating a permanent block of the worker which eventually blocks all workers in the cluster.

Root Cause Analysis

The issue is in celery/worker/request.py lines 601-602:

The raise MemoryError(...) exits the function before self.acknowledge() is ever reached.

Detailed Code Path (prefork pool, acks_late=True)

Step 1: Child process handles the MemoryError correctly

In the child process, celery/app/trace.py line 604 catches MemoryError as Exception (MemoryError is a subclass of Exception):

on_error → handle_failure stores the FAILURE in the result backend and returns normally. The child sends (True, (1, ExceptionInfo, T)) back to the parent indicating "function returned, but the task failed".

Step 2: Parent process receives the result

In billiard/pool.py _set(), the result is marked as a success (the function call itself didn't crash), so the _callback (Request.on_success) is called.

Step 3: on_success detects the task failure

request.py on_success() unpacks failed=1, unwraps the ExceptionWithTraceback to get the original MemoryError, and calls self.on_failure(retval, return_ok=True).

Step 4: on_failure raises instead of acknowledging

request.py line 601:

This raise occurs before the acknowledge code at line 624:

Step 5: billiard swallows the exception

billiard/pool.py safe_apply_callback() catches the raised MemoryError:

Since _callbacks_propagate is () and MemoryError is an Exception subclass, the exception is logged and swallowed. Execution continues, but the message was never acknowledged.

Step 6: Poison pill

The unacknowledged message permanently occupies one of RabbitMQ's server-side prefetch slots (per basic_qos). With worker_prefetch_multiplier=1, only 1 unacked messages is allowed. Each MemoryError failure permanently consumes one slot.

On worker restart, RabbitMQ redelivers all unacked messages. The poison-pill message fails the same way again, blocking slots on whichever worker receives it. Over time, this degrades the entire cluster.

Proposed Fix

The isinstance(exc, MemoryError) check in on_failure was likely written when MemoryError was not a subclass of Exception (Python 2) and the raise would propagate past except Exception. In Python 3, MemoryError IS Exception, so:

  1. The original intent (crash the pool) is defeated; billiard catches and swallows it.
  2. The side effect (skipping acknowledge) creates a permanent poison pill.

The fix should either:

  • Remove the special-case entirely: let MemoryError follow the normal failure path (acknowledge the message, store failure). The child process already handled it safely.
  • Or: acknowledge the message before raising, so the poison-pill scenario cannot occur.

Activity

  1. DresdenGman commented on Aug 10, 2026

    @DresdenGman

    "This appears to be a distinct high-severity failure mode from #10456: with acks_late=True, the special MemoryError path can leave the message unacknowledged and consume the worker's prefetch slot indefinitely. The reproduction is self-contained. Would maintainers prefer a focused regression test around Request.on_failure, or should this be coordinated with billiard before a Celery patch?"

  2. auvipy commented on Aug 10, 2026

    @auvipy
    Member

    a billiard patch, followed up by regression test around Request.on_failure

  3. sahilmathur254 commented on Aug 11, 2026

    @sahilmathur254
    Contributor

    Thanks, the billiard-first split makes sense. I can handle this as two focused milestones: first, add a billiard regression for the callback path involving MemoryError and make the minimal lifecycle fix there; then add Celery regression coverage around Request.on_failure once that behaviour is available. @DresdenGman , are you already working on either patch? @auvipy , should the PR target billiard’s main branch, with the Celery change limited to regression coverage after the dependency fix lands?

  4. DresdenGman commented on Aug 13, 2026

    @DresdenGman

    I’m not currently working on either patch, so please go ahead with the billiard-first split. Thanks for checking.

  5. added this to the 5.7.0 milestone on Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions