Skip to content

cmd/docker: skip the cobra mousetrap when arguments are passed - #7497

Open
azchohfi wants to merge 1 commit into
docker:masterfrom
azchohfi:o36-mousetrap
Open

azchohfi wants to merge 1 commit into
docker:masterfrom
azchohfi:o36-mousetrap

Conversation

@azchohfi

@azchohfi azchohfi commented Nov 7, 2026

Copy link
Copy Markdown

Summary

On Windows, cobra runs a "mousetrap" check before every command: if docker.exe was started from Explorer it prints "This is a command line tool" and exits. The check (mousetrap.StartedByExplorer) calls syscall.Getppid() and then looks the parent up by PID, and on Windows both walk a CreateToolhelp32Snapshot of every running process. With ~530 processes on my machine that is 31-38 ms per docker command, including docker --version.

A double-click never passes arguments, so this sets cobra.MousetrapHelpText = "" when there are any, the same as GitHub CLI does in internal/ghcmd/cmd.go. Started without arguments (double-click), the hint is shown as before. spf13/cobra#2545 proposes the same condition in cobra; this makes docker independent of when that lands.

Measured on Windows 11 (i9-10900K), master built with and without the change (Go 1.27.3), DOCKER_CONTEXT=default, 40 interleaved runs, median of paired differences with 95% confidence interval:

before after difference
docker --version 80.2 ms 42.2 ms -37.2 ms (-39.7 to -35.5)
docker context show 79.5 ms 42.9 ms -35.9 ms (-40.0 to -33.6)

The saving grows with the number of running processes. go test ./cmd/docker/ passes (Linux).

I used an AI assistant (GitHub Copilot) for the investigation and to help write this change; I reviewed and tested it.

Release notes (optional)

Speed up every command on Windows by skipping the "started from Explorer" check when arguments are given.

On Windows, cobra checks before every command whether docker.exe was started
from Explorer, to show a 'this is a command line tool' hint. The check calls
syscall.Getppid and then looks the parent up by PID, and both take a snapshot
of all running processes, which costs tens of milliseconds on a typical
machine. A double-click never passes arguments, so only do the check when
there are none. GitHub CLI does the same.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Alexandre Zollinger Chohfi <alzollin@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant