Skip to content

Commit 66d222a

Browse files
committed
Add finalizers to ensure that repos are closed and blobreaders are closed
It may be prudent to add runtime finalizers to the git.Repository and git.blobReader objects to absolutely ensure that these are both properly cancelled, cleaned and closed out. This commit is an extract from go-gitea#19448 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 4e912a6 commit 66d222a

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

modules/git/blob_nogogit.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"bytes"
1313
"io"
1414
"math"
15+
"runtime"
1516

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

57-
return &blobReader{
58+
br := &blobReader{
5859
rd: rd,
5960
n: size,
6061
cancel: cancel,
61-
}, nil
62+
}
63+
runtime.SetFinalizer(br, func(br *blobReader) { br.Close() })
64+
65+
return br, nil
6266
}
6367

6468
// Size returns the uncompressed size of the blob

modules/git/repo_base_gogit.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6363
return nil, err
6464
}
6565

66-
return &Repository{
66+
repo := &Repository{
6767
Path: repoPath,
6868
gogitRepo: gogitRepo,
6969
gogitStorage: storage,
7070
tagCache: newObjectCache(),
7171
Ctx: ctx,
72-
}, nil
72+
}
73+
74+
runtime.SetFinalizer(repo, func(repo *Repository) { repo.Close() })
75+
76+
return repo, nil
7377
}
7478

7579
// Close this repository, in particular close the underlying gogitStorage if this is not nil

modules/git/repo_base_nogogit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"context"
1414
"errors"
1515
"path/filepath"
16+
"runtime"
1617

1718
"code.gitea.io/gitea/modules/log"
1819
)
@@ -64,6 +65,8 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
6465
repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
6566
repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)
6667

68+
runtime.SetFinalizer(repo, func(repo *Repository) { repo.Close() })
69+
6770
return repo, nil
6871
}
6972

0 commit comments

Comments
 (0)