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 many leaks of the test case #26909

Merged
merged 3 commits into from
Aug 6, 2021
Merged

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Problem Summary:

When I tried to figure out the DATA RACE in #26241, I found those problems.

The PR #26241 does not introduce the DATA RACE itself, but as it reuse statement context, if some goroutine still reference the old statement context, then the DATA RACE happen.
It help us find those leaks, typically such case:

_, err : = tk.Exec("select xx ...")
// the record set is not closed and ignored... leak!
// so the background executor goroutine is still using the statement context

tk.MustExec("another sql ...")  
// DATA RACE, the reuse of the statement context conflicts with the previous executor goroutine

What is changed and how it works?

What's Changed:

Fix some leak places.

How it Works:

Release resource to avoid leak

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Most of the change is the test code.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Aug 5, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • xhebox
  • xiongjiwei

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 5, 2021
@@ -298,14 +300,17 @@ func (e *HashAggExec) Close() error {

// Open implements the Executor Open interface.
func (e *HashAggExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return err
}
failpoint.Inject("mockHashAggExecBaseExecutorOpenReturnedError", func(val failpoint.Value) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The failpoint is moved before the Open, because after calling Open and then error, the children Executor is not closed.

return err
}
// If panic in Open, the children executor should be closed because they are open.
defer closeBaseExecutor(&e.baseExecutor)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the panic in Open case, we do not handle the resource release well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually here is not just a problem in the test code, it's also a bug of the product.

set @@tidb_memtracker_size = xxx;
insert into select * from t1 where a in select b from t2 ... 
// If Out of memory, panic in Open, then there will be goroutine leak 

@github-actions github-actions bot added sig/execution SIG execution sig/sql-infra SIG: SQL Infra labels Aug 5, 2021
@@ -44,6 +44,10 @@ func (e *ExplainExec) Open(ctx context.Context) error {
// Close implements the Executor Close interface.
func (e *ExplainExec) Close() error {
e.rows = nil
if e.analyzeExec != nil && !e.executed {
// Open(), but Next() is not called.
return e.analyzeExec.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.

Some test code run the following logic:

rs, err := Execute("explan analyze xxxxx")

// Next() is not called

rs.Close()

And the analyzeExec is not released ...
Fix for it.

@@ -57,16 +57,22 @@ func (s *seqTestSuite) TestPrepared(c *C) {
tk.MustExec("create table prepare_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1)")
tk.MustExec("insert prepare_test (c1) values (1),(2),(NULL)")

tk.MustExec(`prepare stmt_test_1 from 'select id from prepare_test where id > ?'; set @a = 1; execute stmt_test_1 using @a;`)
tk.MustExec(`prepare stmt_test_1 from 'select id from prepare_test where id > ?';`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Multiple statement is ban in the real code.
Some test code are still using them ... and the resulted record set seems not to be closed correctly.

@@ -1378,9 +1378,6 @@ func upgradeToVer67(s Session, ver int64) {
if err != nil {
logutil.BgLogger().Fatal("upgradeToVer67 error", zap.Error(err))
}
if rs != nil {
defer terror.Call(rs.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.

The defer close the result too late, do it explicitly.

func() {
    t1, err := Execute("sql1 ...")
    defer t1.Close()

    t2, err := Execute("sql2" ...)
    // t1 is not closed, so statement context is still in use 
    // and we will have trouble reuse the statement context.
}

Copy link
Contributor

@xhebox xhebox left a comment

Choose a reason for hiding this comment

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

I remembered that recordset.Close() has an error returned, do we assert it? I will give lgtm, if you don't want to do it now.

executor/aggregate.go Outdated Show resolved Hide resolved
executor/executor_pkg_test.go Show resolved Hide resolved
planner/core/integration_test.go Show resolved Hide resolved
session/bootstrap_test.go Outdated Show resolved Hide resolved
@tiancaiamao
Copy link
Contributor Author

I remembered that recordset.Close() has an error returned, do we assert it? I will give lgtm, if you don't want to do it now.

In the test case many places ignore the error. golint does not check the ignored error in the test code currently.

executor/aggregate.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 6, 2021
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Aug 6, 2021
@tiancaiamao
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 947f271

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Aug 6, 2021
@ti-chi-bot
Copy link
Member

@tiancaiamao: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot ti-chi-bot merged commit a8adc4c into pingcap:master Aug 6, 2021
@tiancaiamao tiancaiamao deleted the fix-leak branch August 6, 2021 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. sig/execution SIG execution sig/sql-infra SIG: SQL Infra size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants