@@ -408,11 +408,23 @@ func run(newCfg *Config, start bool) (Context, error) {
408
408
return ctx , nil
409
409
}
410
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
+
411
424
// Provision any admin routers which may need to access
412
425
// some of the other apps at runtime
413
426
err = ctx .cfg .Admin .provisionAdminRouters (ctx )
414
427
if err != nil {
415
- globalMetrics .configSuccess .Set (0 )
416
428
return ctx , err
417
429
}
418
430
@@ -438,7 +450,6 @@ func run(newCfg *Config, start bool) (Context, error) {
438
450
return nil
439
451
}()
440
452
if err != nil {
441
- globalMetrics .configSuccess .Set (0 )
442
453
return ctx , err
443
454
}
444
455
globalMetrics .configSuccess .Set (1 )
@@ -449,7 +460,8 @@ func run(newCfg *Config, start bool) (Context, error) {
449
460
450
461
// now that the user's config is running, finish setting up anything else,
451
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
453
465
}
454
466
455
467
// provisionContext creates a new context from the given configuration and provisions
0 commit comments