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

fix: shutdown deadlock of pipe when its ring is full (#108) #113

Merged
merged 1 commit into from
Oct 30, 2022
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
8 changes: 6 additions & 2 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ func (p *pipe) _background() {
}
go func() {
exit(p._backgroundWrite())
for atomic.LoadInt32(&p.waits) != 0 {
if _, _, ch := p.queue.NextWriteCmd(); ch == nil {
runtime.Gosched()
}
}
close(wait)
}()
{
Expand All @@ -228,7 +233,6 @@ func (p *pipe) _background() {
atomic.AddInt32(&p.waits, -1)
}()
}
<-wait

p.nsubs.Close()
p.psubs.Close()
Expand All @@ -253,7 +257,6 @@ func (p *pipe) _background() {
p.onInvalidations(nil)
}
for atomic.LoadInt32(&p.waits) != 0 {
p.queue.NextWriteCmd()
if ones[0], multi, ch, cond = p.queue.NextResultCh(); ch != nil {
if multi == nil {
multi = ones
Expand All @@ -269,6 +272,7 @@ func (p *pipe) _background() {
runtime.Gosched()
}
}
<-wait
atomic.StoreInt32(&p.state, 4)
}

Expand Down
27 changes: 27 additions & 0 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,33 @@ func TestExitOnWriteMultiError(t *testing.T) {
}
}

func TestExitOnRingFullAndConnError(t *testing.T) {
p, mock, _, closeConn := setup(t, ClientOption{
RingScaleEachConn: 1,
})
p.background()

// fill the ring
for i := 0; i < len(p.queue.(*ring).store); i++ {
go func() {
if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).Error(); err != io.EOF && !strings.HasPrefix(err.Error(), "io:") {
t.Errorf("unexpected result, expected io err, got %v", err)
}
}()
}
// let writer loop over the ring
for i := 0; i < len(p.queue.(*ring).store); i++ {
mock.Expect("GET", "a")
}

time.Sleep(time.Second) // make sure the writer is waiting for the next write
closeConn()

if err := p.Do(context.Background(), cmds.NewCompleted([]string{"GET", "a"})).Error(); err != io.EOF && !strings.HasPrefix(err.Error(), "io:") {
t.Errorf("unexpected result, expected io err, got %v", err)
}
}

func TestExitAllGoroutineOnWriteError(t *testing.T) {
conn, mock, _, closeConn := setup(t, ClientOption{})

Expand Down