Skip to content

Commit 9f2db8e

Browse files
authored
backend: fix panic when reading result set fails (#486)
1 parent 384e746 commit 9f2db8e

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

pkg/proxy/backend/cmd_processor_query.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ func (cp *CmdProcessor) query(packetIO *pnet.PacketIO, sql string) (result *mysq
3939
err = errors.WithStack(mysql.ErrMalformPacket)
4040
default:
4141
var rs *mysql.Result
42-
rs, err = cp.readResultSet(packetIO, response)
43-
result = rs.Resultset
42+
if rs, err = cp.readResultSet(packetIO, response); err == nil {
43+
result = rs.Resultset
44+
}
4445
}
4546
return
4647
}

pkg/proxy/backend/cmd_processor_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,3 +1068,35 @@ func TestNetworkError(t *testing.T) {
10681068
ts.query(t, proxyErrChecker)
10691069
clean()
10701070
}
1071+
1072+
// Test that TiProxy won't panic when query encounters an error.
1073+
func TestQueryError(t *testing.T) {
1074+
tc := newTCPConnSuite(t)
1075+
tests := []struct {
1076+
cfg cfgOverrider
1077+
c checker
1078+
}{
1079+
{
1080+
cfg: func(cfg *testConfig) {
1081+
cfg.backendConfig.abnormalExit = true
1082+
},
1083+
},
1084+
{
1085+
cfg: func(cfg *testConfig) {
1086+
cfg.backendConfig.respondType = responseTypeErr
1087+
},
1088+
},
1089+
{
1090+
cfg: func(cfg *testConfig) {
1091+
cfg.backendConfig.respondType = responseTypeResultSet
1092+
cfg.backendConfig.columns = 1
1093+
cfg.backendConfig.exitInResult = true
1094+
},
1095+
},
1096+
}
1097+
for _, test := range tests {
1098+
ts, clean := newTestSuite(t, tc, test.cfg)
1099+
ts.query(t, func(t *testing.T, ts *testSuite) {})
1100+
clean()
1101+
}
1102+
}

pkg/proxy/backend/mock_backend_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type backendConfig struct {
2828
proxyProtocol bool
2929
authSucceed bool
3030
abnormalExit bool
31+
exitInResult bool
3132
}
3233

3334
func newBackendConfig() *backendConfig {
@@ -293,6 +294,12 @@ func (mb *mockBackend) writeResultSet(packetIO *pnet.PacketIO, names []string, v
293294
return err
294295
}
295296
}
297+
if mb.exitInResult {
298+
if err := packetIO.Flush(); err != nil {
299+
return err
300+
}
301+
return packetIO.Close()
302+
}
296303

297304
if status&mysql.SERVER_STATUS_CURSOR_EXISTS == 0 {
298305
if mb.capability&pnet.ClientDeprecateEOF == 0 {

0 commit comments

Comments
 (0)