Repository: moby/moby (language: go)

Issue:
data race during daemon startup (`libcontainerd/supervisor.(*remote).monitorDaemon()`)
### Description

```
==================
WARNING: DATA RACE
Read at 0x00c000d741e0 by goroutine 52:
  github.com/docker/docker/libcontainerd/supervisor.(*remote).monitorDaemon()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:332 +0xf3b
  github.com/docker/docker/libcontainerd/supervisor.Start.func1()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:99 +0x4f

Previous write at 0x00c000d741e0 by goroutine 54:
  github.com/docker/docker/libcontainerd/supervisor.(*remote).startContainerd.func1()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:198 +0x104

Goroutine 52 (running) created at:
  github.com/docker/docker/libcontainerd/supervisor.Start()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:99 +0x84c
  main.(*DaemonCli).initContainerd()
      /go/src/github.com/docker/docker/cmd/dockerd/daemon_unix.go:141 +0x3ec
  main.(*DaemonCli).start()
      /go/src/github.com/docker/docker/cmd/dockerd/daemon.go:184 +0xf74
  main.runDaemon()
      /go/src/github.com/docker/docker/cmd/dockerd/docker_unix.go:13 +0x12e
  main.newDaemonCommand.func1()
      /go/src/github.com/docker/docker/cmd/dockerd/docker.go:37 +0x12f
  github.com/spf13/cobra.(*Command).execute()
      /go/src/github.com/docker/docker/vendor/github.com/spf13/cobra/command.go:983 +0xf77
  github.com/spf13/cobra.(*Command).ExecuteC()
      /go/src/github.com/docker/docker/vendor/github.com/spf13/cobra/command.go:1115 +0x657
  github.com/spf13/cobra.(*Command).Execute()
      /go/src/github.com/docker/docker/vendor/github.com/spf13/cobra/command.go:1039 +0x254
  main.main()
      /go/src/github.com/docker/docker/cmd/dockerd/docker.go:106 +0x255

Goroutine 54 (running) created at:
  github.com/docker/docker/libcontainerd/supervisor.(*remote).startContainerd()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:182 +0x964
  github.com/docker/docker/libcontainerd/supervisor.(*remote).monitorDaemon()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:290 +0x3c4
  github.com/docker/docker/libcontainerd/supervisor.Start.func1()
      /go/src/github.com/docker/docker/libcontainerd/supervisor/remote_daemon.go:99 +0x4f
==================
```

### Reproduce

Build a binary with `BUILDFLAGS=-race` and run it.

### Expected behavior

The data race detector doesn't complain

### docker version

```bash
some dirty tree close to 8a81b9d35fb89251437bd046d2154f6c73b22f7c
```


### docker info

```bash
N/A
```


### Additional Info

_No response_


Discussion / hints:
- Possibly related to #44215

Or the race might have predated my touching of the code

Fix patch:
diff --git a/libcontainerd/supervisor/remote_daemon.go b/libcontainerd/supervisor/remote_daemon.go
index 7138dc00a87e8..10a8a4fda77a9 100644
--- a/libcontainerd/supervisor/remote_daemon.go
+++ b/libcontainerd/supervisor/remote_daemon.go
@@ -190,12 +190,13 @@ func (r *remote) startContainerd() error {
 		runtime.LockOSThread()
 		defer runtime.UnlockOSThread()
 		err := cmd.Start()
-		startedCh <- err
 		if err != nil {
+			startedCh <- err
 			return
 		}
-
 		r.daemonWaitCh = make(chan struct{})
+		startedCh <- nil
+
 		// Reap our child when needed
 		if err := cmd.Wait(); err != nil {
 			r.logger.WithError(err).Errorf("containerd did not exit successfully")


Test patch: