Skip to content

Commit

Permalink
kata_agent: fix connection race
Browse files Browse the repository at this point in the history
If we send multiple grpcs at once before a client is created, we end up
creating multiple connections to the agent and that breaks when using builtin
proxy since only one connection is allowed.

Fixes: kata-containers#431

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Jun 21, 2018
1 parent fca7eb8 commit ee33245
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -77,9 +78,13 @@ type KataAgentState struct {
}

type kataAgent struct {
shim shim
proxy proxy
client *kataclient.AgentClient
shim shim
proxy proxy

// lock protects the client pointer
sync.Mutex
client *kataclient.AgentClient

reqHandlers map[string]reqFunc
state KataAgentState
keepConn bool
Expand Down Expand Up @@ -1089,6 +1094,14 @@ func (k *kataAgent) statsContainer(sandbox *Sandbox, c Container) (*ContainerSta
}

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 {
return nil
}
Expand All @@ -1105,6 +1118,9 @@ func (k *kataAgent) connect() error {
}

func (k *kataAgent) disconnect() error {
k.Lock()
defer k.Unlock()

if k.client == nil {
return nil
}
Expand Down

0 comments on commit ee33245

Please sign in to comment.