Skip to content

Commit

Permalink
Merge pull request #1168 from stgraber/cli
Browse files Browse the repository at this point in the history
client: Fix error handling in push mode copy
  • Loading branch information
brauner authored Aug 31, 2024
2 parents e3d4153 + 67dee19 commit 214b2d6
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions client/incus_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func (r *ProtocolIncus) CopyInstance(source InstanceServer, instance api.Instanc
target.Certificate = info.Certificate
sourceReq.Target = &target

return r.tryMigrateInstance(source, instance.Name, sourceReq, info.Addresses)
return r.tryMigrateInstance(source, instance.Name, sourceReq, info.Addresses, op)
}

// Get source server connection information
Expand Down Expand Up @@ -999,7 +999,7 @@ func (r *ProtocolIncus) RenameInstance(name string, instance api.InstancePost) (

// tryMigrateInstance attempts to migrate a specific instance from a source server to one of the target URLs.
// The function runs the migration operation asynchronously and returns a RemoteOperation to track the progress and handle any errors.
func (r *ProtocolIncus) tryMigrateInstance(source InstanceServer, name string, req api.InstancePost, urls []string) (RemoteOperation, error) {
func (r *ProtocolIncus) tryMigrateInstance(source InstanceServer, name string, req api.InstancePost, urls []string, op Operation) (RemoteOperation, error) {
if len(urls) == 0 {
return nil, fmt.Errorf("The target server isn't listening on the network")
}
Expand All @@ -1011,6 +1011,9 @@ func (r *ProtocolIncus) tryMigrateInstance(source InstanceServer, name string, r
operation := req.Target.Operation

// Forward targetOp to remote op
chConnect := make(chan error, 1)
chWait := make(chan error, 1)

go func() {
success := false
var errors []remoteOperationResult
Expand Down Expand Up @@ -1044,10 +1047,35 @@ func (r *ProtocolIncus) tryMigrateInstance(source InstanceServer, name string, r
break
}

if !success {
rop.err = remoteOperationError("Failed instance migration", errors)
if success {
chConnect <- nil
close(chConnect)
} else {
chConnect <- remoteOperationError("Failed instance migration", errors)
close(chConnect)

if op != nil {
_ = op.Cancel()
}
}
}()

if op != nil {
go func() {
chWait <- op.Wait()
close(chWait)
}()
}

go func() {
var err error

select {
case err = <-chConnect:
case err = <-chWait:
}

rop.err = err
close(rop.chDone)
}()

Expand Down

0 comments on commit 214b2d6

Please sign in to comment.