Skip to content

Close resources left by go-git after request is finished #8915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ type Context struct {
Org *Organization
}

// Close Releases resources used by this context, like file descriptors
func (ctx *Context) Close() {
if ctx.Repo != nil && ctx.Repo.GitRepo != nil {
if err := ctx.Repo.GitRepo.Close(); err != nil {
log.Warn("Unable to clean up repository resources %s", err.Error())
}
}
}

// IsUserSiteAdmin returns true if current user is a site admin
func (ctx *Context) IsUserSiteAdmin() bool {
return ctx.IsSigned && ctx.User.IsAdmin
Expand Down Expand Up @@ -342,3 +351,12 @@ func Contexter() macaron.Handler {
c.Map(ctx)
}
}

// Cleanup Cleans up used resources like open file descriptors at the end of the request
func Cleanup() macaron.Handler {
return func(ctx *Context) {
defer ctx.Close()

ctx.Next()
}
}
5 changes: 5 additions & 0 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func OpenRepository(repoPath string) (*Repository, error) {
}, nil
}

// Close Release file descriptors that are left open for performance reasons
func (repo *Repository) Close() error {
return repo.gogitStorage.Close()
}

// IsEmpty Check if repository is empty.
func (repo *Repository) IsEmpty() (bool, error) {
var errbuf strings.Builder
Expand Down
13 changes: 13 additions & 0 deletions routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
// Check if repository name has been changed.
if repo.LowerName != strings.ToLower(newRepoName) {
isNameChanged = true

// Close any file descriptors, primarily for Windows which refuses to move directories with open descriptors
if err := ctx.Repo.GitRepo.Close(); err != nil {
ctx.ServerError("ChangeRepositoryName", err)
return
}

if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
ctx.Data["Err_RepoName"] = true
switch {
Expand Down Expand Up @@ -370,6 +377,12 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
return
}

// Close any file descriptors, primarily for Windows which refuses to move directories with open descriptors
if err := ctx.Repo.GitRepo.Close(); err != nil {
ctx.ServerError("ChangeRepositoryName", err)
return
}

oldOwnerID := ctx.Repo.Owner.ID
if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
if models.IsErrRepoAlreadyExist(err) {
Expand Down
2 changes: 2 additions & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func NewMacaron() *macaron.Macaron {
// OK we are now set-up enough to allow us to create a nicer recovery than
// the default macaron recovery
m.Use(context.Recovery())

m.Use(context.Cleanup())
m.SetAutoHead(true)
return m
}
Expand Down