Skip to content

Handle SIGTERM correctly.

We are handling differently SIGINT and SIGTERM since we need a special treatment for twisted/SIGTERM (see #5672 (closed)). Even though we do the same for each signal, the app closes differently depending on the signal and it should do the same.

The problem is that we are relying in calls using @callLater@ and they are ignored after that the SIGTERM handler is finished since the app is terminated. Most likely there is no 'later' after the signal handler is called.

For some extra information look at: #5803 (closed)

Here is an example that confirms that behavior:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import signal

from twisted.internet import reactor, threads


def sigterm_handler():
    print 'SIGTERM'
    reactor.callLater(0, test)  # not called
    threads.deferToThread(demo)  # called


def test():
    print 'test'


def demo():
    print 'demo'


if __name__ == '__main__':
    # Ensure that the application quits using CTRL-C
    signal.signal(signal.SIGINT, signal.SIG_DFL)

    reactor.addSystemEventTrigger('before', 'shutdown', sigterm_handler)
    reactor.run()

(from redmine: created on 2014-06-27, closed on 2014-07-28, relates #5803 (closed))