From bfb44f885468839259bc2ead999953cdd85654e1 Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Wed, 31 May 2017 04:57:17 -0400 Subject: Fix status table race condition (#1835) --- modules/sync/status_pool.go | 12 ++++++++++++ modules/sync/status_pool_test.go | 9 +++++++++ 2 files changed, 21 insertions(+) (limited to 'modules/sync') diff --git a/modules/sync/status_pool.go b/modules/sync/status_pool.go index 46d15aa08c..acbd93ab17 100644 --- a/modules/sync/status_pool.go +++ b/modules/sync/status_pool.go @@ -24,6 +24,18 @@ func NewStatusTable() *StatusTable { } } +// StartIfNotRunning sets value of given name to true if not already in pool. +// Returns whether set value was set to true +func (p *StatusTable) StartIfNotRunning(name string) bool { + p.lock.Lock() + _, ok := p.pool[name] + if !ok { + p.pool[name] = struct{}{} + } + p.lock.Unlock() + return !ok +} + // Start sets value of given name to true in the pool. func (p *StatusTable) Start(name string) { p.lock.Lock() diff --git a/modules/sync/status_pool_test.go b/modules/sync/status_pool_test.go index eef3ff6f4f..b388c50db2 100644 --- a/modules/sync/status_pool_test.go +++ b/modules/sync/status_pool_test.go @@ -18,6 +18,15 @@ func Test_StatusTable(t *testing.T) { table.Start("xyz") assert.True(t, table.IsRunning("xyz")) + assert.False(t, table.StartIfNotRunning("xyz")) + assert.True(t, table.IsRunning("xyz")) + + table.Stop("xyz") + assert.False(t, table.IsRunning("xyz")) + + assert.True(t, table.StartIfNotRunning("xyz")) + assert.True(t, table.IsRunning("xyz")) + table.Stop("xyz") assert.False(t, table.IsRunning("xyz")) } -- cgit v1.2.3