moby/reference
Miloslav Trmač f29dda9acd Don't fail on two concurrent reference.store.AddDigest calls
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č <mitr@redhat.com>
2018-09-07 00:06:04 +02:00
..
errors.go Add canonical import comment 2018-02-05 16:51:57 -05:00
store.go Don't fail on two concurrent reference.store.AddDigest calls 2018-09-07 00:06:04 +02:00
store_test.go Don't fail on two concurrent reference.store.AddDigest calls 2018-09-07 00:06:04 +02:00