Find unused port for codechecker server
@@ -43,6 +43,7 @@ from codechecker_web.shared import webserver_context, database_status, \
host_check, env
LOG = logger.get_logger('server')
+DEFAULT_SERVER_PORT = 8001
def get_argparser_ctor_args():
@@ -113,7 +114,7 @@ def add_arguments_to_parser(parser):
type=int,
dest="view_port",
metavar='PORT',
- default=8001,
+ default=DEFAULT_SERVER_PORT,
required=False,
help="The port which will be used as listen port for "
"the server.")
@@ -954,21 +955,31 @@ def server_init_start(args):
'version': context.package_git_tag}
try:
- server.start_server(args.config_directory,
- package_data,
- args.view_port,
- cfg_sql_server,
- args.listen_address,
- 'force_auth' in args,
- args.skip_db_cleanup,
- context,
- environ)
+ server_args = {
+ 'config_directory': args.config_directory,
+ 'package_data': package_data,
+ 'port': args.view_port,
+ 'config_sql_server': cfg_sql_server,
+ 'listen_address': args.listen_address,
+ 'force_auth': 'force_auth' in args,
+ 'skip_db_cleanup': args.skip_db_cleanup,
+ 'context': context,
+ 'check_env': environ
+ }
+ server.start_server(**server_args)
except socket.error as err:
- if err.errno == errno.EADDRINUSE:
- LOG.error("Server can't be started, maybe the given port number "
- "(%s) is already used. Check the connection "
- "parameters.", args.view_port)
- sys.exit(1)
+ # let os find a port, unless user requested specific port in args
+ if (
+ err.errno == errno.EADDRINUSE
+ and args.view_port == DEFAULT_SERVER_PORT
+ ):
+ LOG.info(
+ "Port %s is already used, trying another port.",
+ server_args['port']
+ )
+
+ server_args['port'] = 0
+ server.start_server(**server_args)
else:
raise
