Repository: docker/compose
File: pkg/compose/up.go (Go)

Code before:
				eg.Go(func() error {
					err := progress.RunWithLog(context.WithoutCancel(globalCtx), func(c context.Context) error {
						return s.stop(c, project.Name, api.StopOptions{
							Services: options.Create.Services,
							Project:  project,
						}, printer.HandleEvent)
					}, s.stdinfo(), logConsumer)
					appendErr(err)
					return nil
				})
			}
		})
	}

	if options.Start.ExitCodeFrom != "" {
		once := true
		// capture exit code from first container to exit with selected service
		monitor.withListener(func(event api.ContainerEvent) {
			if once && event.Type == api.ContainerEventExited && event.Service == options.Start.ExitCodeFrom {
				exitCode = event.ExitCode
				once = false
			}
		})
	}

	// use an independent context tied to the errgroup for background attach operations
	// the primary context is still used for other operations
	// this means that once any attach operation fails, all other attaches are cancelled,
	// but an attach failing won't interfere with the rest of the start
	//
	// FIXME(thaJeztah): this context was never cancelled, so didn't add anything? See https://github.com/docker/compose/commit/2c12ad19db5003cdf4cc0de8711970dbb6914b17
	// _, attachCtx := errgroup.WithContext(ctx)
	// containers, err := s.attach(attachCtx, project, printer.HandleEvent, options.Start.AttachTo)
	containers, err := s.attach(globalCtx, project, printer.HandleEvent, options.Start.AttachTo)
	if err != nil {
		cancel()
		_ = eg.Wait()
		return err
	}
	attached := make([]string, len(containers))
	for i, ctr := range containers {
		attached[i] = ctr.ID
	}

	monitor.withListener(func(event api.ContainerEvent) {
		if event.Type != api.ContainerEventStarted {
			return
		}
		if slices.Contains(attached, event.ID) {
			return
		}

Review comment (suggestion):
This part seemed like it was not yet functional, or at least, the derived context was never cancelled, and the new error-group was discarded, so effectively it was just a copy of the context that was never cancelled (added in https://github.com/docker/compose/commit/2c12ad19db5003cdf4cc0de8711970dbb6914b17)

I didn't remove the code yet, because it probably needs to be looked at, and either removed, or perhaps the "attach" ones being in their own errgroup? I wasn't sure, so could use input here.

Code after:
				eg.Go(func() error {
					err := progress.RunWithLog(context.WithoutCancel(globalCtx), func(c context.Context) error {
						return s.stop(c, project.Name, api.StopOptions{
							Services: options.Create.Services,
							Project:  project,
						}, printer.HandleEvent)
					}, s.stdinfo(), logConsumer)
					appendErr(err)
					return nil
				})
			}
		})
	}

	if options.Start.ExitCodeFrom != "" {
		once := true
		// capture exit code from first container to exit with selected service
		monitor.withListener(func(event api.ContainerEvent) {
			if once && event.Type == api.ContainerEventExited && event.Service == options.Start.ExitCodeFrom {
				exitCode = event.ExitCode
				once = false
			}
		})
	}

	containers, err := s.attach(globalCtx, project, printer.HandleEvent, options.Start.AttachTo)
	if err != nil {
		cancel()
		_ = eg.Wait()
		return err
	}
	attached := make([]string, len(containers))
	for i, ctr := range containers {
		attached[i] = ctr.ID
	}

	monitor.withListener(func(event api.ContainerEvent) {
		if event.Type != api.ContainerEventStarted {
			return
		}
		if slices.Contains(attached, event.ID) {
			return
		}
		eg.Go(func() error {
			ctr, err := s.apiClient().ContainerInspect(globalCtx, event.ID)
			if err != nil {
				appendErr(err)
				return nil
			}

			err = s.doLogContainer(globalCtx, options.Start.Attach, event.Source, ctr, api.LogOptions{