Ticket #761: fix-twistd-exit-code.patch

File fix-twistd-exit-code.patch, 4.0 KB (added by Martin Gergov, 7 years ago)
  • twisted/internet/base.py

     
    1111import socket # needed only for sync-dns
    1212from zope.interface import implementer, classImplements
    1313
    14 import sys
     14import sys, signal
    1515import warnings
    1616from heapq import heappush, heappop, heapify
    1717
     
    468468    installed = False
    469469    usingThreads = False
    470470    resolver = BlockingResolver()
     471    _exitStatus = 0
    471472
    472473    __name__ = "twisted.internet.reactor"
    473474
     
    601602        """
    602603        log.msg("Received SIGINT, shutting down.")
    603604        self.callFromThread(self.stop)
     605        self._exitStatus = signal.SIGINT
    604606
    605607    def sigBreak(self, *args):
    606608        """Handle a SIGBREAK interrupt.
     
    613615        """
    614616        log.msg("Received SIGTERM, shutting down.")
    615617        self.callFromThread(self.stop)
     618        self._exitStatus = signal.SIGTERM
    616619
    617620    def disconnectAll(self):
    618621        """Disconnect every reader, and writer in the system.
  • twisted/internet/test/test_base.py

     
    55Tests for L{twisted.internet.base}.
    66"""
    77
    8 import socket
     8import socket, signal, os
    99try:
    1010    from Queue import Queue
    1111except ImportError:
     
    1919from twisted.internet.base import ThreadedResolver, DelayedCall
    2020from twisted.internet.task import Clock
    2121from twisted.trial.unittest import TestCase
     22from twisted.internet.base import _SignalReactorMixin, ReactorBase
    2223
    2324
    2425@implementer(IReactorTime, IReactorThreads)
     
    270271        self.assertTrue(self.zero != self.one)
    271272        self.assertFalse(self.zero != self.zero)
    272273        self.assertFalse(self.one != self.one)
     274
     275class FakeReactorWithSignalHandlers(FakeReactor, _SignalReactorMixin,
     276                                    ReactorBase):
     277    def __init__(self):
     278        FakeReactor.__init__(self)
     279
     280class SignalReactorTests(TestCase):
     281
     282    def test_exitStatusAfterKill(self):
     283        """
     284        Assert appropriate exit statuses for corresponding signals.
     285        """
     286        oldIntHandle = signal.getsignal(signal.SIGINT)
     287        oldTermHandle = signal.getsignal(signal.SIGTERM)
     288        reactor = FakeReactorWithSignalHandlers()
     289        reactor._handleSignals()
     290        self.addCleanup(reactor._stop)
     291        pid = os.getpid()
     292        os.kill(pid, signal.SIGINT)
     293        intStatus = reactor._exitStatus
     294        os.kill(pid, signal.SIGTERM)
     295        termStatus = reactor._exitStatus
     296
     297        self.assertEqual(intStatus, signal.SIGINT)
     298        self.assertEqual(termStatus, signal.SIGTERM)
     299
     300        signal.signal(signal.SIGINT, oldIntHandle)
     301        signal.signal(signal.SIGTERM, oldTermHandle)
  • twisted/application/app.py

     
    1212from twisted.python.log import ILogObserver
    1313from twisted.persisted import sob
    1414from twisted.application import service, reactors
    15 from twisted.internet import defer
     15from twisted.internet import defer, reactor
    1616from twisted import copyright, plugin
    1717
    1818# Expose the new implementation of installReactor at the old location.
  • twisted/topfiles/761.bugfix

     
     1Twistd returns SIGINT for SIGINT signal and SIGTERM for SIGTERM signal.
     2 No newline at end of file
  • bin/twistd

     
    1111sys.path.insert(0, os.path.abspath(os.getcwd()))
    1212
    1313from twisted.scripts.twistd import run
     14from twisted.internet import reactor
    1415run()
     16sys.exit(reactor._exitStatus)