wassy92x / lovelace-digital-clock

A custom digital clock card for Home Assistant
MIT License
89 stars 13 forks source link

100% CPU on backend #27

Closed evarsanyi closed 1 year ago

evarsanyi commented 1 year ago

I was experimenting and put this clock on a 'test' tab on one of my lovelace dashboards. Even though the component wasn't loaded on any dashboard the JS loaded on the client side randomly (from my POV) starts beating up the backend with a template render call every millisecond. Coming from 3 wall displays this was enough to crash home assistant. Taking it out of the resources file fixed the issue.

I ended up finding this with tcpdump looking at the network traffic, on the HA side its pretty hard to figure out where the calls are coming from. With debug logging on you'll see megabytes of messages like this:

2023-01-20 10:18:25.675 DEBUG (MainThread) [homeassistant.helpers.event] Template group [TrackTemplate(template=Template("{{as_timestamp(now())}}"), variables=None, rate_limit=None)] listens for {'all': False, 'entities': set(), 'domains': set(), 'time': True}, re-render blocker by super template: False 2023-01-20 10:18:25.676 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139717910366384] Client exceeded max pending messages [2]: 2048

evarsanyi commented 1 year ago

If it helps, I installed this via HACS:

lovelace_resources: "url": "/hacsfiles/lovelace-digital-clock/digital-clock.js?hacstag=373832981122",

wassy92x commented 1 year ago

The log-line which you posted can't be from this clock, because the date-time is read from your client by calling a javascript function, not by rendering any template on the backend.

evarsanyi commented 1 year ago

Apologies if this is bad diagnosis on my part, removing the www/community/lovelace-digital-clock directory 100% fixed the issue and all the noisy traffic stopped. The code I found in DigitalClock.js that caused this (and matches the log messages from the backend) is still in this repo:

  } else {
            dateTime = DateTime.fromSeconds(await new Promise<number>((resolve) => {
                this.hass.connection.subscribeMessage(
                    (msg) => resolve(parseInt((msg as any).result, 10)),
                    {type: "render_template", template: '{{as_timestamp(now())}}'}
                );
            }));
        } */

however it appears you've commented it out at HEAD, so I guess close this as 'fixed'? Mostly it was so hard to find this I wanted to let you know in case someone else has the issue. I'm still searching for a clock widget that uses the backend time (w/o melting the server down).

wassy92x commented 1 year ago

As you already noticed, the lines are in comments. I will check the releases, if there is maybe a faulty one

wassy92x commented 1 year ago

I checked all releases. There is no release which is faultly.

Also you can check the diff of commit d0fc4fa. The lines were added in this commit, but they are commented out.

Unfortunatelly I can't comprehend this issue.

evarsanyi commented 1 year ago

In the version I have (which I think I got from hacs based on the directory it was in, it would have been long ago) that block of code was not commented out and was the cause of the high traffic I was seeing, I uncommented it for a test long ago and fumbled reverting it (restored a backup).

If the code is running as you distribute it and there is some bug around triggering updates on a schedule it might make the page use a lot of CPU (instead of in my case making lots of network calls).

Thanks for publishing this project!