Fix race condition between task creation and removal#1152
Closed
jinuxstyle wants to merge 1 commit into
Closed
Conversation
This commit fixes a race condition between task creation and removal
which could causes orphan containers. It happens when a agent task is
handling creation event and creating a container, a removal event
comes in and cancels the task's context. Both the creation and
removal would fail but the container would still created by the docker
daemon. The removal fails because when the DELETE request arrives the
docker daemon, the container is still being created.
goroutine (creation) task (removal)
--------- ----
... ...
ctlr.Prepare(ctx)
r.adapter.createNetworks(ctx)
... case <-shutdown
r.adapter.create(ctx) cancel()
//failed due to context cancelled tm.ctlr.Remove(ctx)
//container still createdby daemon //failed due to "No such
//container" error because
//the container not created
//yet
...
//the container created by docker
//daemon but becomes orphan
This patch fixes it by moving the cancel operation to be after the
remove operation. In addition, a lock is added for exclusive access
to the container resouces between creation and removal operations.
These ensures that the creation of a container could either be
finished or not started when the remove operation enters the critical
section. The pull operation is moved out the critical section because
it might be too time consuming.
Signed-off-by: Jin Xu <jinuxstyle@hotmail.com>
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit fixes a race condition between task creation and removal
which could causes orphan containers. It happens when a agent task is
handling creation event and creating a container, a removal event
comes in and cancels the task's context. Both the creation and
removal would fail but the container would still created by the docker
daemon. The removal fails because when the DELETE request arrives the
docker daemon, the container is still being created.
This patch fixes it by moving the cancel operation to be after the
remove operation. In addition, a lock is added for exclusive access
to the container resouces between creation and removal operations.
These ensures that the creation of a container could either be
finished or not started when the remove operation enters the critical
section. The pull operation is moved out the critical section because
it might be too time consuming.
Signed-off-by: Jin Xu jinuxstyle@hotmail.com