Fix panic when starting up daemon with existing bootstrap

This commit is contained in:
Brian Picciano 2024-07-21 17:20:48 +02:00
parent 1ea16d80e4
commit af69f1cfba

View File

@ -223,13 +223,10 @@ func NewDaemon(
return d, nil return d, nil
} }
// initialize must be called with d.l write lock held, but it will unlock the // initialize must be called with d.l write lock held.
// lock itself.
func (d *daemon) initialize( func (d *daemon) initialize(
ctx context.Context, currBootstrap bootstrap.Bootstrap, ctx context.Context, currBootstrap bootstrap.Bootstrap,
) error { ) error {
defer d.l.Unlock()
// we update this Host's data using whatever configuration has been provided // 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 // by the daemon config. This way the daemon has the most up-to-date
// possible bootstrap. This updated bootstrap will later get updated in // possible bootstrap. This updated bootstrap will later get updated in
@ -493,22 +490,20 @@ func (d *daemon) CreateNetwork(
} }
d.l.Lock() d.l.Lock()
defer d.l.Unlock()
if d.state != daemonStateNoNetwork { if d.state != daemonStateNoNetwork {
d.l.Unlock()
return ErrAlreadyJoined return ErrAlreadyJoined
} }
if len(d.daemonConfig.Storage.Allocations) < 3 { if len(d.daemonConfig.Storage.Allocations) < 3 {
d.l.Unlock()
return ErrInvalidConfig.WithData( return ErrInvalidConfig.WithData(
"At least three storage allocations are required.", "At least three storage allocations are required.",
) )
} }
// initialize will unlock d.l // initialize will unlock d.l
err = d.initialize(ctx, hostBootstrap) if err = d.initialize(ctx, hostBootstrap); err != nil {
if err != nil {
return fmt.Errorf("initializing daemon: %w", err) return fmt.Errorf("initializing daemon: %w", err)
} }
@ -519,21 +514,19 @@ func (d *daemon) JoinNetwork(
ctx context.Context, newBootstrap JoiningBootstrap, ctx context.Context, newBootstrap JoiningBootstrap,
) error { ) error {
d.l.Lock() d.l.Lock()
defer d.l.Unlock()
if d.state != daemonStateNoNetwork { if d.state != daemonStateNoNetwork {
d.l.Unlock()
return ErrAlreadyJoined return ErrAlreadyJoined
} }
err := secrets.Import(ctx, d.secretsStore, newBootstrap.Secrets) err := secrets.Import(ctx, d.secretsStore, newBootstrap.Secrets)
if err != nil { if err != nil {
d.l.Unlock()
return fmt.Errorf("importing secrets: %w", err) return fmt.Errorf("importing secrets: %w", err)
} }
// initialize will unlock d.l // initialize will unlock d.l
err = d.initialize(ctx, newBootstrap.Bootstrap) if err = d.initialize(ctx, newBootstrap.Bootstrap); err != nil {
if err != nil {
return fmt.Errorf("initializing daemon: %w", err) return fmt.Errorf("initializing daemon: %w", err)
} }