Repository navigation
Conversation
text_string_to_metric_families() and building the monitor models are CPU-bound and scale with the number of monitors. Running them on the event loop can stall the host application (e.g. Home Assistant) for seconds at a time. Offload them with asyncio.to_thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
|
Closing: testing on a live instance showed that moving the parse off the event loop doesn't fix the freeze reported in home-assistant/core#178608 (polls took ~0.1s; the stall happened during entity setup). I'll revisit when I have more time. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UptimeKuma.metrics()parses the Prometheus response (text_string_to_metric_families) and builds theUptimeKumaMonitormodels directly on the event loop. That work is CPU-bound and grows with the number of monitors, so on a larger Kuma instance it can block the caller's event loop. In Home Assistant this shows up as the whole instance freezing, with unrelated integrations logging "took longer than the scheduled update interval" at the same moment (home-assistant/core#178608).This PR moves the parsing into a module-level
_parse_metrics()helper and runs it withasyncio.to_thread. The HTTP request and error handling stay on the loop. The public API and return values don't change, andself.versionis still updated whenapp_versionis present.Changes
pythonkuma/uptimekuma.py: parsing and model building moved into_parse_metrics(text), which returns(version, monitors).metrics()awaits it viaasyncio.to_thread.UptimeKumaParseExceptionis still raised on invalid input.tests/test_metrics.py: new test checking that the parser runs on a worker thread, not the event-loop thread.Related: home-assistant/core#178608
🤖 Generated with Claude Code