From cdaa7159d20cd4dcdaeba33657d96e6e9e475ed8 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 20 Sep 2018 09:13:37 +0200 Subject: [PATCH 1/3] Fix Windows loop --- homeassistant/__main__.py | 18 +++++++++++------- homeassistant/core.py | 5 +---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index af89564f1..3ca708a95 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -19,15 +19,19 @@ from homeassistant.const import ( ) -def attempt_use_uvloop() -> None: +def set_loop() -> None: """Attempt to use uvloop.""" import asyncio - try: - import uvloop - except ImportError: - pass + + if sys.platform == 'win32': + asyncio.set_event_loop(asyncio.ProactorEventLoop()) else: - asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + try: + import uvloop + except ImportError: + pass + else: + asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) def validate_python() -> None: @@ -345,7 +349,7 @@ def main() -> int: monkey_patch.disable_c_asyncio() monkey_patch.patch_weakref_tasks() - attempt_use_uvloop() + set_loop() args = get_arguments() diff --git a/homeassistant/core.py b/homeassistant/core.py index 653f95cce..d1f811502 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -129,10 +129,7 @@ class HomeAssistant: self, loop: Optional[asyncio.events.AbstractEventLoop] = None) -> None: """Initialize new Home Assistant object.""" - if sys.platform == 'win32': - self.loop = loop or asyncio.ProactorEventLoop() - else: - self.loop = loop or asyncio.get_event_loop() + self.loop = loop or asyncio.get_event_loop() executor_opts = {'max_workers': None} # type: Dict[str, Any] if sys.version_info[:2] >= (3, 6): From b2604b6f8e180b0e22d168f5cb7b3947fd3b1e2c Mon Sep 17 00:00:00 2001 From: Jason Hu Date: Fri, 21 Sep 2018 11:00:54 -0700 Subject: [PATCH 2/3] Fix windows Ctrl+C --- homeassistant/__main__.py | 2 ++ homeassistant/helpers/signal.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index 3ca708a95..25aa765ac 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -255,6 +255,8 @@ async def setup_and_run_hass(config_dir: str, try: subprocess.check_call(nt_args) sys.exit(0) + except KeyboardInterrupt: + sys.exit(0) except subprocess.CalledProcessError as exc: if exc.returncode != RESTART_EXIT_CODE: sys.exit(exc.returncode) diff --git a/homeassistant/helpers/signal.py b/homeassistant/helpers/signal.py index 6068cad33..7496388fb 100644 --- a/homeassistant/helpers/signal.py +++ b/homeassistant/helpers/signal.py @@ -43,3 +43,28 @@ def async_register_signal_handling(hass: HomeAssistant) -> None: signal.SIGHUP, async_signal_handle, RESTART_EXIT_CODE) except ValueError: _LOGGER.warning("Could not bind to SIGHUP") + + else: + old_sigterm = None + old_sigint = None + + @callback + def async_signal_handle(exit_code, frame): + """Wrap signal handling. + + * queue call to shutdown task + * re-instate default handler + """ + signal.signal(signal.SIGTERM, old_sigterm) + signal.signal(signal.SIGINT, old_sigint) + hass.async_create_task(hass.async_stop(exit_code)) + + try: + old_sigterm = signal.signal(signal.SIGTERM, async_signal_handle) + except ValueError: + _LOGGER.warning("Could not bind to SIGTERM") + + try: + old_sigint = signal.signal(signal.SIGINT, async_signal_handle) + except ValueError: + _LOGGER.warning("Could not bind to SIGINT") From 6ecd00ceecddab85cad2991624edf1763f86d919 Mon Sep 17 00:00:00 2001 From: Jason Hu Date: Fri, 21 Sep 2018 11:51:32 -0700 Subject: [PATCH 3/3] Move windows restart handler out of setup_and_run_hass --- homeassistant/__main__.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index 25aa765ac..91b7a7f84 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -248,19 +248,6 @@ async def setup_and_run_hass(config_dir: str, """Set up HASS and run.""" from homeassistant import bootstrap, core - # Run a simple daemon runner process on Windows to handle restarts - if os.name == 'nt' and '--runner' not in sys.argv: - nt_args = cmdline() + ['--runner'] - while True: - try: - subprocess.check_call(nt_args) - sys.exit(0) - except KeyboardInterrupt: - sys.exit(0) - except subprocess.CalledProcessError as exc: - if exc.returncode != RESTART_EXIT_CODE: - sys.exit(exc.returncode) - hass = core.HomeAssistant() if args.demo_mode: @@ -353,6 +340,19 @@ def main() -> int: set_loop() + # Run a simple daemon runner process on Windows to handle restarts + if os.name == 'nt' and '--runner' not in sys.argv: + nt_args = cmdline() + ['--runner'] + while True: + try: + subprocess.check_call(nt_args) + sys.exit(0) + except KeyboardInterrupt: + sys.exit(0) + except subprocess.CalledProcessError as exc: + if exc.returncode != RESTART_EXIT_CODE: + sys.exit(exc.returncode) + args = get_arguments() if args.script is not None: