Fix missing GetObjectNInfo Closure (#11243)

Review for missing Close of returned value from `GetObjectNInfo`.

This was often obscured by the stuff that auto-unlocks when reaching EOF.
This commit is contained in:
Klaus Post 2021-01-08 10:12:26 -08:00 committed by GitHub
parent 4593b146be
commit 51dad1d130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 10 deletions

View File

@ -341,13 +341,13 @@ func transitionObject(ctx context.Context, objectAPI ObjectLayer, objInfo Object
}
oi := gr.ObjInfo
if oi.TransitionStatus == lifecycle.TransitionComplete {
gr.Close() // make sure to avoid leaks.
gr.Close()
return nil
}
putOpts := putTransitionOpts(oi)
if _, err = tgt.PutObject(ctx, arn.Bucket, oi.Name, gr, oi.Size, "", "", putOpts); err != nil {
gr.Close() // make sure to avoid leaks.
gr.Close()
return err
}
gr.Close()

View File

@ -471,6 +471,8 @@ func replicateObject(ctx context.Context, objInfo ObjectInfo, objectAPI ObjectLa
for k, v := range putOpts.Header() {
headerSize += len(k) + len(v)
}
// r takes over closing gr.
r := bandwidth.NewMonitoredReader(ctx, globalBucketMonitor, objInfo.Bucket, objInfo.Name, gr, headerSize, b, target.BandwidthLimit)
_, err = tgt.PutObject(ctx, dest.Bucket, object, r, size, "", "", putOpts)
if err != nil {

View File

@ -478,6 +478,7 @@ func (d *dataUsageCache) load(ctx context.Context, store objectIO, name string)
*d = dataUsageCache{}
return nil
}
defer r.Close()
if err := d.deserialize(r); err != nil {
*d = dataUsageCache{}
logger.LogOnceIf(ctx, err, err.Error())

View File

@ -28,7 +28,7 @@ type MonitoredReader struct {
bucket string // Token to track bucket
bucketMeasurement *bucketMeasurement // bucket measurement object
object string // Token to track object
reader io.Reader // Reader to wrap
reader io.ReadCloser // Reader to wrap
lastStop time.Time // Last timestamp for a measurement
headerSize int // Size of the header not captured by reader
throttle *throttle // throttle the rate at which replication occur
@ -36,8 +36,9 @@ type MonitoredReader struct {
closed bool // Reader is closed
}
// NewMonitoredReader returns a io.ReadCloser that reports bandwidth details
func NewMonitoredReader(ctx context.Context, monitor *Monitor, bucket string, object string, reader io.Reader, headerSize int, bandwidthBytesPerSecond int64, clusterBandwidth int64) *MonitoredReader {
// NewMonitoredReader returns a io.ReadCloser that reports bandwidth details.
// The supplied reader will be closed.
func NewMonitoredReader(ctx context.Context, monitor *Monitor, bucket string, object string, reader io.ReadCloser, headerSize int, bandwidthBytesPerSecond int64, clusterBandwidth int64) *MonitoredReader {
timeNow := time.Now()
b := monitor.track(bucket, object, timeNow)
return &MonitoredReader{
@ -77,10 +78,9 @@ func (m *MonitoredReader) Read(p []byte) (n int, err error) {
// Close stops tracking the io
func (m *MonitoredReader) Close() error {
rc, ok := m.reader.(io.ReadCloser)
m.closed = true
if ok {
return rc.Close()
if m.closed {
return nil
}
return nil
m.closed = true
return m.reader.Close()
}