Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
34823: stop: allow double-calling Stop() r=petermattis,knz a=tbg

Stop() can be called multiple times and it's better to embrace that fact
than to try to fight it. See

https://github.com/cockroachdb/cockroach/blob/89ce71e9b733df3855b370aa20bb809db3d4362c/pkg/cli/start.go#L794-L809

Fixes cockroachdb#34572

Release note (bug fix): Fix a rare crash ("close of closed channel")
that could occur when shutting down a server.

Co-authored-by: Tobias Schottdorf <tobias.schottdorf@gmail.com>
  • Loading branch information
craig[bot] and tbg committed Feb 12, 2019
2 parents 695a6cc + 518f739 commit 7c4b2c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/util/stop/stopper.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ type Stopper struct {
idAlloc int
qCancels map[int]func()
sCancels map[int]func()

stopCalled bool // turns all but first call to Stop into noop
}
}

Expand Down Expand Up @@ -448,6 +450,15 @@ func (s *Stopper) runningTasksLocked() TaskMap {
// Stop signals all live workers to stop and then waits for each to
// confirm it has stopped.
func (s *Stopper) Stop(ctx context.Context) {
s.mu.Lock()
stopCalled := s.mu.stopCalled
s.mu.stopCalled = true
s.mu.Unlock()

if stopCalled {
return
}

defer s.Recover(ctx)
defer unregister(s)

Expand Down
2 changes: 2 additions & 0 deletions pkg/util/stop/stopper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func TestStopperIsStopped(t *testing.T) {
case <-time.After(time.Second):
t.Fatal("stopper should have finished stopping")
}

s.Stop(context.Background())
}

func TestStopperMultipleStopees(t *testing.T) {
Expand Down

0 comments on commit 7c4b2c8

Please sign in to comment.