From 0274933c22ac71f5289c0e227158f4a7feecc4f7 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 31 Aug 2021 10:06:01 +0200 Subject: [PATCH] Fix git.Blob.DataAsync(): close pipe since we return a NopCloser (#16899) (#16900) * 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) Co-authored-by: zeripath --- 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 5b42920ebe..f70ecb5333 100644 --- a/modules/git/blob_nogogit.go +++ b/modules/git/blob_nogogit.go @@ -46,8 +46,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) @@ -105,12 +105,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 @@ -118,14 +118,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 1e4df802b9..2d9670d36a 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -623,7 +623,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 9d678eccdd..4f6daa3b73 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 251aa1e7e9..ddd4b2618f 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1012,10 +1012,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() {