提交 e22b268f 编写于 作者: D Dongsu Park

pkg: fix data race around HasASessionRequested

TestCreateSSHShell fails when running "go test --race", because of
concurrent accesses from multiple goroutines.

```
WARNING: DATA RACE
Read at 0x00c42025b730 by goroutine 42:
k8s.io/minikube/pkg/minikube/cluster.TestCreateSSHShell()
    k8s.io/minikube/pkg/minikube/cluster/cluster_test.go:523 +0x543
testing.tRunner()
    /usr/local/golang/src/testing/testing.go:657 +0x107

Previous write at 0x00c42025b730 by goroutine 49:
k8s.io/minikube/pkg/minikube/tests.(*SSHServer).Start.func1.1()
    k8s.io/minikube/pkg/minikube/tests/ssh_mock.go:95 +0x743
```

To fix that, convert HadASessionRequested to an atomic variable.
Callers should run helper functions, SetSessionRequested() and
IsSessionRequested() instead of direct access to the variable.
上级 f2478037
......@@ -520,7 +520,7 @@ func TestCreateSSHShell(t *testing.T) {
t.Fatalf("Error running ssh command: %s", err)
}
if !s.HadASessionRequested {
if !s.IsSessionRequested() {
t.Fatalf("Expected ssh session to be run")
}
}
......
......@@ -23,6 +23,7 @@ import (
"io"
"net"
"strconv"
"sync/atomic"
"github.com/golang/glog"
"github.com/pkg/errors"
......@@ -33,10 +34,11 @@ import (
type SSHServer struct {
Config *ssh.ServerConfig
// Commands stores the raw commands executed against the server.
Commands map[string]int
Connected bool
Transfers *bytes.Buffer
HadASessionRequested bool
Commands map[string]int
Connected bool
Transfers *bytes.Buffer
// Only access this with atomic ops
hadASessionRequested int32
// CommandsToOutput can be used to mock what the SSHServer returns for a given command
CommandToOutput map[string]string
}
......@@ -59,6 +61,7 @@ func NewSSHServer() (*SSHServer, error) {
return nil, errors.Wrap(err, "Error creating signer from key")
}
s.Config.AddHostKey(signer)
s.SetSessionRequested(false)
return s, nil
}
......@@ -92,7 +95,7 @@ func (s *SSHServer) Start() (int, error) {
// Service the incoming Channel channel.
for newChannel := range chans {
if newChannel.ChannelType() == "session" {
s.HadASessionRequested = true
s.SetSessionRequested(true)
}
channel, requests, err := newChannel.Accept()
s.Connected = true
......@@ -136,3 +139,15 @@ func (s *SSHServer) Start() (int, error) {
}
return port, nil
}
func (s *SSHServer) SetSessionRequested(b bool) {
var i int32
if b {
i = 1
}
atomic.StoreInt32(&s.hadASessionRequested, i)
}
func (s *SSHServer) IsSessionRequested() bool {
return atomic.LoadInt32(&s.hadASessionRequested) != 0
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册