cli/command: Avoid panics after client initialization errors - #7280
Merged
Conversation
When client.New rejects an invalid Docker host or other client option, it returns a nil concrete client with an error. Returning that pointer directly as client.APIClient stores a typed-nil interface, so lazy version checks dereference it. Return an error instead. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Member
|
Hm.. nice one indeed. We should probably do some more fixes in this area after this. Initially wasn't able to get it to panic, but it depends on what command you're running; this one reproduces indeed; DOCKER_API_VERSION=1 DOCKER_HOST=:2375 docker version
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x58 pc=0x100a31280]
goroutine 1 [running]:
github.com/docker/cli/vendor/github.com/moby/moby/client.(*Client).ClientVersion(0x17cc1292c000?)
/go/src/github.com/docker/cli/vendor/github.com/moby/moby/client/client.go:328
github.com/docker/cli/cli/command.(*DockerCli).CurrentVersion(0x17cc1292c000)
/go/src/github.com/docker/cli/cli/command/cli.go:93 +0x34
github.com/docker/cli/cli/command/system.newClientVersion({0x101101511, 0x7}, {0x101f6c3f0, 0x17cc1292c000})
/go/src/github.com/docker/cli/cli/command/system/version.go:126 +0x168
github.com/docker/cli/cli/command/system.runVersion({0x101f5da80, 0x17cc12918000}, {0x101f6c3f0, 0x17cc1292c000}, 0x101db8f40?)
/go/src/github.com/docker/cli/cli/command/system/version.go:220 +0x5c
github.com/docker/cli/cli/command/system.newVersionCommand.func1(0x0?, {0x0?, 0x0?, 0xf059149abeac7925?})
/go/src/github.com/docker/cli/cli/command/system/version.go:183 +0x2c
main.runDocker.(*DockerCli).InstrumentCobraCommands.func5.2(0x17cc12d65508, {0x10205d6e0, 0x0, 0x0})
/go/src/github.com/docker/cli/cli/command/telemetry_utils.go:58 +0x60
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).execute(0x17cc12d65508, {0x17cc12af70b0, 0x0, 0x0})
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1015 +0x814
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteC(0x17cc12936008)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1148 +0x350
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).Execute(...)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1071
github.com/docker/cli/vendor/github.com/spf13/cobra.(*Command).ExecuteContext(...)
/go/src/github.com/docker/cli/vendor/github.com/spf13/cobra/command.go:1064
main.runDocker({0x101f5da80, 0x17cc12918000}, 0x17cc1292c000)
/go/src/github.com/docker/cli/cmd/docker/docker.go:544 +0x76c
main.dockerMain({0x101f5d730?, 0x10205d6e0?})
/go/src/github.com/docker/cli/cmd/docker/docker.go:93 +0x13c
main.main()
/go/src/github.com/docker/cli/cmd/docker/docker.go:43 +0x2c |
thaJeztah
reviewed
Sep 3, 2026
| return newAPIClientFromEndpoint(endpoint, configFile, client.WithUserAgent(UserAgent())) | ||
| } | ||
|
|
||
| func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigFile, extraOpts ...client.Opt) (client.APIClient, error) { |
Member
There was a problem hiding this comment.
This one probably also could've keep a concrete type as return.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
- What I did
When client.New rejects an invalid Docker host or other client option, it returns a nil concrete client with an error.
Returning that pointer directly as client.APIClient stores a typed-nil interface, so lazy version checks dereference it.
Return an error instead.
- How I did it
- How to verify it
- Human readable description for the release notes
- A picture of a cute animal (not mandatory but encouraged)