Skip to content

Commit 39db256

Browse files
committed
Clean up failed app provisioning with defer
1 parent 5051972 commit 39db256

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

context.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
393393
return nil, fmt.Errorf("module value cannot be null")
394394
}
395395

396+
var err error
397+
396398
// if this is an app module, keep a reference to it,
397399
// since submodules may need to reference it during
398400
// provisioning (even though the parent app module
@@ -402,12 +404,17 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
402404
// module has been configured for DNS challenges)
403405
if appModule, ok := val.(App); ok {
404406
ctx.cfg.apps[id] = appModule
407+
defer func() {
408+
if err != nil {
409+
delete(ctx.cfg.apps, id)
410+
}
411+
}()
405412
}
406413

407414
ctx.ancestry = append(ctx.ancestry, val)
408415

409416
if prov, ok := val.(Provisioner); ok {
410-
err := prov.Provision(ctx)
417+
err = prov.Provision(ctx)
411418
if err != nil {
412419
// incomplete provisioning could have left state
413420
// dangling, so make sure it gets cleaned up
@@ -417,18 +424,12 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error
417424
err = fmt.Errorf("%v; additionally, cleanup: %v", err, err2)
418425
}
419426
}
420-
421-
// if this was an app module that failed to provision,
422-
// remove it from the apps map to prevent it from being started at all
423-
if _, ok := val.(App); ok {
424-
delete(ctx.cfg.apps, id)
425-
}
426427
return nil, fmt.Errorf("provision %s: %v", modInfo, err)
427428
}
428429
}
429430

430431
if validator, ok := val.(Validator); ok {
431-
err := validator.Validate()
432+
err = validator.Validate()
432433
if err != nil {
433434
// since the module was already provisioned, make sure we clean up
434435
if cleanerUpper, ok := val.(CleanerUpper); ok {

0 commit comments

Comments
 (0)