Skip to content

Commit a04fc56

Browse files
authored
Use request timeout for git service rpc (#20689) (#20693)
This enables git.Command's Run to optionally use the given context directly so its deadline will be respected. Otherwise, it falls back to the previous behavior of using the supplied timeout or a default timeout value of 360 seconds. repo's serviceRPC() calls now use the context's deadline (which is unset/unlimited) instead of the default 6-minute timeout. This means that large repo clones will no longer arbitrarily time out on the upload-pack step, and pushes can take longer than 6 minutes on the receive-pack step. Fixes #20680
1 parent 92d79b5 commit a04fc56

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

modules/git/command.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ func (c *Command) AddArguments(args ...string) *Command {
9595
return c
9696
}
9797

98-
// RunOpts represents parameters to run the command
98+
// RunOpts represents parameters to run the command. If UseContextTimeout is specified, then Timeout is ignored.
9999
type RunOpts struct {
100-
Env []string
101-
Timeout time.Duration
102-
Dir string
103-
Stdout, Stderr io.Writer
104-
Stdin io.Reader
105-
PipelineFunc func(context.Context, context.CancelFunc) error
100+
Env []string
101+
Timeout time.Duration
102+
UseContextTimeout bool
103+
Dir string
104+
Stdout, Stderr io.Writer
105+
Stdin io.Reader
106+
PipelineFunc func(context.Context, context.CancelFunc) error
106107
}
107108

108109
func commonBaseEnvs() []string {
@@ -171,7 +172,15 @@ func (c *Command) Run(opts *RunOpts) error {
171172
desc = fmt.Sprintf("%s %s [repo_path: %s]", c.name, strings.Join(args, " "), opts.Dir)
172173
}
173174

174-
ctx, cancel, finished := process.GetManager().AddContextTimeout(c.parentContext, opts.Timeout, desc)
175+
var ctx context.Context
176+
var cancel context.CancelFunc
177+
var finished context.CancelFunc
178+
179+
if opts.UseContextTimeout {
180+
ctx, cancel, finished = process.GetManager().AddContext(c.parentContext, desc)
181+
} else {
182+
ctx, cancel, finished = process.GetManager().AddContextTimeout(c.parentContext, opts.Timeout, desc)
183+
}
175184
defer finished()
176185

177186
cmd := exec.CommandContext(ctx, c.name, c.args...)

routers/web/repo/http.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,12 @@ func serviceRPC(ctx gocontext.Context, h serviceHandler, service string) {
474474
cmd := git.NewCommand(h.r.Context(), service, "--stateless-rpc", h.dir)
475475
cmd.SetDescription(fmt.Sprintf("%s %s %s [repo_path: %s]", git.GitExecutable, service, "--stateless-rpc", h.dir))
476476
if err := cmd.Run(&git.RunOpts{
477-
Dir: h.dir,
478-
Env: append(os.Environ(), h.environ...),
479-
Stdout: h.w,
480-
Stdin: reqBody,
481-
Stderr: &stderr,
477+
Dir: h.dir,
478+
Env: append(os.Environ(), h.environ...),
479+
Stdout: h.w,
480+
Stdin: reqBody,
481+
Stderr: &stderr,
482+
UseContextTimeout: true,
482483
}); err != nil {
483484
if err.Error() != "signal: killed" {
484485
log.Error("Fail to serve RPC(%s) in %s: %v - %s", service, h.dir, err, stderr.String())

0 commit comments

Comments
 (0)