-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Conversation
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
/cc @wjhuang2016 @xhebox @tiancaiamao @hawkingrei |
Code Coverage Details: https://codecov.io/github/pingcap/tidb/commit/0f61034c2c3ed4b31107914e98ef54b5c3057178 |
session/session.go
Outdated
tmp, err := s.sysSessionPool().Get() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer s.sysSessionPool().Put(tmp) | ||
se := tmp.(*session) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@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. |
/label needs-cherry-pick-5.4 |
/merge |
This pull request has been accepted and is ready to merge. Commit hash: 07a1f47
|
/merge |
@Defined2014: In response to this:
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. |
/merge |
This pull request has been accepted and is ready to merge. Commit hash: 92cf9e5
|
/run-mysql-test |
/run-unit-test |
/run-check_dev_2 |
/run-mysql-test |
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
cherry pick to release-5.4 in PR #31404 |
What problem does this PR solve?
Issue Number: close #30918
Problem Summary:
What is changed and how it works?
Check List
Tests
Side effects
Documentation
Release note