Skip to content

Stop or delete an asynchronous task within a celery task #9402

@Fianax

Description

@Fianax

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.4.0

celery report Output:

Steps to Reproduce

I have a task like this:

@celery_app.task
def long_task_parse(documents: list[str], invalid_file_names: list[str]):
    """
    """
    logger.info("long_task called")
    content = asyncio.run(long_async_task_parse(documents, invalid_file_names))
    return {'result': content}

and when I try to stop the task, following the documentation, it does not stop.
In fact, whatever is running inside the asyncio.run() does not stop.

How can I stop the celery task to stop what I run in the asyncio.run()?
Or at least stop both or just what is inside the asyncio.run()?

Required Dependencies

  • Minimal Python Version: 3.12.3
  • Minimal Celery Version: 5.4.0
  • Minimal Kombu Version: N/A or Unknown
  • Minimal Broker Version: N/A or Unknown
  • Minimal Result Backend Version: N/A or Unknown
  • Minimal OS and/or Kernel Version: N/A or Unknown
  • Minimal Broker Client Version: N/A or Unknown
  • Minimal Result Backend Client Version: N/A or Unknown

Python Packages

pip freeze Output:

Other Dependencies

N/A

Minimally Reproducible Test Case

To reproduce it, just create a celery task like the one above and execute a method in asyncio.run() that takes several seconds, have that method print something at the end or create a file (something to know it is finished), launch the celery task and stop it.

Expected Behavior

that stopping the celery task stops the execution of asyncio.run().

Actual Behavior

execution of an asyncio.run() inside a celery task, does not stop when trying to stop the celery task

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions