fix: repo home page 500 due to the timeout of "get last commit info" (#38678)

Regression of the ctx removal from "git.Repository" struct (the old code
was already wrong and can still cause 500, the "ctx removal" just makes
the problem easier to reproduce).

Merge duplicate code.

Reviewd by codex: no actionable findings.
This commit is contained in:
wxiaoguang
2026-07-28 19:12:53 +08:00
committed by GitHub
parent a75ed103f4
commit 0ab3d569b4
8 changed files with 113 additions and 200 deletions
+10 -7
View File
@@ -280,14 +280,14 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (_ CatFileBatch, close
repo.mu.Lock()
defer repo.mu.Unlock()
if repo.catFileBatchCloser != nil && !repo.catFileBatchInUse {
if ctx != repo.catFileBatchCloser.Context() {
repo.catFileBatchCloser.Close()
repo.catFileBatchCloser = nil
repo.catFileBatchInUse = false
}
// clean up the cached but canceled batcher
if repo.catFileBatchCloser != nil && repo.catFileBatchCloser.Context().Err() != nil && !repo.catFileBatchInUse {
repo.catFileBatchCloser.Close()
repo.catFileBatchCloser = nil
repo.catFileBatchInUse = false
}
// if no cached batcher, make a new managed one, and cache it
if repo.catFileBatchCloser == nil {
repo.catFileBatchCloser, err = NewBatch(ctx, repo)
if err != nil {
@@ -296,7 +296,10 @@ func (repo *Repository) CatFileBatch(ctx context.Context) (_ CatFileBatch, close
}
}
if !repo.catFileBatchInUse {
// if the cached batcher is in use, or the cached ctx is not the current ctx, then we need a new temp one
// for example: the cached one is from request context, the current ctx is a cancelable ctx with timeout
needTemp := repo.catFileBatchInUse || repo.catFileBatchCloser.Context() != ctx
if !needTemp {
repo.catFileBatchInUse = true
return CatFileBatch(repo.catFileBatchCloser), func() {
repo.mu.Lock()