0

I am using a C# API that talks to DockerEngine (Docker.DotNet) to do some things, but notably, in this case, removed a stopped container. Here is my code:

foreach (var container in RunningContainers)
{
    if (container.State == "dead" || container.State == "exited")
    {
        long exitType = Client.Containers.InspectContainerAsync(container.ID).Result.State.ExitCode;

        if (exitType != 0)
        {
            badExitCodeCount++;
        }
        Client.Containers.RemoveContainerAsync(container.ID, new ContainerRemoveParameters()).Wait();
    }
}

Note that in this case RunningContainers still includes the stopped containers and hasn't had its state refreshed yet.

Here's the bug. The API successfully calls out to DockerEngine after executing RemoveContainerAsync, the container is removed from my local Docker Containers. Then shortly after the code moves on, a Docker "No such container" response is received, throwing an exception.

Any ideas about what's going on here?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
zkatancik
  • 1
  • 3

1 Answers1

0

The problem lied within multiple threads running the method at the same time, tricky race condition I missed.

zkatancik
  • 1
  • 3