Skip to content

Commit

Permalink
fix race when multiple defercleanups are called in goroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed May 31, 2023
1 parent 9620623 commit 07fc3a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/internal_integration/cleanup_test.go
Expand Up @@ -2,6 +2,7 @@ package internal_integration_test

import (
"fmt"
"sync"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -285,5 +286,30 @@ var _ = Describe("Cleanup", func() {
Ω(rt).Should(HaveTracked("A", "AE", "AA", "C-AE", "C-A", "C-AA"))
})
})

Context("when cleanup is added in parallel in some goroutines", func() {
BeforeEach(func() {
success, _ := RunFixture("concurrent cleanup", func() {
Context("ordered", Ordered, func() {
It("A", func() {
wg := &sync.WaitGroup{}
wg.Add(5)
for i := 0; i < 5; i++ {
i := i
go func() {
DeferCleanup(rt.Run, fmt.Sprintf("dc-%d", i))
wg.Done()
}()
}
wg.Wait()
})
})
})
Ω(success).Should(BeTrue())
})
It("doesn't race", func() {
Ω(rt.TrackedRuns()).Should(ConsistOf("dc-0", "dc-1", "dc-2", "dc-3", "dc-4"))
})
})
})
})
2 changes: 2 additions & 0 deletions internal/suite.go
Expand Up @@ -245,7 +245,9 @@ func (suite *Suite) pushCleanupNode(node Node) error {

node.NodeIDWhereCleanupWasGenerated = suite.currentNode.ID
node.NestingLevel = suite.currentNode.NestingLevel
suite.selectiveLock.Lock()
suite.cleanupNodes = append(suite.cleanupNodes, node)
suite.selectiveLock.Unlock()

return nil
}
Expand Down

0 comments on commit 07fc3a0

Please sign in to comment.