core: Reloading with SIGUSR1 if config never changed via admin#7258
core: Reloading with SIGUSR1 if config never changed via admin#7258francislavoie merged 3 commits intomasterfrom
SIGUSR1 if config never changed via admin#7258Conversation
009964b to
613cca5
Compare
mholt
left a comment
There was a problem hiding this comment.
Neat, thanks for working on this.
I have a few suggestions/comments, but overall looking good 👍
|
Also sorry for the lint error. I guess I rebased with a bad commit on master. |
e1ab3ab to
4041406
Compare
|
Hm, no, I'm starting to think the linter is borked. It just started erroring out of nowhere. |
There was a problem hiding this comment.
Thank you Francis!
There is an error: var errReloadFromSourceUnavailable is unused (unused) -- but it seems to be a false positive, as it is used in sigtrap_posix.
EDIT: Ohhh the Windows linter failed here. That makes sense since sigtrap_posix is only defined on Posix systems. Can we put a //nolint:unused on that line?
4041406 to
c607ce7
Compare
c607ce7 to
4fac48b
Compare
It's worth noting that using the CLI,
Using the API directly is similar. That's a lot of knowledge for a Caddy user to have, and a lot of burden for them for something simple like giving Caddy a nudge when a certificate on disk has changed. Having typed that all out maybe this would be welcome as an issue 🤣 should I raise it? |
Ah good point, yeah we could fetch the config in the command to compare
I'm pretty sure it supports unix sockets too just fine. AFAIK you can just specify
That's probably the wrong way to do it. You should probably set up https://caddyserver.com/docs/json/admin/remote/ which lets you use certs to authenticate. Either way, all this seems way out of scope of this PR, this is meant for users of Caddyfile (or other flat config files, whatever adapter), not API users. If you're using the API then it's obviously very much within your control to use the API the "right way" like you described. |
Sorry I only intended to write a quick note, but that comment got out of hand.
Ah. I was going off the docs which says:
I'm totally new to Go, but it can't hurt for me to have a try when I get some free time 👍
Yes sorry that was a bad example. |
This is a long-standing issue (since 2.0!) where we haven't support reloading via signals because of the redesign of config handling, being that we'd support changing config via the admin API.
There's only one scenario that makes sense for us to support reloading via signal, and that's when you run Caddy with
caddy run(and notcaddy startorcaddy file-serveretc), because the process which loaded the config is the same as the one which would receive the signal, and we actually know the config file used when initially loading loading.If running with
caddy run --resume(e.g. start from the saved config, i.e. in an API-first setup) then reloading viaSIGUSR1also doesn't make sense, since you should also be using the API to trigger the reload.So to make this possible, we record the last known config file (and config adapter) during
cmdRun()(i.e.caddy run), and we also register a callback to perform the reload (necessary because Caddy's core cannot use config adapters since it would cause circular dependencies, so it needs to be deferred back to thecmdpackage to perform the config change).If we receive any config change via API (
caddy reloadwhich calls/load, or APIs call to/configfor full or partial changes), then we clear the last known config. We also pass a header incaddy reload's API call with the known config file & adapter, so we can check "is the config file different?" so we can retain the last config if it's still the same file.And finally, the signal handling: if we get a
SIGUSR1signal, then it acts like a forced config reload (even if the config is unchanged, reload anyway), but only if we have a last known config recorded.Assistance Disclosure
Used RooCode + GPT4.1 for most of the work on this, with lots of iteration and manual adjustments along the way.