Join listening thread on shutdown to make sure it exits properly before

terminating the interpreter
This commit is contained in:
Andrew Resch 2008-06-17 19:46:15 +00:00
parent cb9db1658b
commit 2578ca3e21
1 changed files with 28 additions and 4 deletions

View File

@ -44,6 +44,7 @@ from SocketServer import ThreadingMixIn
import deluge.xmlrpclib as xmlrpclib import deluge.xmlrpclib as xmlrpclib
import gobject import gobject
import threading import threading
import socket
import deluge.libtorrent as lt import deluge.libtorrent as lt
import deluge.configmanager import deluge.configmanager
@ -269,9 +270,11 @@ class Core(
component.start() component.start()
t = threading.Thread(target=self.serve_forever) self._should_shutdown = False
t.setDaemon(True)
t.start() self.listen_thread = threading.Thread(target=self.handle_thread)
self.listen_thread.setDaemon(False)
self.listen_thread.start()
gobject.threads_init() gobject.threads_init()
self.loop = gobject.MainLoop() self.loop = gobject.MainLoop()
@ -280,10 +283,31 @@ class Core(
except KeyboardInterrupt: except KeyboardInterrupt:
self._shutdown() self._shutdown()
def handle_thread(self):
try:
while not self._should_shutdown:
self.handle_request()
self._should_shutdown = False
except Exception, e:
log.debug("handle_thread: %s", e)
def _shutdown(self, *data): def _shutdown(self, *data):
"""This is called by a thread from shutdown()""" """This is called by a thread from shutdown()"""
log.info("Shutting down core..") log.info("Shutting down core..")
self._should_shutdown = True
# Shutdown the socket
try:
self.socket.shutdown(socket.SHUT_RDWR)
except Exception, e:
log.debug("exception in socket shutdown: %s", e)
log.debug("Joining listen thread to make sure it shutdowns cleanly..")
# Join the listen thread for a maximum of 1 second
self.listen_thread.join(1.0)
# Start shutting down the components
component.shutdown() component.shutdown()
# Make sure the config file has been saved # Make sure the config file has been saved
self.config.save() self.config.save()
del self.config del self.config