pkg/ioutils: Make subsequent Close attempts noop

Turn subsequent `Close` calls into a no-op and produce a warning with an
optional stack trace (if debug mode is enabled).

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2023-09-28 11:15:49 +02:00
parent 7bc56c5365
commit 585d74bad1
No known key found for this signature in database
GPG key ID: B85EFCFE26DEF92A
2 changed files with 30 additions and 1 deletions

View file

@ -3,11 +3,15 @@ package ioutils // import "github.com/docker/docker/pkg/ioutils"
import ( import (
"context" "context"
"io" "io"
"runtime/debug"
"sync/atomic"
// make sure crypto.SHA256, crypto.sha512 and crypto.SHA384 are registered // make sure crypto.SHA256, crypto.sha512 and crypto.SHA384 are registered
// TODO remove once https://github.com/opencontainers/go-digest/pull/64 is merged. // TODO remove once https://github.com/opencontainers/go-digest/pull/64 is merged.
_ "crypto/sha256" _ "crypto/sha256"
_ "crypto/sha512" _ "crypto/sha512"
"github.com/containerd/log"
) )
// ReadCloserWrapper wraps an io.Reader, and implements an io.ReadCloser // ReadCloserWrapper wraps an io.Reader, and implements an io.ReadCloser
@ -16,10 +20,15 @@ import (
type ReadCloserWrapper struct { type ReadCloserWrapper struct {
io.Reader io.Reader
closer func() error closer func() error
closed atomic.Bool
} }
// Close calls back the passed closer function // Close calls back the passed closer function
func (r *ReadCloserWrapper) Close() error { func (r *ReadCloserWrapper) Close() error {
if !r.closed.CompareAndSwap(false, true) {
subsequentCloseWarn("ReadCloserWrapper")
return nil
}
return r.closer() return r.closer()
} }
@ -87,6 +96,7 @@ type cancelReadCloser struct {
cancel func() cancel func()
pR *io.PipeReader // Stream to read from pR *io.PipeReader // Stream to read from
pW *io.PipeWriter pW *io.PipeWriter
closed atomic.Bool
} }
// NewCancelReadCloser creates a wrapper that closes the ReadCloser when the // NewCancelReadCloser creates a wrapper that closes the ReadCloser when the
@ -146,6 +156,17 @@ func (p *cancelReadCloser) closeWithError(err error) {
// Close closes the wrapper its underlying reader. It will cause // Close closes the wrapper its underlying reader. It will cause
// future calls to Read to return io.EOF. // future calls to Read to return io.EOF.
func (p *cancelReadCloser) Close() error { func (p *cancelReadCloser) Close() error {
if !p.closed.CompareAndSwap(false, true) {
subsequentCloseWarn("cancelReadCloser")
return nil
}
p.closeWithError(io.EOF) p.closeWithError(io.EOF)
return nil return nil
} }
func subsequentCloseWarn(name string) {
log.G(context.TODO()).Error("subsequent attempt to close " + name)
if log.GetLevel() >= log.DebugLevel {
log.G(context.TODO()).Errorf("stack trace: %s", string(debug.Stack()))
}
}

View file

@ -1,6 +1,9 @@
package ioutils // import "github.com/docker/docker/pkg/ioutils" package ioutils // import "github.com/docker/docker/pkg/ioutils"
import "io" import (
"io"
"sync/atomic"
)
// NopWriter represents a type which write operation is nop. // NopWriter represents a type which write operation is nop.
type NopWriter struct{} type NopWriter struct{}
@ -29,9 +32,14 @@ func (f *NopFlusher) Flush() {}
type writeCloserWrapper struct { type writeCloserWrapper struct {
io.Writer io.Writer
closer func() error closer func() error
closed atomic.Bool
} }
func (r *writeCloserWrapper) Close() error { func (r *writeCloserWrapper) Close() error {
if !r.closed.CompareAndSwap(false, true) {
subsequentCloseWarn("WriteCloserWrapper")
return nil
}
return r.closer() return r.closer()
} }