From 5070835d019b36069fbc804d689e1ba40d6e0528 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 17 Oct 2013 12:21:59 -0700 Subject: [PATCH] Ensuring server cleanup on OS signals atexit won't run registered functions for SIGTERM which we send to the server. This prevents clean shutdown. Also making sure that the server logfiles are deleted as well. --- python/ycm/server/default_settings.json | 1 + python/ycm/server/handlers.py | 2 +- python/ycm/server/ycmd.py | 16 ++++++++++++++++ python/ycm/youcompleteme.py | 6 ++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/ycm/server/default_settings.json b/python/ycm/server/default_settings.json index 73682183..4ee627a7 100644 --- a/python/ycm/server/default_settings.json +++ b/python/ycm/server/default_settings.json @@ -28,6 +28,7 @@ }, "server_use_vim_stdout": 0, "server_log_level": "info", + "server_keep_logfiles": 0, "auto_start_csharp_server": 1, "auto_stop_csharp_server": 1, "csharp_server_port": 2000 diff --git a/python/ycm/server/handlers.py b/python/ycm/server/handlers.py index 9c53fcde..6640511a 100644 --- a/python/ycm/server/handlers.py +++ b/python/ycm/server/handlers.py @@ -197,7 +197,7 @@ def _GetCompleterForRequestData( request_data ): @atexit.register -def _ServerShutdown(): +def ServerShutdown(): LOGGER.info( 'Server shutting down' ) if SERVER_STATE: SERVER_STATE.Shutdown() diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index 1173e792..036712b1 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -25,6 +25,7 @@ import logging import json import argparse import waitress +import signal from ycm import user_options_store from ycm import extra_conf_store @@ -34,6 +35,20 @@ def YcmCoreSanityCheck(): raise RuntimeError( 'ycm_core already imported, ycmd has a bug!' ) +# We need to manually call ServerShutdown for the signals that turn down ycmd +# because atexit won't handle them. +def SetUpSignalHandler(): + def SignalHandler(): + import handlers + handlers.ServerShutdown() + + for sig in [ signal.SIGTERM, + signal.SIGINT, + signal.SIGHUP, + signal.SIGQUIT ]: + signal.signal( sig, SignalHandler ) + + def Main(): parser = argparse.ArgumentParser() parser.add_argument( '--host', type = str, default = 'localhost', @@ -69,6 +84,7 @@ def Main(): # preload has executed. import handlers handlers.UpdateUserOptions( options ) + SetUpSignalHandler() waitress.serve( handlers.app, host = args.host, port = args.port, diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index 211d43de..8d8fee92 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -205,6 +205,12 @@ class YouCompleteMe( object ): self._server_popen.terminate() os.remove( self._temp_options_filename ) + if not self._user_options[ 'server_keep_logfiles' ]: + if self._server_stderr: + os.remove( self._server_stderr ) + if self._server_stdout: + os.remove( self._server_stdout ) + def OnCurrentIdentifierFinished( self ): if not self._IsServerAlive():