Uptime Kuma 2 with uptime-kuma-api 1.x: socket timeouts and a dead healer
I've got a small Python script I call the healer. Every 5 minutes it logs in to Uptime Kuma, asks which monitors are down, and tries to fix them. If a monitor is down for 2 runs in a row it restarts the service. If that
I've got a small Python script I call the healer. Every 5 minutes it logs in to Uptime Kuma, asks which monitors are down, and tries to fix them. If a monitor is down for 2 runs in a row it restarts the service. If that doesn't bring it back, it reboots the container, with a cap on how many reboots it'll do in an hour. It's the bit of my homelab that's meant to fix things while I'm asleep.
On 9 August 2026 I opened its log. It was about 2.4 MB of Python tracebacks. It had been dead for months.
Not dead once, either. Dead twice, for two completely different reasons, one after the other. And next to it, a second watcher of mine had the opposite problem: it was far too keen, and it was restarting a perfectly healthy service every 10 minutes.
Same lesson in both, which is why they're in one post.
Phase 1: the wrong password, ~1,915 times
The first block of failures was boring. authIncorrectCreds, about 1,915 of them. The healer had the wrong Kuma password.
The fix was to give it the right one. It reads the password from the environment, then from a local .env file, and deliberately never from my secrets manager at runtime. That's on purpose. The box it runs on had flaky outbound connections back then, and a monitoring script shouldn't need a network round-trip to check whether the network is up.
Fixed that, ran it, and it still failed. Just differently.
Phase 2: Kuma says yes, the client never hears it
The second block was about 1,892 socketio TimeoutError failures. This is the one that had really been killing it for months.
The odd thing was where it failed. The socket connected instantly. The login went through. Kuma's own server log said so:
[AUTH] Successfully logged in user ...
But the acknowledgement of that login never reached the Python client. It sat waiting for an answer the server thought it had already sent, then timed out.
The cause is a version mismatch. The server was Uptime Kuma 2.4.0. The library was uptime-kuma-api 1.2.1, which is written for Kuma 1.x. It mostly works against Kuma 2, which is what makes it sneaky. It just doesn't reliably get that login ack.
Things that didn't help, so you don't have to try them:
- A longer timeout. 300 seconds failed exactly like 15 seconds. The ack isn't slow, it's not coming.
- Downgrading python-socketio. I tried going from 5.16.3 down to 5.11.4 and 5.9.0. Nothing changed reliably.
What does help is a fresh connection. The ack lands on most attempts, just not all of them. So the fix is retries, each one with a brand new client and a short timeout:
for attempt in range(1, KUMA_CONNECT_TRIES + 1):
api = None
try:
api = UptimeKumaApi(KUMA_URL, timeout=KUMA_TIMEOUT_SEC)
api.login(KUMA_USER, KUMA_PASSWORD)
return api
except Exception as e:
last = e
if api is not None:
try:
api.disconnect()
except Exception:
pass
if attempt < KUMA_CONNECT_TRIES:
time.sleep(KUMA_RETRY_WAIT_SEC)
raise RuntimeError(...)
5 tries, 15 seconds each. If every attempt fails it alerts Discord at most once an hour and exits 1, instead of dumping a traceback every 5 minutes. After the change it logged in 8 runs out of 8, usually on attempt 1 to 3.
If you're debugging this yourself, the step that settles it is reading the server's log, not the client's. The client just says timeout. The server tells you the login worked, and that's what points you at the protocol instead of the password or the network.
The bug hiding behind it
With the healer actually running again I watched it for a bit, and one monitor (my 3D printer) went down, then "all up", then down again across three runs. It wasn't flapping. The healer was lying.
Fetching a monitor's recent heartbeats can time out the same way the login does. The code wrapped that call in except: pass. So a failed fetch meant no "down" beat was found, which meant the monitor counted as up. That reset its consecutive-down count to zero, so it might never reach the threshold of 2 that triggers a fix at all.
Now that call retries 3 times, and if it still fails the monitor is reported as unknown, not up. Unknown monitors keep their down-count. And after a restart, if the re-check comes back unknown, it never escalates to rebooting a container. Not being able to ask isn't the same as it being fine, and it isn't the same as it being broken either.
Meanwhile, the opposite problem
Same day, a different script. I've got a security watcher I call Sentinel that runs every 10 minutes and checks my CrowdSec engine is alive, over SSH. For 24 hours it had been posting this to Discord about every 10 minutes:
🚨 CrowdSec LAPI was **<empty>**
and restarting CrowdSec each time.
CrowdSec was never down. Its journal showed nothing but clean stops, always right on the cron boundary. That was my own script restarting it.
The empty string was the clue. The check did systemctl is-active crowdsec over SSH and restarted anything that wasn't active. But systemctl is-active prints the state even when the unit is dead. It exits non-zero and still prints failed or inactive. Empty output only ever means one thing: I didn't get an answer at all. The SSH connection had failed, and the script was reading "couldn't reach the box" as "the service is dead".
The fix is a function that returns None when the box can't be reached, and a real state otherwise:
def unit_state(host, unit):
rc, out, _ = ssh_retry("root", host, f"systemctl is-active {unit}")
out = (out or "").strip().splitlines()[-1].strip() if (out or "").strip() else ""
return out or None
SSH gets 3 tries with a 3 second wait. A real non-active state still restarts. None posts an alert saying no restart was attempted because the health is unknown, and leaves the service alone.
The same bug was in two other checks. The one that counts failed logins on my SSO box recorded 0 when it couldn't read the log. Then the next run saw the real count, worked out a massive jump, and shouted brute-force attack. Now it skips the run and keeps the old number. The failed-SSH check got the same treatment.
After deploying it: zero restarts, normal "all clear" runs, and a real triple SSH failure produced the "unknown" alert instead of a restart.
Why was SSH failing in the first place? Probes from that box were failing to every other box about 20 to 30% of the time. That turned out to be a whole other story, two containers sharing one network identity, which I covered in my DHCP lease churn post. But the watcher should never have depended on SSH being perfect.
One more thing I found
Even if the healer had been alerting all along, I wouldn't have heard. The helper that posts to Discord was fetching an old bot token and getting 401 Unauthorized on every send. Quietly. So the healer's alerts about its own failures were failing too. It reads the working token now, and I checked a send comes back true.
I also added log rotation for both scripts, because 2.4 MB of tracebacks is how this hid for so long. Small gotcha there: on my boxes /var/log is root:syslog with 775, and logrotate skips files under it as "insecure permissions" unless you add su root syslog to the config.
What I'd do differently
- Three answers, not two. Up, down and unknown. Any check that can't tell "down" from "couldn't ask" will eventually either restart something healthy or ignore something broken. Mine managed both at once.
- Empty output is a fact. If a command always prints something when it works, empty means it didn't run. Treat it as its own case.
- Check the client library supports your server's major version. The library I was using targets Kuma 1.x. Against Kuma 2 it works just well enough to fool you.
- Monitor the monitor. A healer that's been dead for months looks exactly like a homelab where nothing broke.
How long would it take you to notice your own auto-fix had stopped? For me it was months.
🤖 Drafted with AI assistance from my own homelab notes, logs and repos, and fact-checked against them before publishing.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.
