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

Code before:
	eg, ctx2 = errgroup.WithContext(ctx)
	for _, container := range containers {
		eg.Go(func() error {
			cntr, 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 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
	}
	close(c.containerStore.Added)
	// 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
	}{
		{
			cntrs:  sandboxes,
			base:   filepath.Join(c.config.RootDir, sandboxesDir),
			errMsg: "failed to cleanup orphaned sandbox directories",
		},
		{
			cntrs:  sandboxes,
			base:   filepath.Join(c.config.StateDir, sandboxesDir),
			errMsg: "failed to cleanup orphaned volatile sandbox directories",
		},

Review comment (suggestion):
Thanks for filing this pull request.
I found there are several similar changes related to this topic. Would you please just focus on one of them?
It's easy for reviewers to track history comments, like https://github.com/containerd/containerd/pull/10409#discussion_r1719852537.

Thanks

Code after:
	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
	}{
		{
			cntrs:  sandboxes,
			base:   filepath.Join(c.config.RootDir, sandboxesDir),
			errMsg: "failed to cleanup orphaned sandbox directories",
		},
		{
			cntrs:  sandboxes,