@@ -59,15 +59,18 @@ type Sandbox struct {
5959 nsOpts * types.NamespaceOption
6060 dnsConfig * types.DNSConfig
6161 stopMutex sync.RWMutex
62- created bool
63- stopped bool
64- networkStopped bool
65- privileged bool
66- hostNetwork bool
67- usernsMode string
68- containerEnvPath string
69- podLinuxOverhead * types.LinuxContainerResources
70- podLinuxResources * types.LinuxContainerResources
62+ // stateMutex protects the use of created, stopped and networkStopped bools
63+ // which are all fields that can change at runtime
64+ stateMutex sync.RWMutex
65+ created bool
66+ stopped bool
67+ networkStopped bool
68+ privileged bool
69+ hostNetwork bool
70+ usernsMode string
71+ containerEnvPath string
72+ podLinuxOverhead * types.LinuxContainerResources
73+ podLinuxResources * types.LinuxContainerResources
7174}
7275
7376// DefaultShmSize is the default shm size.
@@ -326,6 +329,10 @@ func (s *Sandbox) RemoveInfraContainer() {
326329func (s * Sandbox ) SetStopped (ctx context.Context , createFile bool ) {
327330 ctx , span := log .StartSpan (ctx )
328331 defer span .End ()
332+
333+ s .stateMutex .Lock ()
334+ defer s .stateMutex .Unlock ()
335+
329336 if s .stopped {
330337 return
331338 }
@@ -340,16 +347,24 @@ func (s *Sandbox) SetStopped(ctx context.Context, createFile bool) {
340347// Stopped returns whether the sandbox state has been
341348// set to stopped.
342349func (s * Sandbox ) Stopped () bool {
350+ s .stateMutex .RLock ()
351+ defer s .stateMutex .RUnlock ()
352+
343353 return s .stopped
344354}
345355
346356// SetCreated sets the created status of sandbox to true.
347357func (s * Sandbox ) SetCreated () {
358+ s .stateMutex .Lock ()
359+ defer s .stateMutex .Unlock ()
348360 s .created = true
349361}
350362
351363// NetworkStopped returns whether the network has been stopped.
352364func (s * Sandbox ) NetworkStopped () bool {
365+ s .stateMutex .RLock ()
366+ defer s .stateMutex .RUnlock ()
367+
353368 return s .networkStopped
354369}
355370
@@ -363,6 +378,10 @@ func (s *Sandbox) NetworkStopped() bool {
363378func (s * Sandbox ) SetNetworkStopped (ctx context.Context , createFile bool ) error {
364379 ctx , span := log .StartSpan (ctx )
365380 defer span .End ()
381+
382+ s .stateMutex .Lock ()
383+ defer s .stateMutex .Unlock ()
384+
366385 if s .networkStopped {
367386 return nil
368387 }
@@ -394,6 +413,7 @@ func (s *Sandbox) SetContainerEnvFile(ctx context.Context) error {
394413 return nil
395414}
396415
416+ // This function assumes the state lock has been taken for this sandbox.
397417func (s * Sandbox ) createFileInInfraDir (ctx context.Context , filename string ) error {
398418 // If the sandbox is not yet created,
399419 // this function is being called when
@@ -419,6 +439,9 @@ func (s *Sandbox) createFileInInfraDir(ctx context.Context, filename string) err
419439}
420440
421441func (s * Sandbox ) RestoreStopped () {
442+ s .stateMutex .Lock ()
443+ defer s .stateMutex .Unlock ()
444+
422445 if s .fileExistsInInfraDir (sbStoppedFilename ) {
423446 s .stopped = true
424447 }
@@ -441,11 +464,14 @@ func (s *Sandbox) fileExistsInInfraDir(filename string) bool {
441464
442465// Created returns the created status of sandbox.
443466func (s * Sandbox ) Created () bool {
467+ s .stateMutex .RLock ()
468+ defer s .stateMutex .RUnlock ()
469+
444470 return s .created
445471}
446472
447473func (s * Sandbox ) State () types.PodSandboxState {
448- if s .Ready (false ) {
474+ if s .Ready () {
449475 return types .PodSandboxState_SANDBOX_READY
450476 }
451477 return types .PodSandboxState_SANDBOX_NOTREADY
@@ -456,22 +482,9 @@ func (s *Sandbox) State() types.PodSandboxState {
456482// `takeLock` should be set if we need to take the lock to get the infra container's state.
457483// If there is no infra container, it is never considered ready.
458484// If the infra container is spoofed, the pod is considered ready when it has been created, but not stopped.
459- func (s * Sandbox ) Ready (takeLock bool ) bool {
460- podInfraContainer := s .InfraContainer ()
461- if podInfraContainer == nil {
462- return false
463- }
464- if podInfraContainer .Spoofed () {
465- return s .created && ! s .stopped
466- }
467- // Assume the sandbox is ready, unless it has an infra container that
468- // isn't running
469- var cState * oci.ContainerState
470- if takeLock {
471- cState = podInfraContainer .State ()
472- } else {
473- cState = podInfraContainer .StateNoLock ()
474- }
485+ func (s * Sandbox ) Ready () bool {
486+ s .stateMutex .RLock ()
487+ defer s .stateMutex .RUnlock ()
475488
476- return cState . Status == oci . ContainerStateRunning
489+ return s . created && ! s . stopped
477490}
0 commit comments