Skip to content

Commit

Permalink
Support UNIX domain socket connection as alternative to TCP/IP
Browse files Browse the repository at this point in the history
This prevents a race condition on usage of a port (where the port is "guessed"
by the client) and also prevents consuming a TCP port in the ephemeral port
range (or otherwise) which may be a contended resource
  • Loading branch information
puremourning committed Aug 6, 2015
1 parent 36be0bb commit 9528eaf
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions ycmd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def YcmCoreSanityCheck():

# We manually call sys.exit() on SIGTERM and SIGINT so that atexit handlers are
# properly executed.
def SetUpSignalHandler( stdout, stderr, keep_logfiles ):
def SetUpSignalHandler( stdout, stderr, keep_logfiles, unix_socket ):
def SignalHandler( signum, frame ):
# We reset stderr & stdout, just in case something tries to use them
if stderr:
Expand All @@ -59,6 +59,9 @@ def SignalHandler( signum, frame ):
if stdout:
utils.RemoveIfExists( stdout )

if unix_socket:
utils.RemoveIfExists( unix_socket )

sys.exit()

for sig in [ signal.SIGTERM,
Expand All @@ -85,6 +88,11 @@ def ParseArguments():
# Default of 0 will make the OS pick a free port for us
parser.add_argument( '--port', type = int, default = 0,
help = 'server port')
# Optional domain socket to use in place of listening port. This prevents use
# of a TCP port on systems where they are used for interface processes.
parser.add_argument( '--domain_socket', type = str, default = None,
help = 'name of a unix domain socket to use in place of '
'tcp host/port' )
parser.add_argument( '--log', type = str, default = 'info',
help = 'log level, one of '
'[debug|info|warning|error|critical]' )
Expand Down Expand Up @@ -155,15 +163,21 @@ def Main():
from ycmd import handlers
handlers.UpdateUserOptions( options )
handlers.SetHmacSecret( hmac_secret )
SetUpSignalHandler( args.stdout, args.stderr, args.keep_logfiles )
SetUpSignalHandler( args.stdout,
args.stderr,
args.keep_logfiles,
args.domain_socket )
handlers.app.install( WatchdogPlugin( args.idle_suicide_seconds ) )
handlers.app.install( HmacPlugin( hmac_secret ) )
CloseStdin()
waitress.serve( handlers.app,
host = args.host,
port = args.port,
threads = 30 )

if not args.domain_socket:
waitress.serve( handlers.app,
host = args.host,
port = args.port,
threads = 30 )
else:
waitress.serve( handlers.app, unix_socket = args.domain_socket )

if __name__ == "__main__":
Main()
Expand Down

0 comments on commit 9528eaf

Please sign in to comment.