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

backend: fix panic when reading result set fails #486

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/proxy/backend/cmd_processor_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func (cp *CmdProcessor) query(packetIO *pnet.PacketIO, sql string) (result *mysq
err = errors.WithStack(mysql.ErrMalformPacket)
default:
var rs *mysql.Result
rs, err = cp.readResultSet(packetIO, response)
result = rs.Resultset
if rs, err = cp.readResultSet(packetIO, response); err == nil {
result = rs.Resultset
}
}
return
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/proxy/backend/cmd_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,35 @@ func TestNetworkError(t *testing.T) {
ts.query(t, proxyErrChecker)
clean()
}

// Test that TiProxy won't panic when query encounters an error.
func TestQueryError(t *testing.T) {
tc := newTCPConnSuite(t)
tests := []struct {
cfg cfgOverrider
c checker
}{
{
cfg: func(cfg *testConfig) {
cfg.backendConfig.abnormalExit = true
},
},
{
cfg: func(cfg *testConfig) {
cfg.backendConfig.respondType = responseTypeErr
},
},
{
cfg: func(cfg *testConfig) {
cfg.backendConfig.respondType = responseTypeResultSet
cfg.backendConfig.columns = 1
cfg.backendConfig.exitInResult = true
},
},
}
for _, test := range tests {
ts, clean := newTestSuite(t, tc, test.cfg)
ts.query(t, func(t *testing.T, ts *testSuite) {})
clean()
}
}
7 changes: 7 additions & 0 deletions pkg/proxy/backend/mock_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type backendConfig struct {
proxyProtocol bool
authSucceed bool
abnormalExit bool
exitInResult bool
}

func newBackendConfig() *backendConfig {
Expand Down Expand Up @@ -293,6 +294,12 @@ func (mb *mockBackend) writeResultSet(packetIO *pnet.PacketIO, names []string, v
return err
}
}
if mb.exitInResult {
if err := packetIO.Flush(); err != nil {
return err
}
return packetIO.Close()
}

if status&mysql.SERVER_STATUS_CURSOR_EXISTS == 0 {
if mb.capability&pnet.ClientDeprecateEOF == 0 {
Expand Down