Skip to content
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

Prevent dangling GetAttribute calls #18754

Merged
merged 8 commits into from
Feb 14, 2022
14 changes: 8 additions & 6 deletions modules/git/repo_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func (c *CheckAttributeReader) Init(ctx context.Context) error {
// Run run cmd
func (c *CheckAttributeReader) Run() error {
defer func() {
_ = c.Close()
_ = c.stdinReader.Close()
_ = c.stdOut.Close()
}()
stdErr := new(bytes.Buffer)
err := c.cmd.RunWithContext(&RunContext{
Expand All @@ -196,14 +197,17 @@ func (c *CheckAttributeReader) Run() error {
Stdout: c.stdOut,
Stderr: stdErr,
PipelineFunc: func(_ context.Context, _ context.CancelFunc) error {
close(c.running)
select {
case <-c.running:
default:
close(c.running)
}
return nil
},
})
if err != nil && c.ctx.Err() != nil && err.Error() != "signal: killed" {
return fmt.Errorf("failed to run attr-check. Error: %w\nStderr: %s", err, stdErr.String())
}

return nil
}

Expand Down Expand Up @@ -243,10 +247,8 @@ func (c *CheckAttributeReader) CheckPath(path string) (rs map[string]string, err

// Close close pip after use
func (c *CheckAttributeReader) Close() error {
err := c.stdinWriter.Close()
_ = c.stdinReader.Close()
_ = c.stdOut.Close()
c.cancel()
err := c.stdinWriter.Close()
select {
case <-c.running:
default:
Expand Down
5 changes: 4 additions & 1 deletion modules/git/repo_language_stats_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
}
}()
}
defer cancel()
defer func() {
_ = checker.Close()
cancel()
}()
}
}

Expand Down
1 change: 1 addition & 0 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
}()
}
defer func() {
_ = checker.Close()
6543 marked this conversation as resolved.
Show resolved Hide resolved
cancel()
}()
}
Expand Down