New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix memory leaks in websocket api #94780
Conversation
Also adds a lot more debug logging (guards with isEnabledFor to prevent a performance regression) related issue #94726
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Will need to adjust the test as it will be closed now instead of closing |
I will address the above comment and add a test for cancelation on write to make sure things are cleaned up |
I was doing some testing with this patch applied to see whether it fixes the memory leak. Since I don't know how to reliably trigger the frontend bug, my results have been inconclusive. However, I have encountered this error (not sure if it is related nor if it is expected):
|
Thats probably a good thing as it means the cancellation is propagating now and the transport is closing. Unfortunately aiohttp is a big noisy there. I'll see if we can clean that up |
@ondras12345 There was still a path were the websocket wasn't being cleaned up. I ended up with a chain of finally blocks to make sure it does always get to |
leak is gone for me after latest changes I tested by calling I now only have a few in memory instead of hundreds
|
I am currently running 2023.6.2 with changes from this PR (f69c4ab). However, I haven't been able to trigger the frontend bug since I last restarted core. Annoyingly, it seems to be quite random and I haven't figured out how to reproduce it reliably. |
I am able to get it to send one |
I took it one step further and protected all the attributes on the handler to make sure nothing external is holding a reference to them |
Currently running version 7a9e023 I do have the debug log from last time it happened (before applying the latest patches), but there's way too many lines (155MB). What messages in particular should I be looking for? I don't want to post the whole file as it probably contains access tokens and other sensitive info. I am also experimenting with a simple python script that floods the API with |
I added a repr to the object so you can do a dump on the
|
I'm pretty sure the push to change to replace |
@ondras12345 I'm pretty confident the last set of changes will finally solve this. If you get it to leak again after the latest changes, please post the output of
|
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Github actions seems to be a bit flakey today. Going to merge in dev to get another run just to be sure. |
retest is good. no leaks |
thanks for the review |
I want to add some more coverage for disconnects after creating a subscription and cleanup the typing a bit more but I didn't want to make this any larger so I'll open a new pr to add the additional coverage and typing if the Wi-Fi works inflight tonight |
1 similar comment
I want to add some more coverage for disconnects after creating a subscription and cleanup the typing a bit more but I didn't want to make this any larger so I'll open a new pr to add the additional coverage and typing if the Wi-Fi works inflight tonight |
Proposed change
While looking at home-assistant/frontend#16952 I noticed we suppress propagation of cancellation in the websocket api which can prevent the memory from being recovered on disconnect because it shields the aiohttp internals and the cancelation handler from running.
Reference cycles are now broken when the connection is finished to ensure nothing is holding on to the object so it can be garbage collected.
We now also clear the callable on the ActiveConnection to anything holding on to the connection object does not prevent the handler from being garbage collected.
The ActiveConnection will now handle the case where an unsub of a subscription fails and still unsub the rest of the subscriptions.
I also adds a lot more debug logging (guards with isEnabledFor to prevent a performance regression) and repr of the
ActiveConnection
andWebsocketHandler
objects so any future leaks will be much easier to track down since they can now be dumped in a meaningful way with the profiler integration.related issue home-assistant/frontend#16952 related issue #90281
Type of change
Additional 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
.To help with the load of incoming pull requests: