Skip to content

Commit

Permalink
server: fix panic caused by GetLeader (#1766)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored and sre-bot committed Sep 25, 2019
1 parent 3017268 commit e2dbdf6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -157,6 +157,7 @@ github.com/urfave/negroni v0.3.0 h1:PaXOb61mWeZJxc1Ji2xJjpVg9QfPo0rrB+lHyBxGNSU=
github.com/urfave/negroni v0.3.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yookoala/realpath v1.0.0 h1:7OA9pj4FZd+oZDsyvXWQvjn5oBdcHRTV44PpdMSuImQ=
github.com/yookoala/realpath v1.0.0/go.mod h1:gJJMA9wuX7AcqLy1+ffPatSCySA1FQ2S8Ya9AIoYBpE=
go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down
81 changes: 81 additions & 0 deletions server/api/version_test.go
@@ -0,0 +1,81 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/pd/pkg/testutil"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/server/config"
)

var _ = Suite(&testVersionSuite{})

type testVersionSuite struct{}

func (s *testVersionSuite) TestGetVersion(c *C) {
fname := filepath.Join(os.TempDir(), "stdout")
old := os.Stdout
temp, _ := os.Create(fname)
os.Stdout = temp

cfg := server.NewTestSingleConfig(c)
reqCh := make(chan struct{})
go func() {
<-reqCh
time.Sleep(200 * time.Millisecond)
addr := cfg.ClientUrls + apiPrefix + "/api/v1/version"
resp, err := dialClient.Get(addr)
c.Assert(err, IsNil)
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
}()

ctx, cancel := context.WithCancel(context.Background())
ch := make(chan *server.Server)
go func(cfg *config.Config) {
s, err := server.CreateServer(cfg, NewHandler)
c.Assert(err, IsNil)
c.Assert(failpoint.Enable("github.com/pingcap/pd/server/memberNil", `return(true)`), IsNil)
reqCh <- struct{}{}
err = s.Run(ctx)
c.Assert(err, IsNil)
ch <- s
}(cfg)

svr := <-ch
close(ch)
out, _ := ioutil.ReadFile(fname)
c.Assert(strings.Contains(string(out), "PANIC"), IsFalse)

// clean up
func() {
temp.Close()
os.Stdout = old
os.Remove(fname)
svr.Close()
cancel()
testutil.CleanServer(cfg)
}()
c.Assert(failpoint.Disable("github.com/pingcap/pd/server/memberNil"), IsNil)
}
1 change: 1 addition & 0 deletions server/cluster_test.go
Expand Up @@ -509,6 +509,7 @@ func (s *testClusterSuite) TestStoreVersionChange(c *C) {
v, err := semver.NewVersion("1.0.0")
c.Assert(err, IsNil)
c.Assert(s.svr.GetClusterVersion(), Equals, *v)
c.Assert(failpoint.Disable("github.com/pingcap/pd/server/versionChangeConcurrency"), IsNil)
}

func (s *testClusterSuite) TestConcurrentHandleRegion(c *C) {
Expand Down
5 changes: 5 additions & 0 deletions server/server.go 100644 → 100755
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/coreos/go-semver/semver"
"github.com/golang/protobuf/proto"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
Expand Down Expand Up @@ -111,6 +112,7 @@ func CreateServer(cfg *config.Config, apiRegister func(*Server) http.Handler) (*
s := &Server{
cfg: cfg,
scheduleOpt: config.NewScheduleOption(cfg),
member: &member.Member{},
}
s.handler = newHandler(s)

Expand Down Expand Up @@ -198,6 +200,9 @@ func (s *Server) startEtcd(ctx context.Context) error {
}
}
s.client = client
failpoint.Inject("memberNil", func() {
time.Sleep(500 * time.Millisecond)
})
s.member = member.NewMember(etcd, client, etcdServerID)
return nil
}
Expand Down

0 comments on commit e2dbdf6

Please sign in to comment.