diff --git a/internal/lib/sandbox/namespaces.go b/internal/lib/sandbox/namespaces.go index 968f9f36995..da555c6c8de 100644 --- a/internal/lib/sandbox/namespaces.go +++ b/internal/lib/sandbox/namespaces.go @@ -305,7 +305,14 @@ func infraPid(infra *oci.Container) int { if infra != nil { var err error pid, err = infra.Pid() - if err != nil { + // There are some cases where ErrNotInitialized is expected. + // For instance, when we're creating a pod sandbox while managing namespace lifecycle, + // we create the network stack before we create the infra container. + // Since we're pinning namespaces, we already have the namespace we need. + // Later users of this pid will either find we have a valid pinned namespace (which we will in this case), + // or find we have an invalid /proc entry (a negative pid). + // Thus, we don't need to error here if the pid is not initialized + if err != nil && err != oci.ErrNotInitialized { logrus.Errorf("pid for infra container %s not found: %v", infra.ID(), err) } } diff --git a/internal/oci/container.go b/internal/oci/container.go index be82aec704f..745f895021e 100644 --- a/internal/oci/container.go +++ b/internal/oci/container.go @@ -28,6 +28,7 @@ var ( defaultStopSignal = strconv.Itoa(defaultStopSignalInt) ErrContainerStopped = errors.New("container is already stopped") ErrNotFound = errors.New("container process not found") + ErrNotInitialized = errors.New("container PID not initialized") ) // Container represents a runtime container. @@ -413,10 +414,10 @@ func (c *Container) Pid() (int, error) { // and it is the same process that was originally started by the runtime. func (c *Container) pid() (int, error) { if c.state == nil { - return 0, errors.New("state not initialized") + return 0, ErrNotInitialized } if c.state.InitPid <= 0 { - return 0, errors.New("PID not initialized") + return 0, ErrNotInitialized } // container has stopped (as pid is initialized but the runc state has overwritten it)