Repository: containerd/containerd
File: internal/cri/server/restart.go (Go)

Code before:
		}
		c.startSandboxExitMonitor(context.Background(), sb.ID, exitCh)
	}
	// Recover all containers.
	containers, err := c.client.Containers(ctx, filterLabel(crilabels.ContainerKindLabel, crilabels.ContainerKindContainer))
	if err != nil {
		return fmt.Errorf("failed to list containers: %w", err)
	}
	eg, ctx2 = errgroup.WithContext(ctx)
	for _, container := range containers {
		eg.Go(func() error {
			cntr, exitCh, pid, err := c.loadContainer(ctx2, container)
			if err != nil {
				log.G(ctx2).
					WithError(err).
					WithField("container", container.ID()).
					Error("Failed to load container")

				return nil
			}
			log.G(ctx2).Debugf("Loaded container %+v", cntr)
			if err := c.containerStore.Add(cntr); err != nil {
				return fmt.Errorf("failed to add container %q to store: %w", container.ID(), err)
			}
			if exitCh != nil {
				// Start exit monitor,this should run after container is added into containerStore.
				c.startContainerExitMonitor(context.Background(), cntr.ID, pid, exitCh)
			}
			if err := c.containerNameIndex.Reserve(cntr.Name, cntr.ID); err != nil {
				return fmt.Errorf("failed to reserve container name %q: %w", cntr.Name, err)
			}
			return nil
		})
	}
	if err := eg.Wait(); err != nil {
		return err
	}
	// Recover all images.
	if err := c.ImageService.CheckImages(ctx); err != nil {
		return fmt.Errorf("failed to check images: %w", err)
	}

	// It's possible that containerd containers are deleted unexpectedly. In that case,
	// we can't even get metadata, we should cleanup orphaned sandbox/container directories
	// with best effort.

	// Cleanup orphaned sandbox and container directories without corresponding containerd container.
	for _, cleanup := range []struct {
		cntrs  []containerd.Container
		base   string
		errMsg string

Review comment (suggestion):
suggestion:

Start the exit monitor. This should run after that container has been added to the container store.

Code after:
		}
		c.startSandboxExitMonitor(context.Background(), sb.ID, exitCh)
	}
	// Recover all containers.
	containers, err := c.client.Containers(ctx, filterLabel(crilabels.ContainerKindLabel, crilabels.ContainerKindContainer))
	if err != nil {
		return fmt.Errorf("failed to list containers: %w", err)
	}
	eg, ctx2 = errgroup.WithContext(ctx)
	for _, container := range containers {
		eg.Go(func() error {
			cntr, exitCh, pid, err := c.loadContainer(ctx2, container)
			if err != nil {
				log.G(ctx2).
					WithError(err).
					WithField("container", container.ID()).
					Error("Failed to load container")

				return nil
			}
			log.G(ctx2).Debugf("Loaded container %+v", cntr)
			if err := c.containerStore.Add(cntr); err != nil {
				return fmt.Errorf("failed to add container %q to store: %w", container.ID(), err)
			}
			if exitCh != nil {
				// Start the exit monitor. This should run after that container has been added to the container store.
				c.startContainerExitMonitor(context.Background(), cntr.ID, pid, exitCh)
			}
			if err := c.containerNameIndex.Reserve(cntr.Name, cntr.ID); err != nil {
				return fmt.Errorf("failed to reserve container name %q: %w", cntr.Name, err)
			}
			return nil
		})
	}
	if err := eg.Wait(); err != nil {
		return err
	}
	// Recover all images.
	if err := c.ImageService.CheckImages(ctx); err != nil {
		return fmt.Errorf("failed to check images: %w", err)
	}

	// It's possible that containerd containers are deleted unexpectedly. In that case,
	// we can't even get metadata, we should cleanup orphaned sandbox/container directories
	// with best effort.

	// Cleanup orphaned sandbox and container directories without corresponding containerd container.
	for _, cleanup := range []struct {
		cntrs  []containerd.Container
		base   string
		errMsg string