Skip to content

[bug] Missing defer cancel() in Execute for plugin mode #1764

@docker-agent

Description

@docker-agent

🟠 high - bug

File: cmd/root/root.go (line 104)

Code

if isCliPLugin() {
	plugin.Run(func(dockerCli command.Cli) *cobra.Command {
		originalPreRun := rootCmd.PersistentPreRunE
		rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
			if err := plugin.PersistentPreRunE(cmd, args); err != nil {
				return err
			}
			if originalPreRun != nil {
				if err := originalPreRun(cmd, args); err != nil {
					return processErr(ctx, err, stderr, rootCmd)
				}
			}
			return nil
		}
		rootCmd.SetContext(ctx)
		return rootCmd
	}, metadata.Metadata{
		SchemaVersion: "0.1.0",
		Vendor:        "Docker Inc.",
		Version:       version.Version,
	})
} else if err := rootCmd.ExecuteContext(ctx); err != nil {
	return processErr(ctx, err, stderr, rootCmd)
}

Problem

The main function in main.go calls cancel() when root.Execute returns an error or finishes successfully. However, when isCliPLugin() is true, plugin.Run is called, which then sets the context for rootCmd but doesn't handle the cancel() function explicitly after plugin.Run finishes. This could lead to a context not being cancelled, potentially causing goroutine leaks if any goroutines are listening to ctx.Done() and expect it to be closed.

Suggested Fix

The cancel() function should be called after plugin.Run completes, similar to how it's handled in the else if block for rootCmd.ExecuteContext(ctx).


Found by nightly codebase scan

Metadata

Metadata

Assignees

No one assigned

    Labels

    automatedIssues created by cagentkind/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions