-
Notifications
You must be signed in to change notification settings - Fork 6.2k
server: check connection is available in SQLKiller (#60685) #68201
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ import ( | |
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
| "unsafe" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
| "github.com/pingcap/errors" | ||
|
|
@@ -3707,3 +3708,82 @@ func TestAuditPluginRetrying(t *testing.T) { | |
| runExplicitTransactionRetry(db, true) | ||
| }) | ||
| } | ||
|
|
||
| func TestIssue57531(t *testing.T) { | ||
| ts := servertestkit.CreateTidbTestSuite(t) | ||
|
|
||
| processlistCount := func(dbt *testkit.DBTestKit) int { | ||
| rsCnt := 0 | ||
| rs := dbt.MustQuery("show processlist") | ||
| for rs.Next() { | ||
| rsCnt++ | ||
| } | ||
| require.NoError(t, rs.Err()) | ||
| require.NoError(t, rs.Close()) | ||
| return rsCnt | ||
| } | ||
|
|
||
| for i := range 2 { | ||
| ts.RunTests(t, nil, func(dbt *testkit.DBTestKit) { | ||
| var netConn net.Conn | ||
| conn, err := dbt.GetDB().Conn(context.Background()) | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| _ = conn.Close() | ||
| }() | ||
|
|
||
| // get the TCP connection | ||
| err = conn.Raw(func(driverConn any) error { | ||
| v := reflect.ValueOf(driverConn) | ||
| if v.Kind() == reflect.Ptr { | ||
| v = v.Elem() | ||
| } | ||
| f := v.FieldByName("netConn") | ||
| if f.IsValid() && f.Type().Implements(reflect.TypeOf((*net.Conn)(nil)).Elem()) { | ||
| netConn = *(*net.Conn)(unsafe.Pointer(f.UnsafeAddr())) | ||
| } | ||
| return nil | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, netConn) | ||
|
|
||
| // execute `select sleep(300)` | ||
| queryDone := make(chan struct{}) | ||
| go func() { | ||
| defer close(queryDone) | ||
| if i == 0 { | ||
| rows, err := conn.QueryContext(context.Background(), "select sleep(300)") | ||
| if err == nil { | ||
| _ = rows.Close() | ||
| } | ||
| } else { | ||
| stmt, err := conn.PrepareContext(context.Background(), "select sleep(?)") | ||
| if err == nil { | ||
| defer stmt.Close() | ||
| _, _ = stmt.Exec(300) | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| // have two sessions | ||
| require.Eventually(t, func() bool { | ||
| return processlistCount(dbt) == 2 | ||
| }, time.Second, time.Millisecond*10) | ||
|
|
||
| // close tcp connection | ||
| require.NoError(t, netConn.Close()) | ||
|
|
||
| select { | ||
| case <-queryDone: | ||
| case <-time.After(time.Second * 3): | ||
| require.Fail(t, "query did not exit after closing the TCP connection") | ||
| } | ||
| _ = conn.Close() | ||
|
|
||
| // the `select sleep(300)` is killed | ||
| require.Eventually(t, func() bool { | ||
| return processlistCount(dbt) == 1 | ||
| }, time.Second*3, time.Millisecond*10) | ||
|
Comment on lines
+3729
to
+3786
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait for the Line 3769 can become true immediately after Line 3729 opens Suggested tightening- // have two sessions
- require.Eventually(t, func() bool {
- return processlistCount(dbt) == 2
- }, time.Second, time.Millisecond*10)
+ hasRunningSleep := func() bool {
+ rs := dbt.MustQuery("select info from information_schema.processlist where info like 'select sleep%'")
+ defer func() {
+ require.NoError(t, rs.Close())
+ }()
+ ok := rs.Next()
+ require.NoError(t, rs.Err())
+ return ok
+ }
+ require.Eventually(t, hasRunningSleep, time.Second, time.Millisecond*10)As per coding guidelines, test changes should stay minimal and deterministic. 🤖 Prompt for AI Agents |
||
| }) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.