Add a 60s timeout to shell_command to prevent processes from building up (#38491)

If a process never ended, there was not timeout and they would
build up in the background until Home Assistant crashed.
This commit is contained in:
J. Nick Koston
2020-08-04 16:59:19 -10:00
committed by GitHub
parent c33f309d5f
commit dddcb8e299
2 changed files with 39 additions and 2 deletions

View File

@@ -12,6 +12,8 @@ from homeassistant.helpers.typing import ConfigType, HomeAssistantType
DOMAIN = "shell_command"
COMMAND_TIMEOUT = 60
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema(
@@ -74,7 +76,22 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
)
process = await create_process
stdout_data, stderr_data = await process.communicate()
try:
stdout_data, stderr_data = await asyncio.wait_for(
process.communicate(), COMMAND_TIMEOUT
)
except asyncio.TimeoutError:
_LOGGER.exception(
"Timed out running command: `%s`, after: %ss", cmd, COMMAND_TIMEOUT
)
if process:
try:
await process.kill()
except TypeError:
pass
del process
return
if stdout_data:
_LOGGER.debug(