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

[24.0 backport] daemon: daemon.containerRestart: don't cancel restart on context cancel #46697

Merged
merged 1 commit into from Oct 26, 2023

Commits on Oct 24, 2023

  1. daemon: daemon.containerRestart: don't cancel restart on context cancel

    commit def549c passed through the context
    to the daemon.ContainerStart function. As a result, restarting containers
    no longer is an atomic operation, because a context cancellation could
    interrupt the restart (between "stopping" and "(re)starting"), resulting
    in the container being stopped, but not restarted.
    
    Restarting a container, or more factually; making a successful request on
    the `/containers/{id]/restart` endpoint, should be an atomic operation.
    
    This patch uses a context.WithoutCancel for restart requests.
    
    It's worth noting that daemon.containerStop already uses context.WithoutCancel,
    so in that function, we'll be wrapping the context twice, but this should
    likely not cause issues (just redundant for this code-path).
    
    Before this patch, starting a container that bind-mounts the docker socket,
    then restarting itself from within the container would cancel the restart
    operation. The container would be stopped, but not started after that:
    
        docker run -dit --name myself -v /var/run/docker.sock:/var/run/docker.sock docker:cli sh
        docker exec myself sh -c 'docker restart myself'
    
        docker ps -a
        CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                       PORTS     NAMES
        3a2a741c65ff   docker:cli    "docker-entrypoint.s…"   26 seconds ago   Exited (128) 7 seconds ago             myself
    
    With this patch: the stop still cancels the exec, but does not cancel the
    restart operation, and the container is started again:
    
        docker run -dit --name myself -v /var/run/docker.sock:/var/run/docker.sock docker:cli sh
        docker exec myself sh -c 'docker restart myself'
        docker ps
        CONTAINER ID   IMAGE        COMMAND                  CREATED              STATUS         PORTS     NAMES
        4393a01f7c75   docker:cli   "docker-entrypoint.s…"   About a minute ago   Up 4 seconds             myself
    
    Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
    (cherry picked from commit aeb8972)
    Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
    thaJeztah committed Oct 24, 2023
    Configuration menu
    Copy the full SHA
    05d7386 View commit details
    Browse the repository at this point in the history