Resolve docker.NewResolver race condition

Signed-off-by: Jin Dong <djdongjin95@gmail.com>
(cherry picked from commit cdb153ec92)
Signed-off-by: Jin Dong <jin.dong@databricks.com>
This commit is contained in:
Jin Dong authored and Jin Dong committed 2023-07-10 12:30:11 -07:00
1 parent f21a7a34cc
commit fa2efc406f
2 files changed
+28 -1

No files matched your search

+4
View File
@@ -148,7 +148,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)
}
+24 -1
View File
@@ -46,7 +46,6 @@ func TestHTTPResolver(t *testing.T) {
base := s.URL[7:] // strip "http://"
return base, options, s.Close
}
runBasicTest(t, "testname", s)
}
@@ -54,6 +53,30 @@ 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