Skip to content

Commit a695faf

Browse files
committed
Fix git.Blob.DataAsync(): close pipe since we return a NopCloser (go-gitea#16899)
* make sure headGitRepo is closed on err too * refactor * Fix git.Blob.DataAsync(): exec cancel since we already read all bytes (close pipe since we return a NopCloser)
1 parent 49a71a6 commit a695faf

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

modules/git/blob_nogogit.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
4646

4747
if size < 4096 {
4848
bs, err := ioutil.ReadAll(io.LimitReader(rd, size))
49+
defer cancel()
4950
if err != nil {
50-
cancel()
5151
return nil, err
5252
}
5353
_, err = rd.Discard(1)
@@ -105,27 +105,25 @@ func (b *blobReader) Read(p []byte) (n int, err error) {
105105

106106
// Close implements io.Closer
107107
func (b *blobReader) Close() error {
108+
defer b.cancel()
108109
if b.n > 0 {
109110
for b.n > math.MaxInt32 {
110111
n, err := b.rd.Discard(math.MaxInt32)
111112
b.n -= int64(n)
112113
if err != nil {
113-
b.cancel()
114114
return err
115115
}
116116
b.n -= math.MaxInt32
117117
}
118118
n, err := b.rd.Discard(int(b.n))
119119
b.n -= int64(n)
120120
if err != nil {
121-
b.cancel()
122121
return err
123122
}
124123
}
125124
if b.n == 0 {
126125
_, err := b.rd.Discard(1)
127126
b.n--
128-
b.cancel()
129127
return err
130128
}
131129
return nil

routers/web/repo/compare.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,14 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
618618
// CompareDiff show different from one commit to another commit
619619
func CompareDiff(ctx *context.Context) {
620620
headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
621-
621+
defer func() {
622+
if headGitRepo != nil {
623+
headGitRepo.Close()
624+
}
625+
}()
622626
if ctx.Written() {
623627
return
624628
}
625-
defer headGitRepo.Close()
626629

627630
nothingToCompare := PrepareCompareDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch,
628631
gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)))

routers/web/repo/issue.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,6 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos
419419
}
420420

421421
handleTeamMentions(ctx)
422-
if ctx.Written() {
423-
return
424-
}
425422
}
426423

427424
func retrieveProjects(ctx *context.Context, repo *models.Repository) {

routers/web/repo/pull.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,14 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10161016
)
10171017

10181018
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
1019+
defer func() {
1020+
if headGitRepo != nil {
1021+
headGitRepo.Close()
1022+
}
1023+
}()
10191024
if ctx.Written() {
10201025
return
10211026
}
1022-
defer headGitRepo.Close()
10231027

10241028
labelIDs, assigneeIDs, milestoneID, _ := ValidateRepoMetas(ctx, *form, true)
10251029
if ctx.Written() {

0 commit comments

Comments
 (0)