From 415500de2380dc20314594da536d804f47b9e768 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Thu, 9 Feb 2017 05:58:43 +0200 Subject: [PATCH] [recorder] Protect against running in the event loop (#5812) --- homeassistant/components/recorder/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index dba5f25b956..14b9fe11574 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -89,6 +89,8 @@ def execute(qry: QueryType) -> List[Any]: This method also retries a few times in the case of stale connections. """ + _verify_instance() + import sqlalchemy.exc with session_scope() as session: for _ in range(0, RETRIES): @@ -152,6 +154,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool: def query(model_name: Union[str, Any], *args) -> QueryType: """Helper to return a query handle.""" _verify_instance() + if isinstance(model_name, str): return _SESSION().query(get_model(model_name), *args) return _SESSION().query(model_name, *args) @@ -481,4 +484,9 @@ def _verify_instance() -> None: """Throw error if recorder not initialized.""" if _INSTANCE is None: raise RuntimeError("Recorder not initialized.") + + ident = _INSTANCE.hass.loop.__dict__.get("_thread_ident") + if ident is not None and ident == threading.get_ident(): + raise RuntimeError('Cannot be called from within the event loop') + _INSTANCE.block_till_db_ready()