Skip to content

Commit 49a71a6

Browse files
6543zeripath
andauthored
Ensure wiki repos are all closed (#16886) (#16888)
There are multiple places where wiki git repositories are not properly closed. This PR ensures they are closed. Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent 58f4a41 commit 49a71a6

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

routers/web/repo/wiki.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ func wikiContentsByName(ctx *context.Context, commit *git.Commit, wikiName strin
135135
func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
136136
wikiRepo, commit, err := findWikiRepoCommit(ctx)
137137
if err != nil {
138+
if wikiRepo != nil {
139+
wikiRepo.Close()
140+
}
138141
if !git.IsErrNotExist(err) {
139142
ctx.ServerError("GetBranchCommit", err)
140143
}
@@ -222,13 +225,19 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
222225

223226
var buf strings.Builder
224227
if err := markdown.Render(rctx, bytes.NewReader(data), &buf); err != nil {
228+
if wikiRepo != nil {
229+
wikiRepo.Close()
230+
}
225231
ctx.ServerError("Render", err)
226232
return nil, nil
227233
}
228234
ctx.Data["content"] = buf.String()
229235

230236
buf.Reset()
231237
if err := markdown.Render(rctx, bytes.NewReader(sidebarContent), &buf); err != nil {
238+
if wikiRepo != nil {
239+
wikiRepo.Close()
240+
}
232241
ctx.ServerError("Render", err)
233242
return nil, nil
234243
}
@@ -237,6 +246,9 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
237246

238247
buf.Reset()
239248
if err := markdown.Render(rctx, bytes.NewReader(footerContent), &buf); err != nil {
249+
if wikiRepo != nil {
250+
wikiRepo.Close()
251+
}
240252
ctx.ServerError("Render", err)
241253
return nil, nil
242254
}
@@ -380,17 +392,14 @@ func Wiki(ctx *context.Context) {
380392
}
381393

382394
wikiRepo, entry := renderViewPage(ctx)
383-
if ctx.Written() {
384-
if wikiRepo != nil {
385-
wikiRepo.Close()
386-
}
387-
return
388-
}
389395
defer func() {
390396
if wikiRepo != nil {
391397
wikiRepo.Close()
392398
}
393399
}()
400+
if ctx.Written() {
401+
return
402+
}
394403
if entry == nil {
395404
ctx.Data["Title"] = ctx.Tr("repo.wiki")
396405
ctx.HTML(http.StatusOK, tplWikiStart)
@@ -425,17 +434,15 @@ func WikiRevision(ctx *context.Context) {
425434
}
426435

427436
wikiRepo, entry := renderRevisionPage(ctx)
428-
if ctx.Written() {
429-
if wikiRepo != nil {
430-
wikiRepo.Close()
431-
}
432-
return
433-
}
434437
defer func() {
435438
if wikiRepo != nil {
436439
wikiRepo.Close()
437440
}
438441
}()
442+
443+
if ctx.Written() {
444+
return
445+
}
439446
if entry == nil {
440447
ctx.Data["Title"] = ctx.Tr("repo.wiki")
441448
ctx.HTML(http.StatusOK, tplWikiStart)
@@ -472,13 +479,14 @@ func WikiPages(ctx *context.Context) {
472479
}
473480
return
474481
}
475-
476-
entries, err := commit.ListEntries()
477-
if err != nil {
482+
defer func() {
478483
if wikiRepo != nil {
479484
wikiRepo.Close()
480485
}
486+
}()
481487

488+
entries, err := commit.ListEntries()
489+
if err != nil {
482490
ctx.ServerError("ListEntries", err)
483491
return
484492
}
@@ -489,10 +497,6 @@ func WikiPages(ctx *context.Context) {
489497
}
490498
c, err := wikiRepo.GetCommitByPath(entry.Name())
491499
if err != nil {
492-
if wikiRepo != nil {
493-
wikiRepo.Close()
494-
}
495-
496500
ctx.ServerError("GetCommit", err)
497501
return
498502
}
@@ -501,10 +505,6 @@ func WikiPages(ctx *context.Context) {
501505
if models.IsErrWikiInvalidFileName(err) {
502506
continue
503507
}
504-
if wikiRepo != nil {
505-
wikiRepo.Close()
506-
}
507-
508508
ctx.ServerError("WikiFilenameToName", err)
509509
return
510510
}
@@ -516,21 +516,25 @@ func WikiPages(ctx *context.Context) {
516516
}
517517
ctx.Data["Pages"] = pages
518518

519-
defer func() {
520-
if wikiRepo != nil {
521-
wikiRepo.Close()
522-
}
523-
}()
524519
ctx.HTML(http.StatusOK, tplWikiPages)
525520
}
526521

527522
// WikiRaw outputs raw blob requested by user (image for example)
528523
func WikiRaw(ctx *context.Context) {
529524
wikiRepo, commit, err := findWikiRepoCommit(ctx)
530-
if err != nil {
525+
defer func() {
531526
if wikiRepo != nil {
527+
wikiRepo.Close()
528+
}
529+
}()
530+
531+
if err != nil {
532+
if git.IsErrNotExist(err) {
533+
ctx.NotFound("findEntryForFile", nil)
532534
return
533535
}
536+
ctx.ServerError("findEntryForfile", err)
537+
return
534538
}
535539

536540
providedPath := ctx.Params("*")
@@ -546,9 +550,7 @@ func WikiRaw(ctx *context.Context) {
546550

547551
if entry == nil {
548552
// Try to find a wiki page with that name
549-
if strings.HasSuffix(providedPath, ".md") {
550-
providedPath = providedPath[:len(providedPath)-3]
551-
}
553+
providedPath = strings.TrimSuffix(providedPath, ".md")
552554

553555
wikiPath := wiki_service.NameToFilename(providedPath)
554556
entry, err = findEntryForFile(commit, wikiPath)

0 commit comments

Comments
 (0)