Skip to content

Commit 4482f62

Browse files
authored
Prevent dangling GetAttribute calls (#18754)
It appears possible that there could be a hang due to unread data from the repo-attribute command pipes. This PR simply closes these during the defer. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 5348e19 commit 4482f62

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

modules/git/repo_attribute.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func (c *CheckAttributeReader) Init(ctx context.Context) error {
185185
// Run run cmd
186186
func (c *CheckAttributeReader) Run() error {
187187
defer func() {
188-
_ = c.Close()
188+
_ = c.stdinReader.Close()
189+
_ = c.stdOut.Close()
189190
}()
190191
stdErr := new(bytes.Buffer)
191192
err := c.cmd.RunWithContext(&RunContext{
@@ -196,14 +197,17 @@ func (c *CheckAttributeReader) Run() error {
196197
Stdout: c.stdOut,
197198
Stderr: stdErr,
198199
PipelineFunc: func(_ context.Context, _ context.CancelFunc) error {
199-
close(c.running)
200+
select {
201+
case <-c.running:
202+
default:
203+
close(c.running)
204+
}
200205
return nil
201206
},
202207
})
203208
if err != nil && c.ctx.Err() != nil && err.Error() != "signal: killed" {
204209
return fmt.Errorf("failed to run attr-check. Error: %w\nStderr: %s", err, stdErr.String())
205210
}
206-
207211
return nil
208212
}
209213

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

244248
// Close close pip after use
245249
func (c *CheckAttributeReader) Close() error {
246-
err := c.stdinWriter.Close()
247-
_ = c.stdinReader.Close()
248-
_ = c.stdOut.Close()
249250
c.cancel()
251+
err := c.stdinWriter.Close()
250252
select {
251253
case <-c.running:
252254
default:

modules/git/repo_language_stats_nogogit.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
8888
}
8989
}()
9090
}
91-
defer cancel()
91+
defer func() {
92+
_ = checker.Close()
93+
cancel()
94+
}()
9295
}
9396
}
9497

services/gitdiff/gitdiff.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,7 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff
14221422
}()
14231423
}
14241424
defer func() {
1425+
_ = checker.Close()
14251426
cancel()
14261427
}()
14271428
}

0 commit comments

Comments
 (0)