mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-28 16:27:50 +00:00
Merge pull request #432 from bergwolf/connection
kata_agent: fix connection race
This commit is contained in:
commit
62d819c907
@ -13,6 +13,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -79,7 +80,11 @@ type KataAgentState struct {
|
|||||||
type kataAgent struct {
|
type kataAgent struct {
|
||||||
shim shim
|
shim shim
|
||||||
proxy proxy
|
proxy proxy
|
||||||
|
|
||||||
|
// lock protects the client pointer
|
||||||
|
sync.Mutex
|
||||||
client *kataclient.AgentClient
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user