From d21702475ba43100d3a6e9505a1ae259e4842524 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 31 Aug 2021 09:43:31 +0200 Subject: [PATCH] Fix git.Blob.DataAsync(): close pipe since we return a NopCloser (#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) --- modules/git/blob_nogogit.go | 6 ++---- routers/web/repo/compare.go | 1 - routers/web/repo/issue.go | 3 --- routers/web/repo/pull.go | 6 +++++- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/modules/git/blob_nogogit.go b/modules/git/blob_nogogit.go index 26815a63f1..3391bc3931 100644 --- a/modules/git/blob_nogogit.go +++ b/modules/git/blob_nogogit.go @@ -47,8 +47,8 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) { if size < 4096 { bs, err := ioutil.ReadAll(io.LimitReader(rd, size)) + defer cancel() if err != nil { - cancel() return nil, err } _, err = rd.Discard(1) @@ -106,12 +106,12 @@ func (b *blobReader) Read(p []byte) (n int, err error) { // Close implements io.Closer func (b *blobReader) Close() error { + defer b.cancel() if b.n > 0 { for b.n > math.MaxInt32 { n, err := b.rd.Discard(math.MaxInt32) b.n -= int64(n) if err != nil { - b.cancel() return err } b.n -= math.MaxInt32 @@ -119,14 +119,12 @@ func (b *blobReader) Close() error { n, err := b.rd.Discard(int(b.n)) b.n -= int64(n) if err != nil { - b.cancel() return err } } if b.n == 0 { _, err := b.rd.Discard(1) b.n-- - b.cancel() return err } return nil diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index ee8d376612..eb6c37a1a6 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -621,7 +621,6 @@ func CompareDiff(ctx *context.Context) { headGitRepo.Close() } }() - if ctx.Written() { return } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 3f7af7a51f..bb3a0c8a9c 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -419,9 +419,6 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos } handleTeamMentions(ctx) - if ctx.Written() { - return - } } func retrieveProjects(ctx *context.Context, repo *models.Repository) { diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 6e83a72c24..8ff2ddf394 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1024,10 +1024,14 @@ func CompareAndPullRequestPost(ctx *context.Context) { ) headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx) + defer func() { + if headGitRepo != nil { + headGitRepo.Close() + } + }() if ctx.Written() { return } - defer headGitRepo.Close() labelIDs, assigneeIDs, milestoneID, _ := ValidateRepoMetas(ctx, *form, true) if ctx.Written() {