Skip to content

Commit bfb44f8

Browse files
ethantkoeniglunny
authored andcommitted
Fix status table race condition (#1835)
1 parent 0f5b399 commit bfb44f8

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

models/repo.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,10 +1881,9 @@ func DeleteRepositoryArchives() error {
18811881

18821882
// DeleteOldRepositoryArchives deletes old repository archives.
18831883
func DeleteOldRepositoryArchives() {
1884-
if taskStatusTable.IsRunning(archiveCleanup) {
1884+
if !taskStatusTable.StartIfNotRunning(archiveCleanup) {
18851885
return
18861886
}
1887-
taskStatusTable.Start(archiveCleanup)
18881887
defer taskStatusTable.Stop(archiveCleanup)
18891888

18901889
log.Trace("Doing: ArchiveCleanup")
@@ -2025,10 +2024,9 @@ const (
20252024

20262025
// GitFsck calls 'git fsck' to check repository health.
20272026
func GitFsck() {
2028-
if taskStatusTable.IsRunning(gitFsck) {
2027+
if !taskStatusTable.StartIfNotRunning(gitFsck) {
20292028
return
20302029
}
2031-
taskStatusTable.Start(gitFsck)
20322030
defer taskStatusTable.Stop(gitFsck)
20332031

20342032
log.Trace("Doing: GitFsck")
@@ -2097,10 +2095,9 @@ func repoStatsCheck(checker *repoChecker) {
20972095

20982096
// CheckRepoStats checks the repository stats
20992097
func CheckRepoStats() {
2100-
if taskStatusTable.IsRunning(checkRepos) {
2098+
if !taskStatusTable.StartIfNotRunning(checkRepos) {
21012099
return
21022100
}
2103-
taskStatusTable.Start(checkRepos)
21042101
defer taskStatusTable.Stop(checkRepos)
21052102

21062103
log.Trace("Doing: CheckRepoStats")

models/repo_mirror.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,9 @@ func DeleteMirrorByRepoID(repoID int64) error {
202202

203203
// MirrorUpdate checks and updates mirror repositories.
204204
func MirrorUpdate() {
205-
if taskStatusTable.IsRunning(mirrorUpdate) {
205+
if !taskStatusTable.StartIfNotRunning(mirrorUpdate) {
206206
return
207207
}
208-
taskStatusTable.Start(mirrorUpdate)
209208
defer taskStatusTable.Stop(mirrorUpdate)
210209

211210
log.Trace("Doing: MirrorUpdate")

models/user.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,10 +1334,9 @@ func GetWatchedRepos(userID int64, private bool) ([]*Repository, error) {
13341334

13351335
// SyncExternalUsers is used to synchronize users with external authorization source
13361336
func SyncExternalUsers() {
1337-
if taskStatusTable.IsRunning(syncExternalUsers) {
1337+
if !taskStatusTable.StartIfNotRunning(syncExternalUsers) {
13381338
return
13391339
}
1340-
taskStatusTable.Start(syncExternalUsers)
13411340
defer taskStatusTable.Stop(syncExternalUsers)
13421341

13431342
log.Trace("Doing: SyncExternalUsers")

modules/sync/status_pool.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ func NewStatusTable() *StatusTable {
2424
}
2525
}
2626

27+
// StartIfNotRunning sets value of given name to true if not already in pool.
28+
// Returns whether set value was set to true
29+
func (p *StatusTable) StartIfNotRunning(name string) bool {
30+
p.lock.Lock()
31+
_, ok := p.pool[name]
32+
if !ok {
33+
p.pool[name] = struct{}{}
34+
}
35+
p.lock.Unlock()
36+
return !ok
37+
}
38+
2739
// Start sets value of given name to true in the pool.
2840
func (p *StatusTable) Start(name string) {
2941
p.lock.Lock()

modules/sync/status_pool_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ func Test_StatusTable(t *testing.T) {
1818
table.Start("xyz")
1919
assert.True(t, table.IsRunning("xyz"))
2020

21+
assert.False(t, table.StartIfNotRunning("xyz"))
22+
assert.True(t, table.IsRunning("xyz"))
23+
24+
table.Stop("xyz")
25+
assert.False(t, table.IsRunning("xyz"))
26+
27+
assert.True(t, table.StartIfNotRunning("xyz"))
28+
assert.True(t, table.IsRunning("xyz"))
29+
2130
table.Stop("xyz")
2231
assert.False(t, table.IsRunning("xyz"))
2332
}

0 commit comments

Comments
 (0)