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 <art27@cantab.net>
release/v1.15
6543 2021-08-31 10:06:01 +02:00 committed by GitHub
parent 67776372d6
commit 0274933c22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 9 deletions

View File

@ -46,8 +46,8 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
if size < 4096 { if size < 4096 {
bs, err := ioutil.ReadAll(io.LimitReader(rd, size)) bs, err := ioutil.ReadAll(io.LimitReader(rd, size))
defer cancel()
if err != nil { if err != nil {
cancel()
return nil, err return nil, err
} }
_, err = rd.Discard(1) _, err = rd.Discard(1)
@ -105,12 +105,12 @@ func (b *blobReader) Read(p []byte) (n int, err error) {
// Close implements io.Closer // Close implements io.Closer
func (b *blobReader) Close() error { func (b *blobReader) Close() error {
defer b.cancel()
if b.n > 0 { if b.n > 0 {
for b.n > math.MaxInt32 { for b.n > math.MaxInt32 {
n, err := b.rd.Discard(math.MaxInt32) n, err := b.rd.Discard(math.MaxInt32)
b.n -= int64(n) b.n -= int64(n)
if err != nil { if err != nil {
b.cancel()
return err return err
} }
b.n -= math.MaxInt32 b.n -= math.MaxInt32
@ -118,14 +118,12 @@ func (b *blobReader) Close() error {
n, err := b.rd.Discard(int(b.n)) n, err := b.rd.Discard(int(b.n))
b.n -= int64(n) b.n -= int64(n)
if err != nil { if err != nil {
b.cancel()
return err return err
} }
} }
if b.n == 0 { if b.n == 0 {
_, err := b.rd.Discard(1) _, err := b.rd.Discard(1)
b.n-- b.n--
b.cancel()
return err return err
} }
return nil return nil

View File

@ -623,7 +623,6 @@ func CompareDiff(ctx *context.Context) {
headGitRepo.Close() headGitRepo.Close()
} }
}() }()
if ctx.Written() { if ctx.Written() {
return return
} }

View File

@ -419,9 +419,6 @@ func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repos
} }
handleTeamMentions(ctx) handleTeamMentions(ctx)
if ctx.Written() {
return
}
} }
func retrieveProjects(ctx *context.Context, repo *models.Repository) { func retrieveProjects(ctx *context.Context, repo *models.Repository) {

View File

@ -1012,10 +1012,14 @@ func CompareAndPullRequestPost(ctx *context.Context) {
) )
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx) headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)
defer func() {
if headGitRepo != nil {
headGitRepo.Close()
}
}()
if ctx.Written() { if ctx.Written() {
return return
} }
defer headGitRepo.Close()
labelIDs, assigneeIDs, milestoneID, _ := ValidateRepoMetas(ctx, *form, true) labelIDs, assigneeIDs, milestoneID, _ := ValidateRepoMetas(ctx, *form, true)
if ctx.Written() { if ctx.Written() {