Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61c51fb

Browse files
authored
Merge pull request #47221 from vvoland/pkg-pools-close-noop-24
[24.0 backport] pkg/ioutils: Make subsequent Close attempts noop
2 parents 70d91b6 + 2918d58 commit 61c51fb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pkg/ioutils/readers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ package ioutils // import "github.com/docker/docker/pkg/ioutils"
33
import (
44
"context"
55
"io"
6+
"runtime/debug"
7+
"sync/atomic"
68

79
// make sure crypto.SHA256, crypto.sha512 and crypto.SHA384 are registered
810
// TODO remove once https://github.com/opencontainers/go-digest/pull/64 is merged.
911
_ "crypto/sha256"
1012
_ "crypto/sha512"
13+
14+
"github.com/sirupsen/logrus"
1115
)
1216

1317
// ReadCloserWrapper wraps an io.Reader, and implements an io.ReadCloser
@@ -16,10 +20,15 @@ import (
1620
type ReadCloserWrapper struct {
1721
io.Reader
1822
closer func() error
23+
closed atomic.Bool
1924
}
2025

2126
// Close calls back the passed closer function
2227
func (r *ReadCloserWrapper) Close() error {
28+
if !r.closed.CompareAndSwap(false, true) {
29+
subsequentCloseWarn("ReadCloserWrapper")
30+
return nil
31+
}
2332
return r.closer()
2433
}
2534

@@ -87,6 +96,7 @@ type cancelReadCloser struct {
8796
cancel func()
8897
pR *io.PipeReader // Stream to read from
8998
pW *io.PipeWriter
99+
closed atomic.Bool
90100
}
91101

92102
// NewCancelReadCloser creates a wrapper that closes the ReadCloser when the
@@ -146,6 +156,17 @@ func (p *cancelReadCloser) closeWithError(err error) {
146156
// Close closes the wrapper its underlying reader. It will cause
147157
// future calls to Read to return io.EOF.
148158
func (p *cancelReadCloser) Close() error {
159+
if !p.closed.CompareAndSwap(false, true) {
160+
subsequentCloseWarn("cancelReadCloser")
161+
return nil
162+
}
149163
p.closeWithError(io.EOF)
150164
return nil
151165
}
166+
167+
func subsequentCloseWarn(name string) {
168+
logrus.Error("subsequent attempt to close " + name)
169+
if logrus.GetLevel() >= logrus.DebugLevel {
170+
logrus.Errorf("stack trace: %s", string(debug.Stack()))
171+
}
172+
}

pkg/ioutils/writers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package ioutils // import "github.com/docker/docker/pkg/ioutils"
22

3-
import "io"
3+
import (
4+
"io"
5+
"sync/atomic"
6+
)
47

58
// NopWriter represents a type which write operation is nop.
69
type NopWriter struct{}
@@ -29,9 +32,14 @@ func (f *NopFlusher) Flush() {}
2932
type writeCloserWrapper struct {
3033
io.Writer
3134
closer func() error
35+
closed atomic.Bool
3236
}
3337

3438
func (r *writeCloserWrapper) Close() error {
39+
if !r.closed.CompareAndSwap(false, true) {
40+
subsequentCloseWarn("WriteCloserWrapper")
41+
return nil
42+
}
3543
return r.closer()
3644
}
3745

0 commit comments

Comments
 (0)