From b9a000e4ca841b337388426726530ec6015b2416 Mon Sep 17 00:00:00 2001 From: Boqin Qin Date: Mon, 6 Jul 2020 19:28:12 +0800 Subject: [PATCH 1/4] cherry pick #16987 to release-4.0 Signed-off-by: ti-srebot --- util/memory/tracker.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/util/memory/tracker.go b/util/memory/tracker.go index 45ceca71fbb70..7f2cc0de8bff3 100644 --- a/util/memory/tracker.go +++ b/util/memory/tracker.go @@ -158,17 +158,27 @@ func (t *Tracker) Detach() { } func (t *Tracker) remove(oldChild *Tracker) { + found := false t.mu.Lock() - defer t.mu.Unlock() for i, child := range t.mu.children { - if child != oldChild { - continue + if child == oldChild { + t.mu.children = append(t.mu.children[:i], t.mu.children[i+1:]...) + found = true + break } +<<<<<<< HEAD t.Consume(-oldChild.BytesConsumed()) oldChild.parent = nil t.mu.children = append(t.mu.children[:i], t.mu.children[i+1:]...) break +======= + } + t.mu.Unlock() + if found { + oldChild.setParent(nil) + t.Consume(-oldChild.BytesConsumed()) +>>>>>>> a9177fe... util/memory: avoid potential deadlock for Consume in remove (#16987) } } From 142770e7fc8303c6f2282285939189e219ccad58 Mon Sep 17 00:00:00 2001 From: Feng Liyuan Date: Wed, 15 Jul 2020 15:15:11 +0800 Subject: [PATCH 2/4] Update tracker.go --- util/memory/tracker.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/util/memory/tracker.go b/util/memory/tracker.go index 7f2cc0de8bff3..bc0897329456c 100644 --- a/util/memory/tracker.go +++ b/util/memory/tracker.go @@ -166,19 +166,11 @@ func (t *Tracker) remove(oldChild *Tracker) { found = true break } -<<<<<<< HEAD - - t.Consume(-oldChild.BytesConsumed()) - oldChild.parent = nil - t.mu.children = append(t.mu.children[:i], t.mu.children[i+1:]...) - break -======= } t.mu.Unlock() if found { oldChild.setParent(nil) t.Consume(-oldChild.BytesConsumed()) ->>>>>>> a9177fe... util/memory: avoid potential deadlock for Consume in remove (#16987) } } From 152e4d281ca5aa80f95ffe333eaf38ce54e0650c Mon Sep 17 00:00:00 2001 From: Feng Liyuan Date: Wed, 15 Jul 2020 15:20:49 +0800 Subject: [PATCH 3/4] Update tracker.go --- util/memory/tracker.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/util/memory/tracker.go b/util/memory/tracker.go index bc0897329456c..f0655b14c8b56 100644 --- a/util/memory/tracker.go +++ b/util/memory/tracker.go @@ -333,3 +333,9 @@ func (t *Tracker) DetachFromGlobalTracker() { parent.Consume(-t.BytesConsumed()) t.parent = nil } + +func (t *Tracker) setParent(parent *Tracker) { + t.parMu.Lock() + defer t.parMu.Unlock() + t.parMu.parent = parent +} From 20846e8a77415f78e7f20dd3a9830bcf35a2f974 Mon Sep 17 00:00:00 2001 From: Feng Liyuan Date: Wed, 15 Jul 2020 15:24:35 +0800 Subject: [PATCH 4/4] Update tracker.go --- util/memory/tracker.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/util/memory/tracker.go b/util/memory/tracker.go index f0655b14c8b56..cd63dfa4ab2a6 100644 --- a/util/memory/tracker.go +++ b/util/memory/tracker.go @@ -169,7 +169,7 @@ func (t *Tracker) remove(oldChild *Tracker) { } t.mu.Unlock() if found { - oldChild.setParent(nil) + oldChild.parent = nil t.Consume(-oldChild.BytesConsumed()) } } @@ -333,9 +333,3 @@ func (t *Tracker) DetachFromGlobalTracker() { parent.Consume(-t.BytesConsumed()) t.parent = nil } - -func (t *Tracker) setParent(parent *Tracker) { - t.parMu.Lock() - defer t.parMu.Unlock() - t.parMu.parent = parent -}