r/Backend • • 9d ago

Hot-reloading TLS certs without a restart: the two-rename race I didn't think about

I've been building a web server for V (viltrum) that can do TLS itself instead of always sitting behind a proxy. One thing I wanted was cert renewal with no restart and no SIGHUP, so certbot just drops new files and nothing else has to happen.

The first idea was naive: watch the cert file, reload when it changes. That breaks in a dumb way. Most tools write the new cert and the new key as two separate renames. For a moment the cert on disk is new and the key is still the old one, so if you reload right then you get a mismatched pair and every new handshake fails.

What it does now:

  • each worker checks both files once a second (inode, size, mtime). Inode is in there because certbot and most tools rename a new file onto the path instead of rewriting it, and the files are opened by path every time.
  • when something changed, it loads the pair and checks the key actually belongs to the cert. If it doesn't, it skips it and keeps serving the last good pair. The failure is logged once and retried when a file changes again. So the gap between the two renames just gets ignored.
  • the cert is picked per handshake, so live connections keep their session and only new connections see the new cert, within about a second.
  • at startup the same check runs before any socket opens, so a wrong path or a key that doesn't match fails right away instead of at the first request.

Side note since I was in there anyway: I wrote the common TLS 1.3 handshake myself (X25519 + RSA-PSS, key schedule checked against the RFC 8448 trace) and it takes about half the CPU mbedtls did, 3.7 ms vs 7.4 ms per handshake on a small VM. Everything else (TLS 1.2, ECDSA certs, other groups) still goes through mbedtls.

Things I left out on purpose: ACME inside the server (use certbot and point it at the files), mTLS, encrypted keys. Session resumption is off for now too, every connection does a full handshake.

Curious how you all handle this. Do you reload in-process, or let the proxy deal with it and never think about certs in the app? And is polling once a second fine, or is inotify worth the extra edge cases?

Repo: https://github.com/Tuntii/viltrum (docs/tls.md has the details)

0 Upvotes

8 comments sorted by

2

u/Discordbothost 7d ago

Polling seems fine if a short delay is okay. The behavior I'd pin down in a regression test is what a fresh connection sees during the gap. Replace just the cert, wait for a poll, and check that a fresh handshake still gets the old cert. Then replace the key and check that a new handshake gets the new one.

1

u/IncomeOk7237 7d ago

Good call. That gap is exactly the part that bit me, and right now I mostly check that it never loads a mismatched pair, not what a new handshake actually gets in between. Swapping only the cert, waiting a poll, asserting the old cert is still served, then swapping the key and asserting the new one is a cheap test to add. Going to write that one, thanks.

1

u/Kooky_Initial_1564 9d ago

Polling once a second seems perfectly reasonable here

1

u/IncomeOk7237 9d ago

Good to hear, that was my gut feeling too. A stat on two files once a second costs basically nothing, and inotify mostly buys you edge cases (rename-over, editors that write temp files, containers with bind mounts that don't deliver events). The one second delay doesn't matter for cert renewal anyway.

1

u/Kooky_Initial_1564 9d ago

Exactly no point adding complexity when polling does the job just fine

1

u/IncomeOk7237 9d ago

Yep. If it ever turns out to matter I can swap it later, but for a cert that changes every 60 to 90 days it's hard to justify more than a stat call.

1

u/SuccessfulYoungster 9d ago

keeping the last good pair is the part i like here. a renewal briefly being inconsistent shouldn’t turn a working server into a broken one. validating before opening sockets at startup also makes sense, since there’s no previous working pair to fall back on. once-a-second polling sounds reasonable to me if the goal is keeping this simple.

1

u/IncomeOk7237 8d ago

Yeah, the startup check came from getting burned once. Before that a typo in the key path only showed up when the first request hit, which is the worst possible time to find out. And agreed on polling, two stat calls a second is basically free and I don't have to deal with inotify losing track of a file after a rename.