From 90ecc380ce19cb6d9395b5e2b61748cd755fcb78 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 13 Mar 2018 18:12:33 -0700 Subject: [PATCH] funding: fix race conditions in tests by guarding r.lastUpdated w/ a mtx --- fundingmanager.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fundingmanager.go b/fundingmanager.go index 3f265e8c9..d27087ed4 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -70,6 +70,7 @@ type reservationWithCtx struct { chanAmt btcutil.Amount + updateMtx sync.RWMutex lastUpdated time.Time updates chan *lnrpc.OpenStatusUpdate @@ -78,6 +79,9 @@ type reservationWithCtx struct { // isLocked checks the reservation's timestamp to determine whether it is locked. func (r *reservationWithCtx) isLocked() bool { + r.updateMtx.RLock() + defer r.updateMtx.RUnlock() + // The time zero value represents a locked reservation. return r.lastUpdated.IsZero() } @@ -85,11 +89,17 @@ func (r *reservationWithCtx) isLocked() bool { // lock locks the reservation from zombie pruning by setting its timestamp to the // zero value. func (r *reservationWithCtx) lock() { + r.updateMtx.Lock() + defer r.updateMtx.Unlock() + r.lastUpdated = time.Time{} } // updateTimestamp updates the reservation's timestamp with the current time. func (r *reservationWithCtx) updateTimestamp() { + r.updateMtx.Lock() + defer r.updateMtx.Unlock() + r.lastUpdated = time.Now() }