From a9c3a9821b4948bd30772b9a39eb3c6a2aa68154 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 29 Mar 2016 11:56:39 -0700 Subject: [PATCH] bitseq: fix races Signed-off-by: Alexander Morozov --- libnetwork/bitseq/sequence.go | 2 ++ libnetwork/bitseq/store.go | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/libnetwork/bitseq/sequence.go b/libnetwork/bitseq/sequence.go index 0dc1bc4ad0..550bcbb825 100644 --- a/libnetwork/bitseq/sequence.go +++ b/libnetwork/bitseq/sequence.go @@ -370,6 +370,8 @@ func (h *Handle) set(ordinal, start, end uint64, any bool, release bool) (uint64 // checks is needed because to cover the case where the number of bits is not a multiple of blockLen func (h *Handle) validateOrdinal(ordinal uint64) error { + h.Lock() + defer h.Unlock() if ordinal >= h.bits { return fmt.Errorf("bit does not belong to the sequence") } diff --git a/libnetwork/bitseq/store.go b/libnetwork/bitseq/store.go index df50331227..5448927eb1 100644 --- a/libnetwork/bitseq/store.go +++ b/libnetwork/bitseq/store.go @@ -75,6 +75,10 @@ func (h *Handle) CopyTo(o datastore.KVObject) error { defer h.Unlock() dstH := o.(*Handle) + if h == dstH { + return nil + } + dstH.Lock() dstH.bits = h.bits dstH.unselected = h.unselected dstH.head = h.head.getCopy() @@ -83,6 +87,7 @@ func (h *Handle) CopyTo(o datastore.KVObject) error { dstH.dbIndex = h.dbIndex dstH.dbExists = h.dbExists dstH.store = h.store + dstH.Unlock() return nil }