Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid panic on multiple closer.Signal calls #1401

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions y/y.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ func FixedDuration(d time.Duration) string {
// to tell the goroutine to shut down, and a WaitGroup with which to wait for it to finish shutting
// down.
type Closer struct {
closed chan struct{}
waiting sync.WaitGroup
closed chan struct{}
waiting sync.WaitGroup
closeOnce sync.Once
}

// NewCloser constructs a new Closer, with an initial count on the WaitGroup.
Expand All @@ -209,7 +210,10 @@ func (lc *Closer) AddRunning(delta int) {

// Signal signals the HasBeenClosed signal.
func (lc *Closer) Signal() {
close(lc.closed)
// Todo(ibrahim): Change Signal to return error on next badger breaking change.
lc.closeOnce.Do(func() {
close(lc.closed)
jarifibrahim marked this conversation as resolved.
Show resolved Hide resolved
})
}

// HasBeenClosed gets signaled when Signal() is called.
Expand Down
17 changes: 17 additions & 0 deletions y/y_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,20 @@ func TestPagebufferReader4(t *testing.T) {
require.Equal(t, err, io.EOF, "should return EOF")
require.Equal(t, n, 0)
}

func TestMulipleSignals(t *testing.T) {
closer := NewCloser(0)
require.NotPanics(t, func() { closer.Signal() })
// Should not panic.
require.NotPanics(t, func() { closer.Signal() })
require.NotPanics(t, func() { closer.SignalAndWait() })

// Attempt 2.
closer = NewCloser(1)
require.NotPanics(t, func() { closer.Done() })

require.NotPanics(t, func() { closer.SignalAndWait() })
// Should not panic.
require.NotPanics(t, func() { closer.SignalAndWait() })
require.NotPanics(t, func() { closer.Signal() })
}