Skip to content

Commit

Permalink
Merge pull request #445 from istalker2/tapfdmanager-race
Browse files Browse the repository at this point in the history
Protect TapFDSource.fdMap from concurrent access
  • Loading branch information
pigmej authored Oct 2, 2017
2 parents bc8f646 + a583900 commit 8a70445
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/tapmanager/tapfdsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tapmanager
import (
"encoding/json"
"fmt"
"sync"
"time"

"github.com/containernetworking/cni/pkg/ns"
Expand Down Expand Up @@ -67,6 +68,8 @@ type podNetwork struct {
// TapFDSource sets up and tears down Virtlet VM network.
// It implements FDSource interface
type TapFDSource struct {
sync.Mutex

cniClient *cni.Client
fdMap map[string]*podNetwork
}
Expand Down Expand Up @@ -178,6 +181,8 @@ func (s *TapFDSource) GetFD(key string, data []byte) (int, []byte, error) {
return 0, nil, fmt.Errorf("error marshalling net config: %v", err)
}

s.Lock()
defer s.Unlock()
s.fdMap[key] = &podNetwork{
pnd: *pnd,
csn: csn,
Expand All @@ -189,6 +194,8 @@ func (s *TapFDSource) GetFD(key string, data []byte) (int, []byte, error) {

// Release implements Release method of FDSource interface
func (s *TapFDSource) Release(key string) error {
s.Lock()
defer s.Unlock()
pn, found := s.fdMap[key]
if !found {
return fmt.Errorf("bad fd key: %q", key)
Expand Down Expand Up @@ -228,6 +235,8 @@ func (s *TapFDSource) Release(key string) error {

// GetInfo implements GetInfo method of FDSource interface
func (s *TapFDSource) GetInfo(key string) ([]byte, error) {
s.Lock()
defer s.Unlock()
pn, found := s.fdMap[key]
if !found {
return nil, fmt.Errorf("bad fd key: %q", key)
Expand Down

0 comments on commit 8a70445

Please sign in to comment.