Skip to content

Commit

Permalink
server: fix panic in api regions/meta (pingcap#10222)
Browse files Browse the repository at this point in the history
  • Loading branch information
winkyao authored and ngaut committed May 9, 2019
1 parent c2e83af commit 990e256
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/gorilla/mux"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/parser/model"
Expand Down Expand Up @@ -519,6 +520,16 @@ func (t *tikvHandlerTool) getRegionsMeta(regionIDs []uint64) ([]RegionMeta, erro
if err != nil {
return nil, errors.Trace(err)
}

failpoint.Inject("errGetRegionByIDEmpty", func(val failpoint.Value) {
if val.(bool) {
meta = nil
}
})

if meta == nil {
return nil, errors.Errorf("region not found for regionID %q", regionID)
}
regions[i] = RegionMeta{
ID: regionID,
Leader: leader,
Expand Down
8 changes: 8 additions & 0 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
zaplog "github.com/pingcap/log"
"github.com/pingcap/parser/model"
Expand Down Expand Up @@ -237,6 +238,13 @@ func (ts *HTTPHandlerTestSuite) TestRegionsFromMeta(c *C) {
for _, meta := range metas {
c.Assert(meta.ID != 0, IsTrue)
}

// test no panic
c.Assert(failpoint.Enable("github.com/pingcap/tidb/server/errGetRegionByIDEmpty", `return(true)`), IsNil)
resp1, err := http.Get("http://127.0.0.1:10090/regions/meta")
c.Assert(err, IsNil)
defer resp1.Body.Close()
c.Assert(failpoint.Disable("github.com/pingcap/tidb/server/errGetRegionByIDEmpty"), IsNil)
}

func (ts *HTTPHandlerTestSuite) startServer(c *C) {
Expand Down

0 comments on commit 990e256

Please sign in to comment.