summaryrefslogtreecommitdiffstats
path: root/modules/sync
diff options
context:
space:
mode:
authorEthan Koenig <etk39@cornell.edu>2017-05-31 04:57:17 -0400
committerLunny Xiao <xiaolunwen@gmail.com>2017-05-31 16:57:17 +0800
commitbfb44f885468839259bc2ead999953cdd85654e1 (patch)
tree1b8b404133dad5ad4650d15a0a9acc6207fb916b /modules/sync
parent0f5b399e351786254f48dc1e5ed3419c3153703f (diff)
downloadgitea-bfb44f885468839259bc2ead999953cdd85654e1.tar.gz
gitea-bfb44f885468839259bc2ead999953cdd85654e1.zip
Fix status table race condition (#1835)
Diffstat (limited to 'modules/sync')
-rw-r--r--modules/sync/status_pool.go12
-rw-r--r--modules/sync/status_pool_test.go9
2 files changed, 21 insertions, 0 deletions
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"))
}