From 6c01e4b99cb1639dea94258119e4a1ff1a65046c Mon Sep 17 00:00:00 2001 From: Erik Montnemery <erik@montnemery.com> Date: Sat, 17 Aug 2024 10:59:39 +0200 Subject: [PATCH] Use BaseEventLoop._thread_id instead of a custom attribute (#124054) Co-authored-by: J. Nick Koston <nick@koston.org> --- homeassistant/core.py | 4 +--- homeassistant/runner.py | 1 - homeassistant/util/async_.py | 2 +- tests/common.py | 2 -- tests/util/test_async.py | 10 +++++----- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 1050d25ee71..b797798134e 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -451,9 +451,7 @@ class HomeAssistant: self.import_executor = InterruptibleThreadPoolExecutor( max_workers=1, thread_name_prefix="ImportExecutor" ) - self.loop_thread_id = getattr( - self.loop, "_thread_ident", getattr(self.loop, "_thread_id") - ) + self.loop_thread_id = getattr(self.loop, "_thread_id") def verify_event_loop_thread(self, what: str) -> None: """Report and raise if we are not running in the event loop thread.""" diff --git a/homeassistant/runner.py b/homeassistant/runner.py index a1510336302..4bac12ec399 100644 --- a/homeassistant/runner.py +++ b/homeassistant/runner.py @@ -107,7 +107,6 @@ class HassEventLoopPolicy(asyncio.DefaultEventLoopPolicy): def new_event_loop(self) -> asyncio.AbstractEventLoop: """Get the event loop.""" loop: asyncio.AbstractEventLoop = super().new_event_loop() - setattr(loop, "_thread_ident", threading.get_ident()) loop.set_exception_handler(_async_loop_exception_handler) if self.debug: loop.set_debug(True) diff --git a/homeassistant/util/async_.py b/homeassistant/util/async_.py index dcb788f0685..d010d8cb341 100644 --- a/homeassistant/util/async_.py +++ b/homeassistant/util/async_.py @@ -57,7 +57,7 @@ def run_callback_threadsafe[_T, *_Ts]( Return a concurrent.futures.Future to access the result. """ - if (ident := loop.__dict__.get("_thread_ident")) and ident == threading.get_ident(): + if (ident := loop.__dict__.get("_thread_id")) and ident == threading.get_ident(): raise RuntimeError("Cannot be called from within the event loop") future: concurrent.futures.Future[_T] = concurrent.futures.Future() diff --git a/tests/common.py b/tests/common.py index 2f0c032616a..3a4c3f2851c 100644 --- a/tests/common.py +++ b/tests/common.py @@ -204,8 +204,6 @@ def get_test_home_assistant() -> Generator[HomeAssistant]: hass.start = start_hass hass.stop = stop_hass - loop._thread_ident = threading.get_ident() - hass_created_event.set() loop.run_forever() diff --git a/tests/util/test_async.py b/tests/util/test_async.py index 17349cf6ff9..cda10b69c3f 100644 --- a/tests/util/test_async.py +++ b/tests/util/test_async.py @@ -22,18 +22,18 @@ def test_run_callback_threadsafe_from_inside_event_loop( loop = Mock(spec=["call_soon_threadsafe"]) - loop._thread_ident = None + loop._thread_id = None mock_ident.return_value = 5 hasync.run_callback_threadsafe(loop, callback) assert len(loop.call_soon_threadsafe.mock_calls) == 1 - loop._thread_ident = 5 + loop._thread_id = 5 mock_ident.return_value = 5 with pytest.raises(RuntimeError): hasync.run_callback_threadsafe(loop, callback) assert len(loop.call_soon_threadsafe.mock_calls) == 1 - loop._thread_ident = 1 + loop._thread_id = 1 mock_ident.return_value = 5 hasync.run_callback_threadsafe(loop, callback) assert len(loop.call_soon_threadsafe.mock_calls) == 2 @@ -78,7 +78,7 @@ async def test_run_callback_threadsafe(hass: HomeAssistant) -> None: nonlocal it_ran it_ran = True - with patch.dict(hass.loop.__dict__, {"_thread_ident": -1}): + with patch.dict(hass.loop.__dict__, {"_thread_id": -1}): assert hasync.run_callback_threadsafe(hass.loop, callback) assert it_ran is False @@ -98,7 +98,7 @@ async def test_callback_is_always_scheduled(hass: HomeAssistant) -> None: hasync.shutdown_run_callback_threadsafe(hass.loop) with ( - patch.dict(hass.loop.__dict__, {"_thread_ident": -1}), + patch.dict(hass.loop.__dict__, {"_thread_id": -1}), patch.object(hass.loop, "call_soon_threadsafe") as mock_call_soon_threadsafe, pytest.raises(RuntimeError), ): -- GitLab