ensure git processes are cleaned up with request after timeout
1 file changed, 12 insertions(+), 4 deletions(-)
changed files
M git/service/service.go → git/service/service.go
@@ -2,6 +2,7 @@ package service import ( "bytes" + "context" "errors" "fmt" "io"@@ -10,8 +11,11 @@ "net/http" "os/exec" "strings" "syscall" + "time" ) +const timeout = 1 * time.Minute + // Mostly from charmbracelet/soft-serve and sosedoff/gitkit. type ServiceCommand struct {@@ -20,8 +24,10 @@ Stdin io.Reader Stdout http.ResponseWriter } -func (c *ServiceCommand) InfoRefs() error { - cmd := exec.Command("git", []string{ +func (c *ServiceCommand) InfoRefs(ctx context.Context) error { + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + cmd := exec.CommandContext(ctx, "git", []string{ "upload-pack", "--stateless-rpc", "--advertise-refs",@@ -73,8 +79,10 @@ return nil } -func (c *ServiceCommand) UploadPack() (err error) { - cmd := exec.Command("git", []string{ +func (c *ServiceCommand) UploadPack(ctx context.Context) (err error) { + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + cmd := exec.CommandContext(ctx, "git", []string{ "-c", "uploadpack.allowFilter=true", "upload-pack", "--stateless-rpc",