Uwsgi: atexit hook works incorrectly

Created on 7 Aug 2014  路  10Comments  路  Source: unbit/uwsgi

Hello when using uwsgi + python, and trying to catch app exit
sending SIGINT or SIGTERM to the uwsgi master process - atexit hook does not work :(
in the uwsgi log is

SIGINT/SIGQUIT received...killing workers...
gateway "uWSGI http 1" has been buried (pid: 15703)
worker 1 buried after 1 seconds
goodbye to uWSGI.

how can i properly handle uwsgi shutdown? i haven't found any other hook except atexit for this

Most helpful comment

Great! it works! thank you

All 10 comments

You can use atexit python module directly (https://docs.python.org/2/library/atexit.html) or the uwsgi.atexit callable (even if probably it is what you are trying to use). The relevant code path is here: https://github.com/unbit/uwsgi/blob/master/plugins/python/python_plugin.c#L322

uwsgi.atexit does now works for me.
part of test code:

uwsgi.post_fork_hook = after_fork
def after_fork():
    uwsgi.atexit = s
def s():
    print >>sys.stderr, 'exited'

uwsgi.ini

[dev]
listen = 128
gevent = 5000
module = main.server
http-keepalive = true
virtualenv = v
master = true

on SIGTERM/SEGQUIT hook does not works.
it fires only on SIGHUP...

any ideas how to make it work?

I would not trust "atexit" python way, it has proven to be really weak, expecially for most complex cases.

I strongly suggest you to move your "finalization" code in a dedicated python file and call it as a generic hook:

[uwsgi]
...
hook-as-user-atexit = exec:python finalization.py

@unbit it will run separate python script as independent process?
how can i make it run my function inside my project?

Yeah, the big need I have is to run some python function from inside the uWSGI python process so I can access all the data that is there. It sounds like uwsgi.atexit() is the way to go?

up.
will be there any solution to do so?

uwsgi.atexit() is a "last resort" but should work. What i fear here is that the gevent plugin must be improved for managing stop in addition to reloads. This line is where the "shutdown" happens: https://github.com/unbit/uwsgi/blob/master/plugins/gevent/gevent.c#L104

For non-gevent scenario, there is lot of work going on on the perl plugin to improve atexit management, maybe something could be backported to python too (implications are generally the same)

The gevent plugin now re-register SIGINT and SIGTERM to a custom handler that will respect atexit hooks

Great! it works! thank you

Was this page helpful?
0 / 5 - 0 ratings