Skip to content

Commit

Permalink
fix(git): hang on git i/o error
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2024
1 parent bba9b26 commit 81ee0ec
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions pkg/git/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"os"
"os/exec"
"strings"
"sync"

"golang.org/x/sync/errgroup"
"github.com/charmbracelet/log"
)

// Service is a Git daemon service.
Expand Down Expand Up @@ -118,34 +119,46 @@ func gitServiceHandler(ctx context.Context, svc Service, scmd ServiceCommand) er
return err
}

errg, _ := errgroup.WithContext(ctx)
wg := &sync.WaitGroup{}

// stdin
if scmd.Stdin != nil {
errg.Go(func() error {
go func() {
defer stdin.Close() // nolint: errcheck
_, err := io.Copy(stdin, scmd.Stdin)
return err
})
if _, err := io.Copy(stdin, scmd.Stdin); err != nil {
log.Errorf("gitServiceHandler: failed to copy stdin: %v", err)
}
}()
}

// stdout
if scmd.Stdout != nil {
errg.Go(func() error {
_, err := io.Copy(scmd.Stdout, stdout)
return err
})
wg.Add(1)
go func() {
defer wg.Done()
if _, err := io.Copy(scmd.Stdout, stdout); err != nil {
log.Errorf("gitServiceHandler: failed to copy stdout: %v", err)
}
}()
}

// stderr
if scmd.Stderr != nil {
errg.Go(func() error {
_, erro := io.Copy(scmd.Stderr, stderr)
return erro
})
wg.Add(1)
go func() {
defer wg.Done()
if _, erro := io.Copy(scmd.Stderr, stderr); err != nil {
log.Errorf("gitServiceHandler: failed to copy stderr: %v", erro)
}
}()
}

err = errors.Join(errg.Wait(), cmd.Wait())
// Ensure all the output is written before waiting for the command to
// finish.
// Stdin is handled by the client side.
wg.Wait()

err = cmd.Wait()
if err != nil && errors.Is(err, os.ErrNotExist) {
return ErrInvalidRepo
} else if err != nil {
Expand Down

0 comments on commit 81ee0ec

Please sign in to comment.