Merge pull request #432 from bergwolf/connection

kata_agent: fix connection race
This commit is contained in:
zhangwei_cs 2018-06-21 17:48:49 +08:00 committed by GitHub
commit 62d819c907
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"syscall" "syscall"
"time" "time"
@ -77,9 +78,13 @@ type KataAgentState struct {
} }
type kataAgent struct { type kataAgent struct {
shim shim shim shim
proxy proxy proxy proxy
client *kataclient.AgentClient
// lock protects the client pointer
sync.Mutex
client *kataclient.AgentClient
reqHandlers map[string]reqFunc reqHandlers map[string]reqFunc
state KataAgentState state KataAgentState
keepConn bool keepConn bool
@ -1089,6 +1094,14 @@ func (k *kataAgent) statsContainer(sandbox *Sandbox, c Container) (*ContainerSta
} }
func (k *kataAgent) connect() error { func (k *kataAgent) connect() error {
// lockless quick pass
if k.client != nil {
return nil
}
// This is for the first connection only, to prevent race
k.Lock()
defer k.Unlock()
if k.client != nil { if k.client != nil {
return nil return nil
} }
@ -1105,6 +1118,9 @@ func (k *kataAgent) connect() error {
} }
func (k *kataAgent) disconnect() error { func (k *kataAgent) disconnect() error {
k.Lock()
defer k.Unlock()
if k.client == nil { if k.client == nil {
return nil return nil
} }