Prime platform.uname cache at startup to fix blocking subprocess in the event loop (#73975)

Prime platform.uname cache at startup to fix blocking subprocess

- Multiple modules check platform.uname()[0] at startup which
  does a blocking subprocess call. We can avoid this happening
  in the eventloop and distrupting startup stability by priming
  the cache ahead of time in the executor
This commit is contained in:
J. Nick Koston 2022-06-25 03:31:44 -05:00 committed by GitHub
parent 0166816200
commit 55b5ade586
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ from datetime import datetime, timedelta
import logging import logging
import logging.handlers import logging.handlers
import os import os
import platform
import sys import sys
import threading import threading
from time import monotonic from time import monotonic
@ -540,11 +541,22 @@ async def _async_set_up_integrations(
stage_2_domains = domains_to_setup - logging_domains - debuggers - stage_1_domains stage_2_domains = domains_to_setup - logging_domains - debuggers - stage_1_domains
def _cache_uname_processor() -> None:
"""Cache the result of platform.uname().processor in the executor.
Multiple modules call this function at startup which
executes a blocking subprocess call. This is a problem for the
asyncio event loop. By primeing the cache of uname we can
avoid the blocking call in the event loop.
"""
platform.uname().processor # pylint: disable=expression-not-assigned
# Load the registries # Load the registries
await asyncio.gather( await asyncio.gather(
device_registry.async_load(hass), device_registry.async_load(hass),
entity_registry.async_load(hass), entity_registry.async_load(hass),
area_registry.async_load(hass), area_registry.async_load(hass),
hass.async_add_executor_job(_cache_uname_processor),
) )
# Start setup # Start setup