Skip to content

Terminate job implementation for gevent concurrency backend - #9083

Merged
Nusnus merged 20 commits into
celery:mainfrom
ldsink:gevent_terminate
Aug 6, 2024
Merged

Nusnus merged 20 commits into
celery:mainfrom
ldsink:gevent_terminate

Conversation

@ldsink

@ldsink ldsink commented Jun 19, 2024

Copy link
Copy Markdown
Contributor

Description

Modify gevent.py to support terminate job. Ref: #6917 .

Fix: #4019 #8687

@Nusnus
Nusnus self-requested a review June 19, 2024 12:08

@Nusnus Nusnus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flake8...................................................................Failed
- hook id: flake8
- exit code: 1

celery/concurrency/gevent.py:121:32: E128 continuation line under-indented for visual indent
celery/concurrency/gevent.py:122:32: E128 continuation line under-indented for visual indent
celery/concurrency/gevent.py:123:32: E128 continuation line under-indented for visual indent

@Nusnus Nusnus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024-06-19T12:10:02.3834851Z =========================== short test summary info ============================
2024-06-19T12:10:02.3835935Z FAILED t/unit/concurrency/test_gevent.py::test_TaskPool::test_terminate_job - AssertionError: Expected 'kill' to have been called once. Called 0 times.
2024-06-19T12:10:02.3837204Z FAILED t/unit/concurrency/test_gevent.py::test_apply_timeout::test_apply_timeout - TypeError: 'NoneType' object is not callable
2024-06-19T12:10:02.3838160Z = 2 failed, 3311 passed, 10 skipped, 3 xfailed, 59 warnings, 28910 subtests passed in 142.61s (0:02:22) =

@ldsink ldsink changed the title Terminate job implementation for gevent concurrency backend WIP Terminate job implementation for gevent concurrency backend Jun 20, 2024
@ldsink ldsink changed the title WIP Terminate job implementation for gevent concurrency backend Terminate job implementation for gevent concurrency backend Jun 20, 2024
@codecov

codecov Bot commented Jun 20, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 87.87879% with 4 lines in your changes missing coverage. Please review.

Project coverage is 78.04%. Comparing base (97b2d1a) to head (be44a7b).
Report is 120 commits behind head on main.

Files with missing lines Patch % Lines
celery/concurrency/gevent.py 87.87% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9083      +/-   ##
==========================================
+ Coverage   78.03%   78.04%   +0.01%     
==========================================
  Files         150      150              
  Lines       18750    18781      +31     
  Branches     3207     3211       +4     
==========================================
+ Hits        14631    14658      +27     
- Misses       3819     3821       +2     
- Partials      300      302       +2     
Flag Coverage Δ
unittests 78.02% <87.87%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ldsink
ldsink requested a review from Nusnus June 24, 2024 02:22
@auvipy
auvipy self-requested a review July 4, 2024 10:32
@ldsink

ldsink commented Jul 5, 2024

Copy link
Copy Markdown
Contributor Author

Do I need to do anything else? The current modifications are working well in our project.

@Nusnus

Nusnus commented Jul 8, 2024

Copy link
Copy Markdown
Member

Do I need to do anything else? The current modifications are working well in our project.

This PR looks good, and it appears that many people are requesting this change, so I’m pro moving forward.
That being said, unfortunately the smoke-tests still don’t support gevent out of the box, so, is there a way you can prove this is working?

Can you provide a worker log that shows the behavior of the changes? @ldsink

Ideally, it would be a log from main and a log with the PR changes, showing how it behaves now compared to how the worker would behave post-merge.

This can accelerate the merge + increase confidence in the changes, although they do seem correct and have tests.

Thank you 🙏

@Nusnus Nusnus added this to the 5.5 milestone Jul 8, 2024
@ldsink

ldsink commented Jul 9, 2024

Copy link
Copy Markdown
Contributor Author

Define a test task and use gevent as celery concurrency pool.

@celery_app.task
def test_task():  
    print("Enter task...")  
    time.sleep(10)  # sleep 10 seconds here  
    print("Task End.")

Start celery worker as

pipenv run celery --app=app worker --events -P gevent --concurrency=10

Cancel task with terminate=True

task = test_task.delay()  
time.sleep(0.1)  
celery_app.control.revoke(task.id, terminate=True)

Before

Worker will throw an exception and has no effect, the task still executed until the end.
terminate not working as the doc described, not support gevent.
Worker log will print Task End.

