Skip to content

Add finalizers to ensure that repos are closed and blobreaders are closed #19495

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 13 commits into from

Conversation

zeripath
Copy link
Contributor

It may be prudent to add runtime finalizers to the git.Repository and
git.blobReader objects to absolutely ensure that these are both properly
cancelled, cleaned and closed out.

This commit is an extract from #19448

Signed-off-by: Andrew Thornton [email protected]

…osed

It may be prudent to add runtime finalizers to the git.Repository and
git.blobReader objects to absolutely ensure that these are both properly
cancelled, cleaned and closed out.

This commit is an extract from go-gitea#19448

Signed-off-by: Andrew Thornton <[email protected]>
@GiteaBot GiteaBot added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Apr 25, 2022
@wxiaoguang
Copy link
Contributor

There could be an error log to tell developers that the resource is not closed correctly. Otherwise this patch only hide problems.

Signed-off-by: Andrew Thornton <[email protected]>
@lunny
Copy link
Member

lunny commented Apr 26, 2022

I don't think it's a good idea to do that. We should find the real problem.

Signed-off-by: Andrew Thornton <[email protected]>
@zeripath
Copy link
Contributor Author

I don't think it's a good idea to do that. We should find the real problem.

I have no information to suggest that we have any leaks, nor that we should rely on finalizers to deal with them.

However, if we do have leaks - it is infinitely better for a finalizer to take care of them than to have a leak there.

But I do take your point that this could hide a leak - which isn't great so to that end I have pushed a commit that would add some warnings IF the finalizer were to be relied on.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Apr 27, 2022
@6543 6543 added the backport/done All backports for this PR have been created label Apr 27, 2022
@6543
Copy link
Member

6543 commented Apr 27, 2022

I just wonder if this cost us performance?

wxiaoguang pushed a commit that referenced this pull request May 2, 2022
…osed (#19495) (#19496)

It may be prudent to add runtime finalizers to the git.Repository and
git.blobReader objects to absolutely ensure that these are both properly
cancelled, cleaned and closed out.

This commit is a backport of an extract from #19448

Signed-off-by: Andrew Thornton <[email protected]>
@zeripath
Copy link
Contributor Author

zeripath commented May 7, 2022

So there will probably be a small perf hit:

  1. The addition of locks on the Repository will have some effect.
  2. The GC will hit these objects twice. So in the finalizer it may be sensible to nil out as much of the struct as we can.

Now... the addition of locks is going to be helpful because if this gets merged we can try not opening the batch and batchcheck readers until they're actually first called for.

@Gusted
Copy link
Contributor

Gusted commented May 8, 2022

2. The GC will hit these objects twice.

We should remove the finalizer on the object on the Close() calls to avoid this as much as possible.

@Gusted
Copy link
Contributor

Gusted commented May 8, 2022

Small note: since when are backports merged before the normal PR are merged?

@zeripath
Copy link
Contributor Author

zeripath commented May 8, 2022

OK @Gusted I've setFinalizer(nil) for these structs on close. (except the go-git variant as I am fairly sure that the storage can be reopened even after close.

@zeripath zeripath closed this May 8, 2022
@zeripath zeripath reopened this May 8, 2022
@ashimokawa
Copy link
Contributor

The backport version #19496 introduced a major memory leak.

I am not sure if the recent commits added this one would fix it.

lunny added a commit that referenced this pull request May 9, 2022
Copy link
Member

@6543 6543 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@6543 6543 added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels May 9, 2022
6543 pushed a commit that referenced this pull request May 9, 2022
…s are closed (#19495) (#19496)" (#19659)

This reverts commit 88da506.

because it caused a memleak
@wxiaoguang
Copy link
Contributor

wxiaoguang commented May 9, 2022

FYI, I added a Context.Close() recently, it might help for closing http request context resources. Well, if there are some resources really leaked for other reasons, the Close can not help them. And the reason why the finalizers caused more memory leaks needs to be investigated if it's really the case.

// Close frees all resources hold by Context
func (ctx *Context) Close() error {
var err error
if ctx.Req != nil && ctx.Req.MultipartForm != nil {
err = ctx.Req.MultipartForm.RemoveAll() // remove the temp files buffered to tmp directory
}
// TODO: close opened repo, and more
return err
}

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels May 9, 2022
@6543 6543 added status/blocked This PR cannot be merged yet, i.e. because it depends on another unmerged PR lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. labels May 9, 2022
@lunny lunny modified the milestones: 1.17.0, 1.18.0 Jun 4, 2022
@zeripath zeripath closed this Jul 13, 2022
@lunny lunny removed this from the 1.18.0 milestone Dec 20, 2022
@zeripath zeripath deleted the set-finalizer branch December 29, 2022 19:19
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/done All backports for this PR have been created lgtm/need 1 This PR needs approval from one additional maintainer to be merged. status/blocked This PR cannot be merged yet, i.e. because it depends on another unmerged PR type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants