Skip to content

Commit

Permalink
runtime_vm: Create deleteContainer() helper
Browse files Browse the repository at this point in the history
deleteContainer() function is a helper that can be used for cleaning up
containers in case of error. Instead of taking the risk of duplicating
code, let's take the relevant part out of the DeleteContainer(), and
make it available for future uses.

It's important to note that other functions cannot just call
DeleteContainer() directly if they're holding a container's opLock, as
DeleteContainer() would try to acquire the very same lock, causing then
a happy little accident called "deadlock".

Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
(cherry picked from commit d785c14)
(cherry picked from commit bdfe05c)
  • Loading branch information
fidencio committed Aug 10, 2020
1 parent 35d47cd commit cc3ec80
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/oci/runtime_vm.go
Expand Up @@ -530,22 +530,30 @@ func (r *runtimeVM) DeleteContainer(c *Container) error {
c.opLock.Lock()
defer c.opLock.Unlock()

return r.deleteContainer(c, false)
}

// deleteContainer performs all the operations needed to delete a container.
// force must only be used on clean-up cases.
// It does **not** Lock the container, thus it's the caller responsibility to do so, when needed.
func (r *runtimeVM) deleteContainer(c *Container, force bool) error {
r.Lock()
cInfo, ok := r.ctrs[c.ID()]
r.Unlock()
if !ok {
if !ok && !force {
return errors.New("Could not retrieve container information")
}

if err := cInfo.cio.Close(); err != nil {
if err := cInfo.cio.Close(); err != nil && !force {
return err
}

if err := r.remove(r.ctx, c.ID(), ""); err != nil {
if err := r.remove(r.ctx, c.ID(), ""); err != nil && !force {
return err
}

if _, err := r.task.Shutdown(r.ctx, &task.ShutdownRequest{ID: c.ID()}); err != nil && err != ttrpc.ErrClosed {
_, err := r.task.Shutdown(r.ctx, &task.ShutdownRequest{ID: c.ID()})
if err != nil && err != ttrpc.ErrClosed && !force {
return err
}

Expand Down

0 comments on commit cc3ec80

Please sign in to comment.