Revert "Add finalizers to ensure that repos are closed and blobreaders are closed (#19495) (#19496)" (#19659)

This reverts commit 88da50674f.

because it caused a memleak
This commit is contained in:
Lunny Xiao 2022-05-09 19:03:44 +08:00 committed by GitHub
parent 0a2d618d85
commit c7c18e0eb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 157 deletions

View file

@ -12,11 +12,8 @@ import (
"bytes"
"io"
"math"
"runtime"
"sync"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
)
// Blob represents a Git object.
@ -57,15 +54,11 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(bs)), err
}
br := &blobReader{
repo: b.repo,
return &blobReader{
rd: rd,
n: size,
cancel: cancel,
}
runtime.SetFinalizer(br, (*blobReader).finalizer)
return br, nil
}, nil
}
// Size returns the uncompressed size of the blob
@ -93,10 +86,6 @@ func (b *Blob) Size() int64 {
}
type blobReader struct {
lock sync.Mutex
closed bool
repo *Repository
rd *bufio.Reader
n int64
cancel func()
@ -115,57 +104,27 @@ func (b *blobReader) Read(p []byte) (n int, err error) {
}
// Close implements io.Closer
func (b *blobReader) Close() (err error) {
b.lock.Lock()
defer b.lock.Unlock()
if b.closed {
return
}
return b.close()
}
func (b *blobReader) close() (err error) {
func (b *blobReader) Close() error {
defer b.cancel()
b.closed = true
if b.n > 0 {
var n int
for b.n > math.MaxInt32 {
n, err = b.rd.Discard(math.MaxInt32)
n, err := b.rd.Discard(math.MaxInt32)
b.n -= int64(n)
if err != nil {
return
return err
}
b.n -= math.MaxInt32
}
n, err = b.rd.Discard(int(b.n))
n, err := b.rd.Discard(int(b.n))
b.n -= int64(n)
if err != nil {
return
return err
}
}
if b.n == 0 {
_, err = b.rd.Discard(1)
_, err := b.rd.Discard(1)
b.n--
return
return err
}
return
}
func (b *blobReader) finalizer() error {
if b == nil {
return nil
}
b.lock.Lock()
defer b.lock.Unlock()
if b.closed {
return nil
}
pid := ""
if b.repo.Ctx != nil {
pid = " from PID: " + string(process.GetPID(b.repo.Ctx))
}
log.Error("Finalizer running on unclosed blobReader%s: %s%s", pid, b.repo.Path)
return b.close()
return nil
}