Skip to content

Commit

Permalink
address a deadlock in Conn.Close()
Browse files Browse the repository at this point in the history
Signed-off-by: Yaroslav Kolomiiets <[email protected]>
  • Loading branch information
yarikk authored and nemith committed Dec 9, 2019
1 parent 1d8f2ce commit 7f9aac4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,12 @@ func (c *Conn) queueRequest(opcode int32, req interface{}, res interface{}, recv
switch opcode {
case opClose:
// always attempt to send close ops.
c.sendChan <- rq
select {
case c.sendChan <- rq:
case <-time.After(c.connectTimeout * 2):
c.logger.Printf("gave up trying to send opClose to server")
rq.recvChan <- response{-1, ErrConnectionClosed}
}
default:
// otherwise avoid deadlocks for dumb clients who aren't aware that
// the ZK connection is closed yet.
Expand Down
25 changes: 25 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,28 @@ func TestRecurringReAuthHang(t *testing.T) {
t.Fatal(ctx.Err())
}
}

func TestDeadlockInClose(t *testing.T) {
c := &Conn{
shouldQuit: make(chan struct{}),
connectTimeout: 1 * time.Second,
sendChan: make(chan *request, sendChanSize),
logger: DefaultLogger,
}

for i := 0; i < sendChanSize; i++ {
c.sendChan <- &request{}
}

okChan := make(chan struct{})
go func() {
c.Close()
close(okChan)
}()

select {
case <-okChan:
case <-time.After(3 * time.Second):
t.Fatal("apparent deadlock!")
}
}

0 comments on commit 7f9aac4

Please sign in to comment.