You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
nerdctl sometimes fail to check if a container name already exists and ends-up creating duplicates #2992
while true; do sudo ./nerdctl run -d --restart always --name whatevername debian bash; done
Will result after some time with:
apo@oliphant:~ $ sudo ./nerdctl ps -a | grep whatevername
016ee92893a2 docker.io/library/debian:latest "exit 1" About a minute ago Created whatevername
2a9140621055 docker.io/library/debian:latest "exit 1" About a minute ago Created whatevername
3b294997c56c docker.io/library/debian:latest "bash" 51 seconds ago Restarting (0) 8 seconds ago whatevername
5fb34e4cd5a8 docker.io/library/debian:latest "bash" About a minute ago Restarting (0) 8 seconds ago whatevername
86606352a9af docker.io/library/debian:latest "exit 1" About a minute ago Created whatevername
b3eebcb75094 docker.io/library/debian:latest "exit 1" About a minute ago Created whatevername
I am new here, but this is what I gathered from reading the source: looks like the question is managed in namestore.go with a filesystem based store.
Obviously, this is racy, so, I assume either:
there must be a lock mechanism in there that is not doing what it should
or the name is temporarily released for some other reason during the whole exit / restart cycle
I am leaning towards number 2 ^ but any opinion / more informed insight on this would be welcome.
During the whole run/restart cycle, onPostStop is being called, which does release the name.
The race condition happens if another Create is being called after that before the first container is restarted and gets to lock the name again.
The reproducer is actually much simpler - just start a failing container with --restart always, and after some time the name file will just disappear from /var/lib/nerdctl/XXX/names/default/.
Problem now is that I am out of my depth here.
Do you have any insight for me here on what to do to fix this?
I am now thinking the entire namestore is a bad idea and we should rather query containerd instead of trying to maintain our own "db".
This may increase the lookup cost from O(1) to O(n)?
Probably (unless containerd lookup methods are smarter than just enumerating all containers).
It seems to me the OCI lifecycle events are just not enough for us to maintain a copy of the state on our side. postStop just tells us that a container is done - and not whether it is going to be restarted or not.
Here is my current train of thoughts:
Solution A: keep namestore, and patch ocihook
Pretty much, in onStartContainer, call namestore.Acquire again (I have not verified that containerd will actually trigger these in that case - hopefully it does).
It will notfix the problem - but it will alleviate it by making the racy window much shorter - between postStop and onStart for the first (restart always, failing) container.
Solution B: remove namestore entirely, and query containerd
Upside is that we get rid of trying to keep the state on our side (pretty sure there are other ways to fuck it up that this specific issue).
Downside is performance.
Also, I am not 100% positive this won't still be racy (really depends how containerd implements that).
Should an error be returned if the CreateRuntime hook function fails to acquire the name? In this way, there won't be two containers with the same name that are both in the up state simultaneously.
Should an error be returned if the CreateRuntime hook function fails to acquire the name? In this way, there won't be two containers with the same name that are both in the up state simultaneously.
I don't think the "duplicate" situation is much of a problem anymore (but please open a new ticket if it shows back up), as there has been a large amount of patches to address racyness generally.
That being said, this section of the code (and generally CNIEnv) needs to be cleaned-up...
Activity
apostasie commentedon May 12, 2024
Actually, I have a reproducer:
Will result after some time with:
apostasie commentedon May 12, 2024
Maybe the culprit here is the fact that the container almost immediately exits, but has a restart policy?
apostasie commentedon May 12, 2024
@AkihiroSuda this one is really bad
I'll look around and see if I can send a patch.
apostasie commentedon May 12, 2024
I am new here, but this is what I gathered from reading the source: looks like the question is managed in namestore.go with a filesystem based store.
Obviously, this is racy, so, I assume either:
I am leaning towards number 2 ^ but any opinion / more informed insight on this would be welcome.
Thanks!
apostasie commentedon May 12, 2024
Confirming there is a serious issue with the name store:
apo@oliphant:~ $ sudo ls -lA /var/lib/nerdctl/1935db59/names/default/ | grep whatever<- returns nothing - the file in the name store is gone.apostasie commentedon May 12, 2024
Surprisingly, namestore.Release does not seem to ever be called - is there another routine altering the name store?Release is being called somewhere.
This seems to be why this is racy.
Something Releases the name while it should not.
apostasie commentedon May 12, 2024
@AkihiroSuda I need help.
The problem lies in: https://github.com/containerd/nerdctl/blob/main/pkg/ocihook/ocihook.go#L486
During the whole run/restart cycle,
onPostStopis being called, which does release the name.The race condition happens if another
Createis being called after thatbefore the first container is restarted and gets to lock the name again.The reproducer is actually much simpler - just start a failing container with
--restart always, and after some time the name file will just disappear from /var/lib/nerdctl/XXX/names/default/.Problem now is that I am out of my depth here.
Do you have any insight for me here on what to do to fix this?
I am now thinking the entire namestore is a bad idea and we should rather query containerd instead of trying to maintain our own "db".
Thanks in advance.
AkihiroSuda commentedon May 13, 2024
This may increase the lookup cost from O(1) to O(n)?
apostasie commentedon May 13, 2024
Probably (unless containerd lookup methods are smarter than just enumerating all containers).
It seems to me the OCI lifecycle events are just not enough for us to maintain a copy of the state on our side.
postStopjust tells us that a container is done - and not whether it is going to be restarted or not.Here is my current train of thoughts:
Solution A: keep namestore, and patch ocihook
Pretty much, in onStartContainer, call namestore.Acquire again (I have not verified that containerd will actually trigger these in that case - hopefully it does).
It will not fix the problem - but it will alleviate it by making the racy window much shorter - between postStop and onStart for the first (restart always, failing) container.
Solution B: remove namestore entirely, and query containerd
containerNameStore.Acquire(name id)becomesclient.Containers(ctx, fmt.Sprintf("labels.%q==%s", "nerdctl/name", name))Upside is that we get rid of trying to keep the state on our side (pretty sure there are other ways to fuck it up that this specific issue).
Downside is performance.
Also, I am not 100% positive this won't still be racy (really depends how containerd implements that).
Thoughts?
AkihiroSuda commentedon May 13, 2024
I guess we can try A first and see if it practically works. We may try B later if A is still too racy.
apostasie commentedon May 13, 2024
Ok.
I'll send a PR.
Re-acquire name in onStartContainer (see containerd#2992)
Merge pull request #2995 from apostasie/dev-3
apostasie commentedon Jun 16, 2024
@AkihiroSuda
Unfortunately, I am seeing this somewhat often while parallelizing tests.
We might have to revisit this and consider plan B.
yummyMax commentedon Mar 19, 2025
Should an error be returned if the CreateRuntime hook function fails to acquire the name? In this way, there won't be two containers with the same name that are both in the up state simultaneously.
apostasie commentedon Mar 19, 2025
Maybe yes on https://github.com/apostasie/nerdctl/blob/main/pkg/ocihook/ocihook.go#L503
Probably not on https://github.com/apostasie/nerdctl/blob/main/pkg/ocihook/ocihook.go#L505 IIRC - unless much better tested (especially with containers using
--restart,--rm, etc)I don't think the "duplicate" situation is much of a problem anymore (but please open a new ticket if it shows back up), as there has been a large amount of patches to address racyness generally.
That being said, this section of the code (and generally CNIEnv) needs to be cleaned-up...
apostasie commentedon Mar 19, 2025
I'll close this ticket actually.
It specifically refers to the state of affairs back last year - and the codebase around that has changed significantly since then.
By all means though, if you hit this, or a variant of this, please open a new one and let's look into it!