benoitc / gunicorn

gunicorn 'Green Unicorn' is a WSGI HTTP Server for UNIX, fast clients and sleepy applications.
http://www.gunicorn.org
Other
9.88k stars 1.76k forks source link

gunicorn + gevent(worker class) generating exception when terminating the process with `ctrl+c` #3239

Closed Kalebe16 closed 3 months ago

Kalebe16 commented 4 months ago

python-version: 3.12.1

gunicorn-version: 22.0.0

gevent-version: 24.2.1

image image image

benoitc commented 3 months ago

This is not really an issue. I guess exit is not expected but then ctrl-c is also kind of a brutal way to exit the process. closing as none issue.

Kalebe16 commented 3 months ago

From my point of view, terminating the program with an exception is inelegant, anyway, my solution was this:

from gunicorn.app.base import BaseApplication
from gunicorn.workers.ggevent import GeventWorker

def monkey_patch_gevent_worker() -> None:
    def handle_quit(self, sig, frame):
        self.alive = False   # This is enough to stop the gevent worker

    GeventWorker.handle_quit = handle_quit

monkey_patch_gevent_worker()