Skip to content

Commit 699aefa

Browse files
committed
lib,oci: drop stateLock when possible
Should fix a possible deadlock in, at least, ListPodSandbox. There seems to be no reason to hold stateLock when doing operations on the memory_store for containers and sandboxes. Signed-off-by: Antonio Murdaca <[email protected]>
1 parent bdaba5c commit 699aefa

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

libkpod/container_server.go

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -619,46 +619,33 @@ type containerServerState struct {
619619

620620
// AddContainer adds a container to the container state store
621621
func (c *ContainerServer) AddContainer(ctr *oci.Container) {
622-
c.stateLock.Lock()
623-
defer c.stateLock.Unlock()
624622
sandbox := c.state.sandboxes.Get(ctr.Sandbox())
625623
sandbox.AddContainer(ctr)
626624
c.state.containers.Add(ctr.ID(), ctr)
627625
}
628626

629627
// AddInfraContainer adds a container to the container state store
630628
func (c *ContainerServer) AddInfraContainer(ctr *oci.Container) {
631-
c.stateLock.Lock()
632-
defer c.stateLock.Unlock()
633629
c.state.infraContainers.Add(ctr.ID(), ctr)
634630
}
635631

636632
// GetContainer returns a container by its ID
637633
func (c *ContainerServer) GetContainer(id string) *oci.Container {
638-
c.stateLock.Lock()
639-
defer c.stateLock.Unlock()
640634
return c.state.containers.Get(id)
641635
}
642636

643637
// GetInfraContainer returns a container by its ID
644638
func (c *ContainerServer) GetInfraContainer(id string) *oci.Container {
645-
c.stateLock.Lock()
646-
defer c.stateLock.Unlock()
647639
return c.state.infraContainers.Get(id)
648640
}
649641

650642
// HasContainer checks if a container exists in the state
651643
func (c *ContainerServer) HasContainer(id string) bool {
652-
c.stateLock.Lock()
653-
defer c.stateLock.Unlock()
654-
ctr := c.state.containers.Get(id)
655-
return ctr != nil
644+
return c.state.containers.Get(id) != nil
656645
}
657646

658647
// RemoveContainer removes a container from the container state store
659648
func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
660-
c.stateLock.Lock()
661-
defer c.stateLock.Unlock()
662649
sbID := ctr.Sandbox()
663650
sb := c.state.sandboxes.Get(sbID)
664651
sb.RemoveContainer(ctr)
@@ -667,15 +654,11 @@ func (c *ContainerServer) RemoveContainer(ctr *oci.Container) {
667654

668655
// RemoveInfraContainer removes a container from the container state store
669656
func (c *ContainerServer) RemoveInfraContainer(ctr *oci.Container) {
670-
c.stateLock.Lock()
671-
defer c.stateLock.Unlock()
672657
c.state.infraContainers.Delete(ctr.ID())
673658
}
674659

675660
// listContainers returns a list of all containers stored by the server state
676661
func (c *ContainerServer) listContainers() []*oci.Container {
677-
c.stateLock.Lock()
678-
defer c.stateLock.Unlock()
679662
return c.state.containers.List()
680663
}
681664

@@ -699,43 +682,36 @@ func (c *ContainerServer) ListContainers(filters ...func(*oci.Container) bool) (
699682

700683
// AddSandbox adds a sandbox to the sandbox state store
701684
func (c *ContainerServer) AddSandbox(sb *sandbox.Sandbox) {
702-
c.stateLock.Lock()
703-
defer c.stateLock.Unlock()
704685
c.state.sandboxes.Add(sb.ID(), sb)
686+
687+
c.stateLock.Lock()
705688
c.state.processLevels[selinux.NewContext(sb.ProcessLabel())["level"]]++
689+
c.stateLock.Unlock()
706690
}
707691

708692
// GetSandbox returns a sandbox by its ID
709693
func (c *ContainerServer) GetSandbox(id string) *sandbox.Sandbox {
710-
c.stateLock.Lock()
711-
defer c.stateLock.Unlock()
712694
return c.state.sandboxes.Get(id)
713695
}
714696

715697
// GetSandboxContainer returns a sandbox's infra container
716698
func (c *ContainerServer) GetSandboxContainer(id string) *oci.Container {
717-
c.stateLock.Lock()
718-
defer c.stateLock.Unlock()
719699
sb := c.state.sandboxes.Get(id)
720700
return sb.InfraContainer()
721701
}
722702

723703
// HasSandbox checks if a sandbox exists in the state
724704
func (c *ContainerServer) HasSandbox(id string) bool {
725-
c.stateLock.Lock()
726-
defer c.stateLock.Unlock()
727-
sb := c.state.sandboxes.Get(id)
728-
return sb != nil
705+
return c.state.sandboxes.Get(id) != nil
729706
}
730707

731708
// RemoveSandbox removes a sandbox from the state store
732709
func (c *ContainerServer) RemoveSandbox(id string) {
733-
c.stateLock.Lock()
734-
defer c.stateLock.Unlock()
735710
sb := c.state.sandboxes.Get(id)
736711
processLabel := sb.ProcessLabel()
737-
c.state.sandboxes.Delete(id)
738712
level := selinux.NewContext(processLabel)["level"]
713+
714+
c.stateLock.Lock()
739715
pl, ok := c.state.processLevels[level]
740716
if ok {
741717
c.state.processLevels[level] = pl - 1
@@ -744,12 +720,13 @@ func (c *ContainerServer) RemoveSandbox(id string) {
744720
delete(c.state.processLevels, level)
745721
}
746722
}
723+
c.stateLock.Unlock()
724+
725+
c.state.sandboxes.Delete(id)
747726
}
748727

749728
// ListSandboxes lists all sandboxes in the state store
750729
func (c *ContainerServer) ListSandboxes() []*sandbox.Sandbox {
751-
c.stateLock.Lock()
752-
defer c.stateLock.Unlock()
753730
return c.state.sandboxes.List()
754731
}
755732

libkpod/sandbox/memory_store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func (c *memoryStore) Add(id string, cont *Sandbox) {
2525

2626
// Get returns a sandbox from the store by id.
2727
func (c *memoryStore) Get(id string) *Sandbox {
28+
var res *Sandbox
2829
c.RLock()
29-
res := c.s[id]
30+
res = c.s[id]
3031
c.RUnlock()
3132
return res
3233
}

oci/memory_store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func (c *memoryStore) Add(id string, cont *Container) {
2525

2626
// Get returns a container from the store by id.
2727
func (c *memoryStore) Get(id string) *Container {
28+
var res *Container
2829
c.RLock()
29-
res := c.s[id]
30+
res = c.s[id]
3031
c.RUnlock()
3132
return res
3233
}

0 commit comments

Comments
 (0)