Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid race between operation and events listener #12885

Closed
wants to merge 7 commits into from
18 changes: 12 additions & 6 deletions client/lxd_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ func (r *ProtocolLXD) tryCreateContainer(req api.ContainersPost, urls []string)

rop.targetOp = op

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if r.supportsAuthentication {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use SupportsAuthentication everywhere in client so that when we search for usage of this function we get a clear picture of the usage.

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be ignoring the error returned from AddHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen this sort of thing all over the place wherever we try to set up an events listener with getEvents or AddHandler and I didn't understand the reason.

}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -570,8 +572,10 @@ func (r *ProtocolLXD) tryMigrateContainer(source InstanceServer, name string, re

rop.targetOp = op

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if source.SupportsAuthentication() {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -1262,8 +1266,10 @@ func (r *ProtocolLXD) tryMigrateContainerSnapshot(source InstanceServer, contain

rop.targetOp = op

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if source.SupportsAuthentication() {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down
12 changes: 8 additions & 4 deletions client/lxd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,10 @@ func (r *ProtocolLXD) tryCopyImage(req api.ImagesPost, urls []string) (RemoteOpe
rop.targetOp = op
rop.handlerLock.Unlock()

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if r.supportsAuthentication {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -854,8 +856,10 @@ func (r *ProtocolLXD) CopyImage(source ImageServer, image api.Image, args *Image
rop.targetOp = op
rop.handlerLock.Unlock()

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if r.supportsAuthentication {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down
24 changes: 16 additions & 8 deletions client/lxd_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,10 @@ func (r *ProtocolLXD) tryRebuildInstance(instanceName string, req api.InstanceRe
rop.targetOp = op
rop.handlerLock.Unlock()

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if r.supportsAuthentication {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -754,8 +756,10 @@ func (r *ProtocolLXD) tryCreateInstance(req api.InstancesPost, urls []string, op
rop.targetOp = op
rop.handlerLock.Unlock()

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if r.supportsAuthentication {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -1111,8 +1115,10 @@ func (r *ProtocolLXD) tryMigrateInstance(source InstanceServer, name string, req

rop.targetOp = op

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if source.SupportsAuthentication() {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -2110,8 +2116,10 @@ func (r *ProtocolLXD) tryMigrateInstanceSnapshot(source InstanceServer, instance

rop.targetOp = op

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if source.SupportsAuthentication() {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down
12 changes: 8 additions & 4 deletions client/lxd_storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,10 @@ func (r *ProtocolLXD) tryMigrateStoragePoolVolume(source InstanceServer, pool st
chDone: make(chan bool),
}

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if source.SupportsAuthentication() {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down Expand Up @@ -500,8 +502,10 @@ func (r *ProtocolLXD) tryCreateStoragePoolVolume(pool string, req api.StorageVol
chDone: make(chan bool),
}

for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
if r.SupportsAuthentication() {
for _, handler := range rop.handlers {
_, _ = rop.targetOp.AddHandler(handler)
}
}

err = rop.targetOp.Wait()
Expand Down
10 changes: 6 additions & 4 deletions lxc-to-lxd/main_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,12 @@ func convertContainer(d lxd.ContainerServer, container *liblxc.Container, storag
}

progress := cli.ProgressRenderer{Format: "Transferring container: %s"}
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if d.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

rootfs, _ := getRootfs(conf)
Expand Down
20 changes: 12 additions & 8 deletions lxc/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ func (c *cmdAction) doActionAll(action string, resource remoteResource) error {
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if d.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

err = cli.CancelableWait(op, &progress)
Expand Down Expand Up @@ -274,10 +276,12 @@ func (c *cmdAction) doAction(action string, conf *config.Config, nameArg string)
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if d.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for operation to finish
Expand Down
10 changes: 6 additions & 4 deletions lxc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,10 +1254,12 @@ func (c *cmdClusterEvacuateAction) Run(cmd *cobra.Command, args []string) error
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if resource.server.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

err = op.Wait()
Expand Down
20 changes: 12 additions & 8 deletions lxc/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ func (c *cmdCopy) copyInstance(conf *config.Config, sourceResource string, destR
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if dest.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for the copy to complete
Expand Down Expand Up @@ -381,10 +383,12 @@ func (c *cmdCopy) copyInstance(conf *config.Config, sourceResource string, destR
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if dest.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for the copy to complete
Expand Down
10 changes: 6 additions & 4 deletions lxc/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ func (c *cmdExport) Run(cmd *cobra.Command, args []string) error {
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if d.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait until backup is done
Expand Down
18 changes: 11 additions & 7 deletions lxc/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,12 @@ func (c *cmdImageCopy) Run(cmd *cobra.Command, args []string) error {
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if destinationServer.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for operation to finish
Expand Down Expand Up @@ -1373,9 +1375,11 @@ func (c *cmdImageRefresh) Run(cmd *cobra.Command, args []string) error {
}

// Register progress handler
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
return err
if resource.server.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
return err
}
}

// Wait for the refresh to happen
Expand Down
10 changes: 6 additions & 4 deletions lxc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,12 @@ func (c *cmdInit) create(conf *config.Config, args []string) (lxd.InstanceServer
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return nil, "", err
if d.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return nil, "", err
}
}

err = cli.CancelableWait(op, &progress)
Expand Down
10 changes: 6 additions & 4 deletions lxc/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ func (c *cmdLaunch) Run(cmd *cobra.Command, args []string) error {
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if d.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for operation to finish
Expand Down
11 changes: 7 additions & 4 deletions lxc/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,14 @@ func moveClusterInstance(conf *config.Config, sourceResource string, destResourc
Quiet: quiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if source.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for the move to complete
err = cli.CancelableWait(op, &progress)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions lxc/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,12 @@ func (c *cmdPublish) Run(cmd *cobra.Command, args []string) error {
Quiet: c.global.flagQuiet,
}

_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
if s.SupportsAuthentication() {
_, err = op.AddHandler(progress.UpdateOp)
if err != nil {
progress.Done("")
return err
}
}

// Wait for the copy to complete
Expand Down
Loading
Loading