Skip to content

Commit

Permalink
vc: Remove device when AddDevice encounters error
Browse files Browse the repository at this point in the history
Fixes kata-containers#2295

Signed-off-by: Ted Yu <[email protected]>
  • Loading branch information
yutedz committed Dec 3, 2019
1 parent d054556 commit 9c0872d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions virtcontainers/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1892,17 +1892,28 @@ func (s *Sandbox) AddDevice(info config.DeviceInfo) (api.Device, error) {
return nil, fmt.Errorf("device manager isn't initialized")
}

var err error
b, err := s.devManager.NewDevice(info)
if err != nil {
return nil, err
}
defer func() {
if err != nil {
s.devManager.RemoveDevice(b.DeviceID())
}
}()

if err := s.devManager.AttachDevice(b.DeviceID(), s); err != nil {
if err = s.devManager.AttachDevice(b.DeviceID(), s); err != nil {
return nil, err
}
defer func() {
if err != nil {
s.devManager.DetachDevice(b.DeviceID(), s)
}
}()

if !s.supportNewStore() {
if err := s.storeSandboxDevices(); err != nil {
if err = s.storeSandboxDevices(); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit 9c0872d

Please sign in to comment.