Enter task...  
[2024-07-09 16:13:08,099] ERROR  : pidbox command error: NotImplementedError("<class 'celery.concurrency.gevent.TaskPool'> doe  
s not implement kill_job")  
Traceback (most recent call last):  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/kombu/pidbox.py", line 102, in dis  
patch  
   reply = handle(method, arguments)  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/kombu/pidbox.py", line 124, in han  
dle_cast  
   return self.handle(method, arguments)  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/kombu/pidbox.py", line 118, in han  
dle  
   return self.handlers[method](self.state, **arguments)  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/celery/worker/control.py", line 14  
9, in revoke  
   task_ids = _revoke(state, task_ids, terminate, signal, **kwargs)  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/celery/worker/control.py", line 22  
4, in _revoke  
   request.terminate(state.consumer.pool, signal=signum)  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/celery/worker/request.py", line 41  
6, in terminate  
   pool.terminate_job(self.worker_pid, signal)  
 File "/home/jammy/.local/share/virtualenvs/project-9U3MxAWf/lib/python3.10/site-packages/celery/concurrency/base.py", line  
113, in terminate_job  
   raise NotImplementedError(  
NotImplementedError: <class 'celery.concurrency.gevent.TaskPool'> does not implement kill_job  
Task End.

After patch

Worker only print enter task and successfully terminate the task.

Worker console log

Enter task...

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have an integration test as well, please?

Comment thread celery/concurrency/gevent.py Resolved
@Nusnus
Nusnus merged commit 6560531 into celery:main Aug 6, 2024
@ldsink
ldsink deleted the gevent_terminate branch August 7, 2024 03:14
@dwick

dwick commented Sep 11, 2024

Copy link
Copy Markdown

While this has fixed NotImplementedError: <class 'celery.concurrency.gevent.TaskPool'> does not implement kill_job I'm seeing another error still:

[00] [01] [2024-09-06 17:03:23,454: ERROR/MainProcess] pidbox command error: AttributeError("'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate'")
[00] [01]     task_ids = _revoke(state, task_ids, terminate, signal, **kwargs)
[00] [01]     request.terminate(state.consumer.pool, signal=signum)
[00] [01]   File "/usr/local/lib/venv/lib/python3.12/site-packages/celery/worker/request.py", line 423, in terminate
[00] [01]     obj.terminate(signal)
[00] [01] AttributeError: 'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate'

Is this related? I think maybe something was missed in this PR. I'm calling AsyncResult().revoke(terminate=True, signal="SIGKILL")

@ldsink

ldsink commented Oct 9, 2024

Copy link
Copy Markdown
Contributor Author

While this has fixed NotImplementedError: <class 'celery.concurrency.gevent.TaskPool'> does not implement kill_job I'm seeing another error still:

[00] [01] [2024-09-06 17:03:23,454: ERROR/MainProcess] pidbox command error: AttributeError("'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate'")
[00] [01]     task_ids = _revoke(state, task_ids, terminate, signal, **kwargs)
[00] [01]     request.terminate(state.consumer.pool, signal=signum)
[00] [01]   File "/usr/local/lib/venv/lib/python3.12/site-packages/celery/worker/request.py", line 423, in terminate
[00] [01]     obj.terminate(signal)
[00] [01] AttributeError: 'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate'

Is this related? I think maybe something was missed in this PR. I'm calling AsyncResult().revoke(terminate=True, signal="SIGKILL")

Try use app.control.revoke as my example code ?
https://docs.celeryq.dev/en/stable/userguide/workers.html#worker-persistent-revokes

celery_app.control.revoke(task.id, terminate=True)

@woutdenolf

Copy link
Copy Markdown
Contributor

I see the same AttributeError: 'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate' when terminating a task from flower.

This could solve the issue

import types


class TaskPool(base.BasePool):
    """GEvent Pool."""

    def on_apply(self, target, args=None, kwargs=None, callback=None,
                 accept_callback=None, timeout=None,
                 timeout_callback=None, apply_target=base.apply_target, **_):
        timeout = self.timeout if timeout is None else timeout
        greenlet = self._quick_put(apply_timeout if timeout else apply_target,
                               target, args, kwargs, callback, accept_callback,
                               timeout=timeout,
                               timeout_callback=timeout_callback)
        greenlet.terminate = types.MethodType(_terminate, greenlet)
        return greenlet

def _terminate(self, signal):
    self.kill()

What do you think?

@woutdenolf

Copy link
Copy Markdown
Contributor

I made a PR for it #9440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Termination tasks with eventlet/gevent worker pool.

6 participants