Use BaseEventLoop._thread_id instead of a custom attribute (#124054)

Co-authored-by: J. Nick Koston <nick@koston.org>
pull/124101/head
Erik Montnemery 2024-08-17 10:59:39 +02:00 committed by GitHub
parent 7deb9bf30f
commit 6c01e4b99c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 7 additions and 12 deletions

View File

@ -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."""

View File

@ -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)

View File

@ -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()

View File

@ -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()

View File

@ -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),
):