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

session: fix data race in ParseWithParamsInternal #31058

Merged
merged 9 commits into from
Jan 6, 2022

Conversation

Defined2014
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #30918

Problem Summary:

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No 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

fix data race in `ParseWithParamsInternal`

Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Dec 27, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • wjhuang2016
  • xhebox

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 Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 27, 2021
@Defined2014
Copy link
Contributor Author

/cc @wjhuang2016 @xhebox @tiancaiamao @hawkingrei
PTAL

@sre-bot
Copy link
Contributor

sre-bot commented Dec 27, 2021

Comment on lines 1533 to 1538
tmp, err := s.sysSessionPool().Get()
if err != nil {
return nil, err
}
defer s.sysSessionPool().Put(tmp)
se := tmp.(*session)
Copy link
Contributor

Choose a reason for hiding this comment

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

The design of this function is problematic, the parse and the execute is not using the same session.
If the SQL has some side-effect, for example, a warning message, it's left on the pool's session.

A better solution is a callback style API:

func (s *session) runWithInternalSQLExec(fn func(exec sqlexec.RestrictedSQLExec) error) {
    tmp, err := s.sysSessionPool().Get()
	if err != nil {
		return nil, err
	}
	defer s.sysSessionPool().Put(tmp)
    se := tmp.(*session)
    return fn(se)
}

The call use it like this:

se.runWithInternalSQLExec(func(exec *exec.RestrictedSQLExec) {
    stmt := exec.ParseWithParamsInternal(...)
    exec.Exec(stmt)
    ...
})

And you can finally wrap a simpler API like:

func (s *session) ExecRestrictedSQL(sql string) {
    s.runWithInternalSQLExec(func(exec sqlexec.RestrictedSQLExec) {
        stmt := exec.ParseWithParamsInternal(...)
        exec.Exec(stmt)
        ...        
    })
}

The simplied API is more convenient, but its design is not secure.

Copy link
Contributor Author

@Defined2014 Defined2014 Dec 28, 2021

Choose a reason for hiding this comment

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

But we didn't use same session before. And I don't know it's a bug or a feature 😭.
The ExecRestrictedStmt will get a session from session pool, and it is not same as parser's.

Copy link
Contributor

Choose a reason for hiding this comment

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

But we didn't use same session before. And I don't know it's a bug or feature 😭.

Before a previous refactoring, they're in the same session.
The data race is introduced after that refactoring

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix the data race fisrt, it may affects 5.4.0.

@tiancaiamao
Copy link
Contributor

@hawkingrei are you also working on this? we should avoid repeated working

@hawkingrei
Copy link
Member

hawkingrei commented Dec 28, 2021

@hawkingrei are you also working on this? we should avoid repeated working

No, I am not refactoring. I just fix the data race problem.

@hawkingrei
Copy link
Member

@hawkingrei are you also working on this? we should avoid repeated working

No, I am not refactoring. I just fix the data race problem.

I can provide the necessary help.

@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 6, 2022
@Defined2014
Copy link
Contributor Author

/label needs-cherry-pick-5.4

@ti-chi-bot ti-chi-bot added the needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. label Jan 6, 2022
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jan 6, 2022
@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 Jan 6, 2022
@xhebox
Copy link
Contributor

xhebox commented Jan 6, 2022

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 07a1f47

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jan 6, 2022
@ti-chi-bot ti-chi-bot removed the status/can-merge Indicates a PR has been approved by a committer. label Jan 6, 2022
@Defined2014
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

@Defined2014: /merge is only allowed for the committers, you can assign this pull request to the committer in list by filling /assign @committer in the comment to help merge this pull request.

In response to this:

/merge

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.

@xhebox
Copy link
Contributor

xhebox commented Jan 6, 2022

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 92cf9e5

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jan 6, 2022
@Defined2014
Copy link
Contributor Author

/run-mysql-test

@Defined2014
Copy link
Contributor Author

/run-unit-test

@Defined2014
Copy link
Contributor Author

/run-check_dev_2

@Defined2014
Copy link
Contributor Author

/run-mysql-test

@ti-chi-bot ti-chi-bot merged commit 5cd37f0 into pingcap:master Jan 6, 2022
@Defined2014 Defined2014 deleted the 30918 branch January 6, 2022 13:02
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jan 6, 2022
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.4 in PR #31404

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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.

DATA RACE in the SessionVars.InRestrictedSQL
8 participants