mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-13 13:07:57 +00:00
By default, when you create an `exec.CommandContext` in Go, when the context is cancelled or timed out, Go will SIGKILL the subprocess which does not allow the subprocess to perform any cleanup work. As some git operations in Forgejo will be linked to the current HTTP context, and the current HTTP context will be closed when the HTTP client disconnects, this can theoretically cause SIGKILLs when locks are being held. This PR contains two fixes: - Change the behaviour of all subprocesses that Forgejo creates to use a graceful termination, SIGTERM'ing the process, and then SIGKILL'ing it after a timeout. - Fixes a bug where previously all subprocesses are setup in a process group (by1d04e8641d), but they were never *killed* in a process group, preventing child processes from receiving these termination signals. Both SIGTERM and SIGKILL are sent to the process group. Introduces new configuration setting which controls subcommand graceful timeouts. If a subprocess does not shutdown from SIGTERM within `[server].SUBPROCESS_TERMINATE_GRACE` (default 5 seconds), then an error will be logged, and the process will be SIGKILL'd. The error is: ``` 2026/07/01 08:31:20 modules/log/init.go:38:func2() [E] Subprocess pid=3790963 (/nix/store/c0277k5giric1mn9dklllavbzvxl6hzb-git-2.53.0/bin/git blame --porcelain8ee7e7c32b-- options/locale/locale_en-US.ini) failed to terminate from SIGTERM after grace period 0s, and was sent SIGKILL. The subprocess termination may leave incomplete state on the server, such as lock files. `[server].SUBPROCESS_TERMINATE_GRACE` can be changed to give subprocesses more time to cleanly exit. ``` (This was produced on a live server by setting SUBPROCESS_TERMINATE_GRACE to zero seconds, loading a page that does a `git blame`, and hitting escape in the browser quickly.) ### Tests for Go changes - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - Will create a documentation PR with new config settings. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13263 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package process
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
// Exec a command and use the default timeout.
|
|
func (pm *Manager) Exec(desc, cmdName string, args ...string) (string, string, error) {
|
|
return pm.ExecDir(DefaultContext, -1, "", desc, cmdName, args...)
|
|
}
|
|
|
|
// ExecTimeout a command and use a specific timeout duration.
|
|
func (pm *Manager) ExecTimeout(timeout time.Duration, desc, cmdName string, args ...string) (string, string, error) {
|
|
return pm.ExecDir(DefaultContext, timeout, "", desc, cmdName, args...)
|
|
}
|
|
|
|
// ExecDir a command and use the default timeout.
|
|
func (pm *Manager) ExecDir(ctx context.Context, timeout time.Duration, dir, desc, cmdName string, args ...string) (string, string, error) {
|
|
return pm.ExecDirEnv(ctx, timeout, dir, desc, nil, cmdName, args...)
|
|
}
|
|
|
|
// ExecDirEnv runs a command in given path and environment variables, and waits for its completion up to the given
|
|
// timeout (or 60s if -1 is given). Returns its complete stdout and stderr outputs and an error, if any (including
|
|
// timeout)
|
|
func (pm *Manager) ExecDirEnv(ctx context.Context, timeout time.Duration, dir, desc string, env []string, cmdName string, args ...string) (string, string, error) {
|
|
return pm.ExecDirEnvStdIn(ctx, timeout, dir, desc, env, nil, cmdName, args...)
|
|
}
|
|
|
|
// ExecDirEnvStdIn runs a command in given path and environment variables with provided stdIN, and waits for its
|
|
// completion up to the given timeout (or 60s if timeout <= 0 is given). If the process times out, will send SIGTERM
|
|
// and wait 5s before the process is SIGKILL'd. Returns its complete stdout and stderr outputs and an error, if any
|
|
// (including timeout)
|
|
func (pm *Manager) ExecDirEnvStdIn(ctx context.Context, timeout time.Duration, dir, desc string, env []string, stdIn io.Reader, cmdName string, args ...string) (string, string, error) {
|
|
if timeout <= 0 {
|
|
timeout = 60 * time.Second
|
|
}
|
|
|
|
stdOut := new(bytes.Buffer)
|
|
stdErr := new(bytes.Buffer)
|
|
|
|
ctx, _, finished := pm.AddContextTimeout(ctx, timeout, desc)
|
|
defer finished()
|
|
|
|
cmd := exec.CommandContext(ctx, cmdName, args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = env
|
|
cmd.Stdout = stdOut
|
|
cmd.Stderr = stdErr
|
|
if stdIn != nil {
|
|
cmd.Stdin = stdIn
|
|
}
|
|
SetupCancellableCommand(cmd)
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
err := cmd.Wait()
|
|
if err != nil {
|
|
err = &Error{
|
|
PID: GetPID(ctx),
|
|
Description: desc,
|
|
Err: err,
|
|
CtxErr: ctx.Err(),
|
|
Stdout: stdOut.String(),
|
|
Stderr: stdErr.String(),
|
|
}
|
|
}
|
|
|
|
return stdOut.String(), stdErr.String(), err
|
|
}
|