From af69f1cfbac8b793e47a002d9006070571ab4400 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 21 Jul 2024 17:20:48 +0200 Subject: [PATCH] Fix panic when starting up daemon with existing bootstrap --- go/daemon/daemon.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/go/daemon/daemon.go b/go/daemon/daemon.go index 4257b3c..45a7165 100644 --- a/go/daemon/daemon.go +++ b/go/daemon/daemon.go @@ -223,13 +223,10 @@ func NewDaemon( return d, nil } -// initialize must be called with d.l write lock held, but it will unlock the -// lock itself. +// initialize must be called with d.l write lock held. func (d *daemon) initialize( ctx context.Context, currBootstrap bootstrap.Bootstrap, ) error { - defer d.l.Unlock() - // we update this Host's data using whatever configuration has been provided // by the daemon config. This way the daemon has the most up-to-date // possible bootstrap. This updated bootstrap will later get updated in @@ -493,22 +490,20 @@ func (d *daemon) CreateNetwork( } d.l.Lock() + defer d.l.Unlock() if d.state != daemonStateNoNetwork { - d.l.Unlock() return ErrAlreadyJoined } if len(d.daemonConfig.Storage.Allocations) < 3 { - d.l.Unlock() return ErrInvalidConfig.WithData( "At least three storage allocations are required.", ) } // initialize will unlock d.l - err = d.initialize(ctx, hostBootstrap) - if err != nil { + if err = d.initialize(ctx, hostBootstrap); err != nil { return fmt.Errorf("initializing daemon: %w", err) } @@ -519,21 +514,19 @@ func (d *daemon) JoinNetwork( ctx context.Context, newBootstrap JoiningBootstrap, ) error { d.l.Lock() + defer d.l.Unlock() if d.state != daemonStateNoNetwork { - d.l.Unlock() return ErrAlreadyJoined } err := secrets.Import(ctx, d.secretsStore, newBootstrap.Secrets) if err != nil { - d.l.Unlock() return fmt.Errorf("importing secrets: %w", err) } // initialize will unlock d.l - err = d.initialize(ctx, newBootstrap.Bootstrap) - if err != nil { + if err = d.initialize(ctx, newBootstrap.Bootstrap); err != nil { return fmt.Errorf("initializing daemon: %w", err) }