Skip to content

Commit

Permalink
profiler: Start() doesn't unlock mutex on error (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge authored Jan 26, 2021
1 parent 663dff7 commit d7e00ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
// the WithAPIKey option, or if a hostname is not found.
func Start(opts ...Option) error {
mu.Lock()
defer mu.Unlock()
if activeProfiler != nil {
activeProfiler.stop()
}
Expand All @@ -39,7 +40,6 @@ func Start(opts ...Option) error {
}
activeProfiler = p
activeProfiler.run()
mu.Unlock()
return nil
}

Expand Down
10 changes: 8 additions & 2 deletions profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ func TestStart(t *testing.T) {
})

t.Run("options/GoodAPIKey", func(t *testing.T) {
_, err := newProfiler(WithAPIKey("12345678901234567890123456789012"))
err := Start(WithAPIKey("12345678901234567890123456789012"))
defer Stop()
assert.Nil(t, err)
})

t.Run("options/BadAPIKey", func(t *testing.T) {
_, err := newProfiler(WithAPIKey("aaaa"))
err := Start(WithAPIKey("aaaa"))
defer Stop()
assert.NotNil(t, err)

// Check that mu gets unlocked, even if newProfiler() returns an error.
mu.Lock()
mu.Unlock()
})
}

Expand Down

0 comments on commit d7e00ea

Please sign in to comment.