@marcel91200 / caddy Mirror · read-only / commits / 3d0b4fac
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 23:30 UTC

core: Clean up new config if it failed to run (#7068)

WeidiDeng committed Jun 17, 2025 at 03:15 UTC 3d0b4fac5a583d615fe411a4af9a24a7bcc3bee3
1 file changed +15 -3
caddy.go
+15 -3
@@ -408,11 +408,23 @@ func run(newCfg *Config, start bool) (Context, error) {
408 return ctx, nil
409 }
410
411 + defer func() {
412 + // if newCfg fails to start completely, clean up the already provisioned modules
413 + // partially copied from provisionContext
414 + if err != nil {
415 + globalMetrics.configSuccess.Set(0)
416 + ctx.cfg.cancelFunc()
417 +
418 + if currentCtx.cfg != nil {
419 + certmagic.Default.Storage = currentCtx.cfg.storage
420 + }
421 + }
422 + }()
423 +
424 // Provision any admin routers which may need to access
425 // some of the other apps at runtime
426 err = ctx.cfg.Admin.provisionAdminRouters(ctx)
427 if err != nil {
415 - globalMetrics.configSuccess.Set(0)
428 return ctx, err
429 }
430
@@ -438,7 +450,6 @@ func run(newCfg *Config, start bool) (Context, error) {
450 return nil
451 }()
452 if err != nil {
441 - globalMetrics.configSuccess.Set(0)
453 return ctx, err
454 }
455 globalMetrics.configSuccess.Set(1)
@@ -449,7 +460,8 @@ func run(newCfg *Config, start bool) (Context, error) {
460
461 // now that the user's config is running, finish setting up anything else,
462 // such as remote admin endpoint, config loader, etc.
452 - return ctx, finishSettingUp(ctx, ctx.cfg)
463 + err = finishSettingUp(ctx, ctx.cfg)
464 + return ctx, err
465 }
466
467 // provisionContext creates a new context from the given configuration and provisions