Abort execution of template renders that overwhelm the system#40647
Abort execution of template renders that overwhelm the system#40647balloob merged 14 commits intohome-assistant:devfrom
Conversation
This only affects templates render requests from the websocket api. jinja attempts to detect these type of templates in advance and will raise OverflowError for cases it knows will overwhelm the system, however it cannot detect all cases. This solution pre-tests each render request to ensure a template can be rendered in MAX_TEMPLATE_RENDER_TIME seconds. If the template takes too long, execution is canceled and the subscription is not setup.
|
Hey there @home-assistant/core, mind taking a look at this pull request as its been labeled with an integration ( |
|
We can't actual cancel the job once it starts running. There doesn't seem to be a clean way to do that |
|
Shame there isn't a cleaner way. This approach should work, and it is not like there are not thousands of projects doing this same sort of thread killing, but still rather unfortunate. The fact that the templates are immutable/pure, is the main reason I'm not particularly concerned by this approach. There are some other tricks that might be nicer, but won't necessarily handle every case. For the given example, using If we were using async mode of jinja, it feels like there might be some trick we can play by using a custom event loop, but once again not convinced that could handle everything a thread abort approach can. |
Proposed change
This only affects templates render requests from the websocket api that request a timeout.
jinja attempts to detect these type of templates in advance and will
raise OverflowError for cases it knows will overwhelm the system,
however it cannot detect all cases.
Example:
OverflowError: Range too big. The sandbox blocks ranges larger than MAX_RANGE (100000).This solution pre-tests each initial render request to ensure a template
can be rendered in requested timeout in seconds. If the template
takes too long, execution is canceled and the subscription is not
setup.
Pasting
into the template developer tools before this change would cause
the entire system to lock up and run out of memory.
25440 root 20 0 9834.1m 5.171g 100.7 26.7 1:00.92 R python3 -m homeassistant --config /configType of change
Example entry for
configuration.yaml:# Example configuration.yamlAdditional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: