Skip to content

Commit

Permalink
Prevent dangling archiver goroutine (go-gitea#19516)
Browse files Browse the repository at this point in the history
Backport go-gitea#19516

Within doArchive there is a service goroutine that performs the
archiving function.  This goroutine reports its error using a `chan
error` called `done`. Prior to this PR this channel had 0 capacity
meaning that the goroutine would block until the `done` channel was
cleared - however there are a couple of ways in which this channel might
not be read.

The simplest solution is to add a single space of capacity to the
goroutine which will mean that the goroutine will always complete and
even if the `done` channel is not read it will be simply garbage
collected away.

(The PR also contains two other places when setting up the indexers
which do not leak but where the blocking of the sending goroutine is
also unnecessary and so we should just add a small amount of capacity
and let the sending goroutine complete as soon as it can.)

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: 6543 <6543@obermui.de>
  • Loading branch information
zeripath and 6543 committed Apr 27, 2022
1 parent 9cc93c0 commit bf35a71
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion modules/indexer/code/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Init() {
log.Info("PID: %d Repository Indexer closed", os.Getpid())
})

waitChannel := make(chan time.Duration)
waitChannel := make(chan time.Duration, 1)

// Create the Queue
switch setting.Indexer.RepoType {
Expand Down
2 changes: 1 addition & 1 deletion modules/indexer/issues/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var (
// InitIssueIndexer initialize issue indexer, syncReindex is true then reindex until
// all issue index done.
func InitIssueIndexer(syncReindex bool) {
waitChannel := make(chan time.Duration)
waitChannel := make(chan time.Duration, 1)

// Create the Queue
switch setting.Indexer.IssueType {
Expand Down
2 changes: 1 addition & 1 deletion services/repository/archiver/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) {
w.Close()
rd.Close()
}()
var done = make(chan error)
done := make(chan error, 1) // Ensure that there is some capacity which will ensure that the goroutine below can always finish
repo, err := repo_model.GetRepositoryByID(archiver.RepoID)
if err != nil {
return nil, fmt.Errorf("archiver.LoadRepo failed: %v", err)
Expand Down

0 comments on commit bf35a71

Please sign in to comment.