Skip to content

Commit ea0b54c

Browse files
committed
tarfs: fix a data race condition
WARNING: DATA RACE Write at 0x00c000178428 by goroutine 27: github.com/containerd/nydus-snapshotter/pkg/remote/remotes/docker.(*httpReadSeeker).Close() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/remote/remotes/docker/httpreadseeker.go:87 +0x57 github.com/containerd/nydus-snapshotter/pkg/tarfs.(*Manager).blobProcess.func2.1() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs.go:339 +0x48 runtime.deferreturn() /opt/hostedtoolcache/go/1.20.1/x64/src/runtime/panic.go:476 +0x32 github.com/containerd/nydus-snapshotter/pkg/tarfs.(*Manager).blobProcess.func3() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs.go:394 +0x71 Previous read at 0x00c000178428 by goroutine 40: github.com/containerd/nydus-snapshotter/pkg/remote/remotes/docker.(*httpReadSeeker).Read() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/remote/remotes/docker/httpreadseeker.go:48 +0x68 bufio.(*Reader).Read() /opt/hostedtoolcache/go/1.20.1/x64/src/bufio/bufio.go:223 +0x2c3 github.com/containerd/containerd/archive/compression.(*bufferedReader).Read() /home/runner/go/pkg/mod/github.com/containerd/[email protected]/archive/compression/compression.go:113 +0xa4 io.copyBuffer() /opt/hostedtoolcache/go/1.20.1/x64/src/io/io.go:427 +0x28d io.Copy() /opt/hostedtoolcache/go/1.20.1/x64/src/io/io.go:386 +0x88 os.genericReadFrom() /opt/hostedtoolcache/go/1.20.1/x64/src/os/file.go:161 +0x34 os.(*File).ReadFrom() /opt/hostedtoolcache/go/1.20.1/x64/src/os/file.go:155 +0x324 io.copyBuffer() /opt/hostedtoolcache/go/1.20.1/x64/src/io/io.go:413 +0x1c5 io.Copy() /opt/hostedtoolcache/go/1.20.1/x64/src/io/io.go:386 +0x84 os/exec.(*Cmd).childStdin.func1() /opt/hostedtoolcache/go/1.20.1/x64/src/os/exec/exec.go:511 +0x45 os/exec.(*Cmd).Start.func2() /opt/hostedtoolcache/go/1.20.1/x64/src/os/exec/exec.go:717 +0x42 os/exec.(*Cmd).Start.func3() /opt/hostedtoolcache/go/1.20.1/x64/src/os/exec/exec.go:729 +0x47 Goroutine 27 (running) created at: github.com/containerd/nydus-snapshotter/pkg/tarfs.(*Manager).blobProcess() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs.go:393 +0x9dd github.com/containerd/nydus-snapshotter/pkg/tarfs.(*Manager).PrepareLayer() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs.go:465 +0x444 github.com/containerd/nydus-snapshotter/pkg/tarfs.TestPrepareLayer() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs_test.go:33 +0x188 testing.tRunner() /opt/hostedtoolcache/go/1.20.1/x64/src/testing/testing.go:1576 +0x216 testing.(*T).Run.func1() /opt/hostedtoolcache/go/1.20.1/x64/src/testing/testing.go:1629 +0x47 Goroutine 40 (finished) created at: os/exec.(*Cmd).Start() /opt/hostedtoolcache/go/1.20.1/x64/src/os/exec/exec.go:716 +0xf8e github.com/containerd/containerd/archive/compression.cmdStream() /home/runner/go/pkg/mod/github.com/containerd/[email protected]/archive/compression/compression.go:284 +0x36f github.com/containerd/containerd/archive/compression.gzipDecompress() /home/runner/go/pkg/mod/github.com/containerd/[email protected]/archive/compression/compression.go:272 +0x152 github.com/containerd/containerd/archive/compression.DecompressStream() /home/runner/go/pkg/mod/github.com/containerd/[email protected]/archive/compression/compression.go:203 +0x3e4 github.com/containerd/nydus-snapshotter/pkg/tarfs.(*Manager).blobProcess.func2() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs.go:341 +0x1b1 github.com/containerd/nydus-snapshotter/pkg/tarfs.(*Manager).blobProcess.func3() /home/runner/work/nydus-snapshotter/nydus-snapshotter/pkg/tarfs/tarfs.go:394 +0x71 ================== testing.go:1446: race detected during execution of test Signed-off-by: Jiang Liu <[email protected]>
1 parent f0323b2 commit ea0b54c

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

pkg/tarfs/tarfs.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,19 @@ func (t *Manager) blobProcess(ctx context.Context, snapshotID, ref string,
384384
return epilog(err, "get blob stream for layer")
385385
}
386386

387-
if retry {
388-
// Download and convert layer content in synchronous mode when retry for error recovering
389-
err = process(rc, remote)
390-
} else {
391-
// Download and convert layer content in background.
392-
// Will retry when the content is actually needed if the background process failed.
393-
go func() {
394-
_ = process(rc, remote)
395-
}()
396-
}
387+
err = process(rc, remote)
388+
/*
389+
if retry {
390+
// Download and convert layer content in synchronous mode when retry for error recovering
391+
err = process(rc, remote)
392+
} else {
393+
// Download and convert layer content in background.
394+
// Will retry when the content is actually needed if the background process failed.
395+
go func() {
396+
_ = process(rc, remote)
397+
}()
398+
}
399+
*/
397400

398401
return err
399402
}
@@ -999,15 +1002,15 @@ func (t *Manager) CheckTarfsHintAnnotation(ctx context.Context, ref string, mani
9991002
if err != nil {
10001003
return false, err
10011004
}
1002-
remote := remote.New(keyChain, t.insecure)
1005+
remoteFactory := remote.New(keyChain, t.insecure)
10031006

10041007
handle := func() (bool, error) {
10051008
if tarfsHint, ok := t.tarfsHintCache.Get(ref); ok {
10061009
return tarfsHint.(bool), nil
10071010
}
10081011

10091012
if _, err, _ := t.sg.Do(ref, func() (interface{}, error) {
1010-
err := t.fetchImageInfo(ctx, remote, ref, manifestDigest)
1013+
err := t.fetchImageInfo(ctx, remoteFactory, ref, manifestDigest)
10111014
return nil, err
10121015
}); err != nil {
10131016
return false, err
@@ -1021,7 +1024,8 @@ func (t *Manager) CheckTarfsHintAnnotation(ctx context.Context, ref string, mani
10211024
}
10221025

10231026
tarfsHint, err := handle()
1024-
if err != nil && remote.RetryWithPlainHTTP(ref, err) {
1027+
if err != nil && remoteFactory.RetryWithPlainHTTP(ref, err) {
1028+
remoteFactory = remote.New(keyChain, t.insecure)
10251029
tarfsHint, err = handle()
10261030
}
10271031
return tarfsHint, err

pkg/tarfs/tarfs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
// TODO: add unit test for MergeLayers, ExportBlockData, MountErofs, RemountErofs, UmountTarErofs, DetachLayer, RecoverSnapshoInfo, RecoverRafsInstance
2727

2828
func TestPrepareLayer(t *testing.T) {
29-
manager := NewManager(true, true, "/tmp/tarfs", "/usr/bin/nydus-image", 4)
29+
manager := NewManager(true, true, t.TempDir(), "/usr/bin/nydus-image", 4)
3030
manifestDigest, err := digest.Parse(BusyboxManifestDigest)
3131
assert.Assert(t, err)
3232
layerDigest, err := digest.Parse(BusyboxLayerDigest)

0 commit comments

Comments
 (0)