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

store/tikv: call Next() after copIterator closed lead to goroutine leak #5624

Merged
merged 2 commits into from
Jan 15, 2018
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
33 changes: 33 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -2302,3 +2303,35 @@ func (s *testSuite) TestMaxInt64Handle(c *C) {
tk.MustExec("delete from t where id = 9223372036854775807")
tk.MustQuery("select * from t").Check(nil)
}

func (s *testSuite) TestEarlyClose(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table earlyclose (id int primary key)")

// Insert 1000 rows.
var values []string
for i := 0; i < 1000; i++ {
values = append(values, fmt.Sprintf("(%d)", i))
}
tk.MustExec("insert earlyclose values " + strings.Join(values, ","))

// Get table ID for split.
dom := sessionctx.GetDomain(tk.Se)
is := dom.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("earlyclose"))
c.Assert(err, IsNil)
tblID := tbl.Meta().ID

// Split the table.
s.cluster.SplitTable(s.mvccStore, tblID, 500)

for i := 0; i < 500; i++ {
rss, err := tk.Se.Execute("select * from earlyclose order by id")
c.Assert(err, IsNil)
rs := rss[0]
_, err = rs.Next()
c.Assert(err, IsNil)
rs.Close()
}
}
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just close the task.respChan in copIterator.Close?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because send to a closed channel would panic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/closed/exit/ or s/exit/closed/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think closed is better, you see the comment line in the next line:
// Close() is already called, so Next() is invalid.

@zz-jason

if closed {
// Close() is already called, so Next() is invalid.
return nil, nil
}
if ok {
break
}
Expand Down