benoitc / gunicorn

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

Asynchronous thread created by a worker was killed when the worker began restarting #3283

Open skongkonga opened 1 month ago

skongkonga commented 1 month ago

Django: 4.2.5 Gunicorn: 22.0.0 Gevent: 24.2.1

In order to respond faster, I create an asynchronous thread (by "gevent.spawn" or "threading.Thread") to process the request after receiving it, and then respond directly. If the worker reaches the "max_requests" limit and starts to restart automatically, the asynchronous thread it created will also be forced to end (it is requesting other interfaces).

try:
    logger.debug('query started')
    response = requests.post(url, query)
    logger.debug('query done')
except:
    logger.debug('query failed')
finally:
    logger.debug('query finished')

When it happens, there is only 'query started' in the log, no 'query done' or even 'query finished'.

My temporary solution is to set "max_requests" to 0. But this doesn't completely solve the problem, because when I need to update the code I still need to restart the worker.