Skip to content

Commit

Permalink
store/tikv: call Next() after copIterator Closed lead to goroutine leak
Browse files Browse the repository at this point in the history
After Close(), worker groutine receive from copIterator.finished and
exit directly, without writing any thing to taskCh.
Next() receives from taskCh and may hang forever, cause the caller goroutine
leak.
  • Loading branch information
tiancaiamao committed Jan 12, 2018
1 parent 76c5083 commit 17c3e3d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion store/tikv/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,15 @@ func (it *copIterator) run(ctx goctx.Context) {
})
}

func recvFromRespCh(respCh <-chan copResponse, finished <-chan struct{}) (resp copResponse, ok bool, exit bool) {
select {
case resp, ok = <-respCh:
case <-finished:
exit = true
}
return
}

func (it *copIterator) sendToTaskCh(ctx goctx.Context, t *copTask, taskCh chan<- *copTask) (finished bool, canceled bool) {
select {
case taskCh <- t:
Expand Down Expand Up @@ -421,13 +430,18 @@ func (it *copIterator) Next() ([]byte, error) {
return nil, nil
}
} else {
var closed bool
for {
if it.curr >= len(it.tasks) {
// Resp will be nil if iterator is finished.
return nil, nil
}
task := it.tasks[it.curr]
resp, ok = <-task.respChan
resp, ok, closed = recvFromRespCh(task.respChan, it.finished)
if closed {
// Close() is already called, so Next() is invalid.
return nil, nil
}
if ok {
break
}
Expand Down

0 comments on commit 17c3e3d

Please sign in to comment.