Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 2, 2022
8 changes: 6 additions & 2 deletions modules/git/blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"bytes"
"io"
"math"
"runtime"

"code.gitea.io/gitea/modules/log"
)
Expand Down Expand Up @@ -54,11 +55,14 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(bs)), err
}

return &blobReader{
br := &blobReader{
rd: rd,
n: size,
cancel: cancel,
}, nil
}
runtime.SetFinalizer(br, func(br *blobReader) { br.Close() })

return br, nil
}

// Size returns the uncompressed size of the blob
Expand Down
9 changes: 7 additions & 2 deletions modules/git/repo_base_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"errors"
"path/filepath"
"runtime"

gitealog "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -63,13 +64,17 @@ func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error
return nil, err
}

return &Repository{
repo := &Repository{
Path: repoPath,
gogitRepo: gogitRepo,
gogitStorage: storage,
tagCache: newObjectCache(),
Ctx: ctx,
}, nil
}

runtime.SetFinalizer(repo, func(repo *Repository) { repo.Close() })

return repo, nil
}

// Close this repository, in particular close the underlying gogitStorage if this is not nil
Expand Down
3 changes: 3 additions & 0 deletions modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"context"
"errors"
"path/filepath"
"runtime"

"code.gitea.io/gitea/modules/log"
)
Expand Down Expand Up @@ -64,6 +65,8 @@ func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)

runtime.SetFinalizer(repo, func(repo *Repository) { repo.Close() })

return repo, nil
}

Expand Down