Skip to content

support Request termination for gevent - #9440

Merged
Nusnus merged 2 commits into
celery:mainfrom
woutdenolf:followup_pr9083
Dec 1, 2024
Merged

Nusnus merged 2 commits into
celery:mainfrom
woutdenolf:followup_pr9083

Conversation

@woutdenolf

@woutdenolf woutdenolf commented Nov 28, 2024 •

Copy link
Copy Markdown
Contributor

PR #9083 adds support for terminating gevent jobs.

I can confirm @dwick observation #9083 (comment)_

[2024-11-28 23:25:56,623: ERROR/MainProcess] pidbox command error: AttributeError("'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate'")
Traceback (most recent call last):
  File ".../python3.9/site-packages/kombu/pidbox.py", line 102, in dispatch
    reply = handle(method, arguments)
  File ".../python3.9/site-packages/kombu/pidbox.py", line 124, in handle_cast
    return self.handle(method, arguments)
  File ".../python3.9/site-packages/kombu/pidbox.py", line 118, in handle
    return self.handlers[method](self.state, **arguments)
  File "/home/denolf/dev/celery/celery/worker/control.py", line 149, in revoke
    task_ids = _revoke(state, task_ids, terminate, signal, **kwargs)
  File "/home/denolf/dev/celery/celery/worker/control.py", line 224, in _revoke
    request.terminate(state.consumer.pool, signal=signum)
  File "/home/denolf/dev/celery/celery/worker/request.py", line 423, in terminate
    obj.terminate(signal)
AttributeError: 'gevent._gevent_cgreenlet.Greenlet' object has no attribute 'terminate'

The reason is this

class Request:
    """A request for task execution."""

    def terminate(self, pool, signal=None):
        signal = _signals.signum(signal or TERM_SIGNAME)
        if self.time_start:
            pool.terminate_job(self.worker_pid, signal)   # Ask for termination with the pool
            self._announce_revoked('terminated', True, signal, False)
        else:
            self._terminate_on_ack = pool, signal
        if self._apply_result is not None:
            obj = self._apply_result()  # is a weakref
            if obj is not None:
                obj.terminate(signal)  # Ask for termination with the greenlet itself

In this MR was bind terminate to the greenlet instance when returned from on_apply.

@codecov

codecov Bot commented Nov 28, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 78.15%. Comparing base (eb559a6) to head (b0c49a3).
Report is 55 commits behind head on main.

Files with missing lines Patch % Lines
celery/concurrency/gevent.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9440      +/-   ##
==========================================
- Coverage   78.16%   78.15%   -0.01%     
==========================================
  Files         153      153              
  Lines       19025    19029       +4     
  Branches     2518     2518              
==========================================
+ Hits        14870    14873       +3     
- Misses       3866     3867       +1     
  Partials      289      289              
Flag Coverage Δ
unittests 78.13% <75.00%> (-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.

@woutdenolf

woutdenolf commented Nov 28, 2024 •

Copy link
Copy Markdown
Contributor Author

To make it more likely for obj.terminate to be called

    def _make_killable_target(target):
        def killable_target(*args, **kwargs):
            from greenlet import GreenletExit
            try:
                return target(*args, **kwargs)
            except GreenletExit:
                import time
                time.sleep(1)  # <<<<<
                return (False, None, None)

This does show that the greenlet may get killed twice and therefore capturing GreenletExit for cleanup will not always work.

Alternatively we can not return the greenlet from on_apply (like we do for eventlet)

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=apply_target, **_):
        timeout = self.timeout if timeout is None else timeout
        target = self._make_killable_target(target)
        greenlet = self._quick_put(apply_timeout if timeout else apply_target,
                                   target, args, kwargs, callback, accept_callback,
                                   self.getpid, timeout=timeout, timeout_callback=timeout_callback)
        self._add_to_pool_map(id(greenlet), greenlet)

I'm not sure what the side-effects are of this.

@Nusnus What do you think is the best?

@woutdenolf

woutdenolf commented Nov 28, 2024 •

Copy link
Copy Markdown
Contributor Author

A third option would be to bind an dummy function

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

    def on_apply(...):
        ...
        greenlet.terminate = types.MethodType(_terminate, greenlet)
        return greenlet

def _terminate(self, signal):
    # Done in `TaskPool.terminate_job`
    pass

This way the greenlet gets killed only once and we return a result handler from on_apply.

@woutdenolf

Copy link
Copy Markdown
Contributor Author

I went for the third option in the end: on_apply still returns the greenlet and it has a terminate method that does nothing.

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

@woutdenolf

@Nusnus What do you think is the best?

TBH I am not completely sure, but the 3rd (and current) option appears to be a simple fix, which is favorable.

I’ve postponed the final release in favor of another RC which I’ll release tomorrow.
I’ll include this patch, and let’s see if the issue gets resolved.

@Nusnus
Nusnus merged commit 4d129fd into celery:main Dec 1, 2024
@Nusnus

Nusnus commented Dec 3, 2024 •

Copy link
Copy Markdown
Member

I’ve postponed the final release in favor of another RC which I’ll release tomorrow. I’ll include this patch, and let’s see if the issue gets resolved.

v5.5.0rc3 @woutdenolf

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants