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:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user