From 53d51a467dc6db2d28db5082e8dbf3b7deb49ebd Mon Sep 17 00:00:00 2001 From: Jan Harkes Date: Fri, 20 May 2016 14:45:16 -0400 Subject: [PATCH] Single process restart fixes (#2118) * Ignore permission errors on setpgid. When launched in a docker container we got a permission denied error from setpgid. * Don't fail if we find our own pidfile. When we restart using exec we are running a new instance of home-assistant with the same process id so we shouldn't be surprised to find an existing pidfile in that case. * Allow restart to work when started as python -m homeassistant. When we are started with `python -m homeassistant`, the restart command line becomes `python /path/to/hass/homeassistant/__main__.py`. But in that case the python path includes `/path/to/hass/homeassistant` instead of `/path/to/hass` and we fail on the first import. Fix this by recognizing `/__main__.py` as part of the first argument and injecting the proper path as PYTHONPATH environment before we start the new home-assistant instance. --- homeassistant/__main__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index 0cc99cb03f2..9494c2a02d1 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -174,6 +174,10 @@ def check_pid(pid_file): # PID File does not exist return + # If we just restarted, we just found our own pidfile. + if pid == os.getpid(): + return + try: os.kill(pid, 0) except OSError: @@ -253,6 +257,9 @@ def closefds_osx(min_fd, max_fd): def cmdline(): """Collect path and arguments to re-execute the current hass instance.""" + if sys.argv[0].endswith('/__main__.py'): + modulepath = os.path.dirname(sys.argv[0]) + os.environ['PYTHONPATH'] = os.path.dirname(modulepath) return [sys.executable] + [arg for arg in sys.argv if arg != '--daemon'] @@ -395,7 +402,10 @@ def main(): # Create new process group if we can if hasattr(os, 'setpgid'): - os.setpgid(0, 0) + try: + os.setpgid(0, 0) + except PermissionError: + pass exit_code = setup_and_run_hass(config_dir, args) if exit_code == RESTART_EXIT_CODE and not args.runner: