Skip to content

client: fix datarace when accessing cli.Version field#50448

Merged
vvoland merged 1 commit intomoby:masterfrom
alessio-perugini:fix-data-race-on-list
Jul 22, 2025
Merged

client: fix datarace when accessing cli.Version field#50448
vvoland merged 1 commit intomoby:masterfrom
alessio-perugini:fix-data-race-on-list

Conversation

@alessio-perugini
Copy link
Contributor

@alessio-perugini alessio-perugini commented Jul 18, 2025

Originally I've found this datarace on a project I'm working at. I'm not able to consistently reproduce this. But by looking at the codebase I took a chance to fix other 2 possible function that might produce such data race.

Original stack trace produced when running go test -race on GH CI:

WARNING: DATA RACE
Write at 0x00c0005dc688 by goroutine 43:
  github.com/docker/docker/client.(*Client).negotiateAPIVersionPing()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/client.go:389 +0x12f
  github.com/docker/docker/client.(*Client).checkVersion()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/client.go:298 +0x249
  github.com/docker/docker/client.(*Client).getAPIPath()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/client.go:307 +0x76
  github.com/docker/docker/client.(*Client).sendRequest()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/request.go:111 +0x9b
  github.com/docker/docker/client.(*Client).get()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/request.go:28 +0x736
  github.com/docker/docker/client.(*Client).ContainerList()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/container_list.go:47 +0x6f0

Previous read at 0x00c0005dc688 by goroutine 42:
  github.com/docker/docker/client.(*Client).ContainerList()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/container_list.go:39 +0x5ef
Go SDK: Fix data race in `ContainerExecStart`, `ContainerList`, and `Events`.

Originally I've found this datarace on a project I'm working at. I'm not
able to consistently reproduce this. But by looking at the codebase I
took a chance to fix other 2 possible function that might produce such
data race.

Original stack trace produced when running `go test -race` on GH CI:

```
WARNING: DATA RACE
Write at 0x00c0005dc688 by goroutine 43:
  github.com/docker/docker/client.(*Client).negotiateAPIVersionPing()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/client.go:389 +0x12f
  github.com/docker/docker/client.(*Client).checkVersion()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/client.go:298 +0x249
  github.com/docker/docker/client.(*Client).getAPIPath()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/client.go:307 +0x76
  github.com/docker/docker/client.(*Client).sendRequest()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/request.go:111 +0x9b
  github.com/docker/docker/client.(*Client).get()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/request.go:28 +0x736
  github.com/docker/docker/client.(*Client).ContainerList()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/container_list.go:47 +0x6f0

Previous read at 0x00c0005dc688 by goroutine 42:
  github.com/docker/docker/client.(*Client).ContainerList()
      /home/runner/go/pkg/mod/github.com/docker/docker@v28.2.2+incompatible/client/container_list.go:39 +0x5ef
```

Co-authored-by: Luca Rinaldi <lucarin@protonmail.com>
Signed-off-by: Alessio Perugini <alessio@perugini.xyz>
Copy link
Contributor

@vvoland vvoland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's correct solution - perhaps this should be handled by cli.ClientVersion() and then we should change all cli.version usages to use the accessor?

Overall, I'm concerned about having to explicitly call the checkVersion.

@alessio-perugini
Copy link
Contributor Author

alessio-perugini commented Jul 21, 2025

@vvoland I 💯 agree. Before coming up with this PR, I looked at the codebase, and it seems we have plenty of other places where we're doing the exact same thing. That's how I came up with this PR. But it is definitely error-prone to handle this case like that.

For reference, below I'm linking the same usage in a different part of the repo (might be useful for a future refactoring):

@vvoland vvoland added kind/bugfix PR's that fix bugs area/go-sdk impact/go-sdk Noteworthy (compatibility changes) in the Go SDK labels Jul 22, 2025
@vvoland vvoland added this to the 29.0.0 milestone Jul 22, 2025
@vvoland
Copy link
Contributor

vvoland commented Jul 22, 2025

Oh, good point! I didn't notice we already do this 🙈

We should take a look into it as a follow up.

Copy link
Contributor

@vvoland vvoland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@vvoland vvoland merged commit c9a83e3 into moby:master Jul 22, 2025
187 of 192 checks passed
@thaJeztah
Copy link
Member

This one made changes to the client module, so needed a re-vendor (as we vendor the client module)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/go-sdk impact/changelog impact/go-sdk Noteworthy (compatibility changes) in the Go SDK kind/bugfix PR's that fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants