Skip to content
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

(*Cache).SetOptions does not check for default intervals #331

Open
jameshartig opened this issue Feb 13, 2025 · 0 comments
Open

(*Cache).SetOptions does not check for default intervals #331

jameshartig opened this issue Feb 13, 2025 · 0 comments

Comments

@jameshartig
Copy link

jameshartig commented Feb 13, 2025

What version of the package are you using?

v0.19.2

What are you trying to do?

Start caddy without any tls but multiple http servers. We have a boilerplate default config (below) and then modify the config at runtime using the admin server.

What steps did you take?

Started caddy with the following default config:

{
  "admin": {
    "listen": "unix//var/run/caddy/caddy.sock|0600"
  },
  "apps": {
    "http": {
      "grace_period": "30s",
      "servers": {
        "default": {
          "automatic_https": {
            "disable": true
          },
          "idle_timeout": "650s",
          "listen": [
            ":7080"
          ],
          "max_header_bytes": 1048576,
          "metrics": {},
          "read_header_timeout": "1m",
          "read_timeout": "3m",
          "routes": [
            {
              "@id": "$last-default-404",
              "handle": [
                {
                  "handler": "static_response",
                  "status_code": "404"
                }
              ]
            }
          ],
          "write_timeout": "5m"
        },
        "metrics": {
          "automatic_https": {
            "disable": true
          },
          "listen": [
            ":7081"
          ],
          "routes": [
            {
              "@id": "$last-metrics-404",
              "handle": [
                {
                  "handler": "static_response",
                  "status_code": "404"
                }
              ]
            }
          ]
        }
      },
      "shutdown_delay": "5s"
    }
  },
  "logging": {
    "logs": {
      "default": {
        "level": "ERROR"
      }
    },
    "sink": {
      "writer": {
        "output": "stderr"
      }
    }
  },
  "storage": {
    "module": "file_system",
    "root": "/var/lib/caddy"
  }
}

I believe we didn't try to initially configure caddy until after it started to log the below panic.

We do make a small configuration change as soon as the server starts which is probably what triggered this.

What did you expect to happen, and what actually happened instead?

I expected it to start up correctly.

There was a panic on startup caused by a timer being started with a non-positive interval.

{"level":"info","ts":1739472209.4884524,"msg":"no autosave file exists","autosave_file":"/opt/caddy/autosave.json"}
{"level":"info","ts":1739472209.4886894,"msg":"using provided configuration","config_file":"/opt/caddy/caddy.json","config_adapter":""}
{"level":"error","ts":1739472209.526402,"logger":"tls.cache.maintenance","msg":"panic","cache":"0xc000478180","error":"non-positive interval for NewTicker","stack":"goroutine 12 [running]:\ngithub.com/caddyserver/certmagic.(*Cache).maintainAssets.func1()\n\tgithub.com/caddyserver/[email protected]/maintain.go:48 +0x85\npanic({0x16b4c80?, 0x1e6e450?})\n\truntime/panic.go:914 +0x21f\ntime.NewTicker(0xc0002b4070?)\n\ttime/tick.go:22 +0xe5\ngithub.com/caddyserver/certmagic.(*Cache).maintainAssets(0xc000478180, 0x0)\n\tgithub.com/caddyserver/[email protected]/maintain.go:57 +0x207\ncreated by github.com/caddyserver/certmagic.NewCache in goroutine 1\n\tgithub.com/caddyserver/[email protected]/cache.go:127 +0x1f6\n"}
... repeats several times ...

This doesn't happen every time as its a race condition between (*Cache)SetOptions being called and the maintainAssets goroutine being scheduled.

How do you think this should be fixed?

The (*Cache).SetOptions should do the same thing as NewCache and check for non-positive intervals and set them to their defaults.

Please link to any related issues, pull requests, and/or discussion

It seems like the TLS module in caddy does which I believe to be the only possible place that could have caused this. If SetOptions is called before the maintainAssets is scheduled then it could set the intervals to 0. I haven't figured out why the TLS app is being provisioned but I believe it has something to do with the http app always having a default TLS app.

	certCacheMu.Lock()
	if certCache == nil {
		certCache = certmagic.NewCache(cacheOpts)
	} else {
		certCache.SetOptions(cacheOpts)
	}
	certCacheMu.Unlock()

Bonus: What do you use CertMagic for, and do you find it useful?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant