Skip to content

Don't fail on two concurrent reference.store.AddDigest calls - #37781

Merged
yongtang merged 1 commit into
moby:masterfrom
mtrmac:reference-race-upstream
Oct 18, 2018
Merged

yongtang merged 1 commit into
moby:masterfrom
mtrmac:reference-race-upstream

Conversation

@mtrmac

@mtrmac mtrmac commented Sep 6, 2018

Copy link
Copy Markdown
Contributor

- What I did
Fixed a race when two goroutines independently try to add the same digest reference.

- How I did it
See the commit message.

- How to verify it
On a trivial level, using the modified unit tests. Beyond that, whatever is the usual standard, I guess.

It should be in principle possible to make an actual reproducer by concurrently issuing many pull and/or push API requests for the same image (same manifest digest); I haven’t even attempted that so far. The race window is a few lines of Go code in an operation which involves remote HTTP requests, so it might require quite a few concurrent attempts.

I don’t have such a reproducer at this point; the race was found by code inspection based on a reported occurrence of the Cannot overwrite digest error message.

- Description for the changelog
Fixes a race possibly causing one of several concurrent push/pull operations with the same remote image fail with “Cannot overwrite digest …”.

reference.store.addReference fails when adding a digest reference
that already exists (regardless of the reference target).  Both
callers (via reference.store.AddDigest) do check in advance, using
reference.store.Get, whether the digest reference exists before
calling AddDigest, but the reference store lock is released between
the two calls, so if another thread sets the reference in the meantime,
AddDigest may fail with
> Cannot overwrite digest ...
.

Handle this by checking that the pre-existing reference points at the
same image, i.e. that there is nothing to do, and succeeding immediately
in that case.  This is even cheaper, avoids a reference.store.save() call.

(In principle, the same failure could have happened via
reference.store.AddTag, as
> Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option
but almost all callers (except for migrate/v1.Migrate, which is run
single-threaded anyway) set the "force" parameter of AddTag to true,
which makes the race invisible.  This commit does not change the behavior
of that case, except for speeding it up by avoiding the
reference.store.save() call.)

The existing reference.store.Get checks are now, in a sense, redundant
as such, but their existence allows the callers to provide nice
context-dependent error messages, so this commit leaves them unchanged.

Signed-off-by: Miloslav Trmač <[email protected]>

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codecov

codecov Bot commented Sep 15, 2018

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@4842f7a). Click here to learn what that means.
The diff coverage is 100%.

@@            Coverage Diff            @@
##             master   #37781   +/-   ##
=========================================
  Coverage          ?   35.86%           
=========================================
  Files             ?      610           
  Lines             ?    45547           
  Branches          ?        0           
=========================================
  Hits              ?    16336           
  Misses            ?    26964           
  Partials          ?     2247

@yongtang yongtang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yongtang
yongtang merged commit f7e5154 into moby:master Oct 18, 2018
@mtrmac
mtrmac deleted the reference-race-upstream branch October 19, 2018 12:41
nemacysts added a commit to Yelp/paasta that referenced this pull request May 13, 2023
We've since upgraded the version of Docker we run and, according to
moby/moby#37781 - this has now been fixed.

Even without having found that PR, I'm pretty confident that this issue
has been fixed as we pull an ungodly amount of simultaneous images in
our kube nodes and have never seen issues like this :)
nemacysts added a commit to Yelp/paasta that referenced this pull request May 15, 2023
We've since upgraded the version of Docker we run and, according to
moby/moby#37781 - this has now been fixed.

Even without having found that PR, I'm pretty confident that this issue
has been fixed as we pull an ungodly amount of simultaneous images in
our kube nodes and have never seen issues like this :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants