Skip to content

Commit

Permalink
Resolve docker.NewResolver race condition
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Dong <djdongjin95@gmail.com>
(cherry picked from commit cdb153e)
Signed-off-by: Jin Dong <jin.dong@databricks.com>
  • Loading branch information
djdongjin committed Jul 10, 2023
1 parent f3e02c7 commit 31c466f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions remotes/docker/resolver.go
Expand Up @@ -145,7 +145,11 @@ func NewResolver(options ResolverOptions) remotes.Resolver {

if options.Headers == nil {
options.Headers = make(http.Header)
} else {
// make a copy of the headers to avoid race due to concurrent map write
options.Headers = options.Headers.Clone()
}

if _, ok := options.Headers["User-Agent"]; !ok {
options.Headers.Set("User-Agent", "containerd/"+version.Version)
}
Expand Down
25 changes: 24 additions & 1 deletion remotes/docker/resolver_test.go
Expand Up @@ -46,14 +46,37 @@ func TestHTTPResolver(t *testing.T) {
base := s.URL[7:] // strip "http://"
return base, options, s.Close
}

runBasicTest(t, "testname", s)
}

func TestHTTPSResolver(t *testing.T) {
runBasicTest(t, "testname", tlsServer)
}

func TestResolverOptionsRace(t *testing.T) {
header := http.Header{}
header.Set("X-Test", "test")

s := func(h http.Handler) (string, ResolverOptions, func()) {
s := httptest.NewServer(h)

options := ResolverOptions{
Headers: header,
}
base := s.URL[7:] // strip "http://"
return base, options, s.Close
}

for i := 0; i < 5; i++ {
t.Run(fmt.Sprintf("test ResolverOptions race %d", i), func(t *testing.T) {
// parallel sub tests so the race condition (if not handled) can be caught
// by race detector
t.Parallel()
runBasicTest(t, "testname", s)
})
}
}

func TestBasicResolver(t *testing.T) {
basicAuth := func(h http.Handler) (string, ResolverOptions, func()) {
// Wrap with basic auth
Expand Down

0 comments on commit 31c466f

Please sign in to comment.