snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
This commit is contained in:
67
modules/process/command_unix_test.go
Normal file
67
modules/process/command_unix_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build unix
|
||||
|
||||
package process
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCommandContextCancelKillProcessGroup(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
|
||||
// When executing external commands, there can be multiple subprocesses involved.
|
||||
// e.g.: Gitea -> "git upload-pack" -> "git pack-objects".
|
||||
// If the context is canceled (HTTP client disconnects), all subprocesses must terminate.
|
||||
|
||||
// Spawn a shell that itself spawns a long-lived background process and
|
||||
// prints its PID — mimicking git upload-pack spawning git pack-objects.
|
||||
|
||||
r, w, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
|
||||
cmd := CommandContext(ctx, "sh", "-c", "sleep 600 & echo $!; wait")
|
||||
cmd.Stdout = w
|
||||
require.NoError(t, cmd.Start())
|
||||
_ = w.Close() // parent keeps only the read end
|
||||
|
||||
t.Cleanup(func() {
|
||||
// make sure our test doesn't leave a zombie process even if test fails
|
||||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
_ = cmd.Wait()
|
||||
})
|
||||
|
||||
// Block until the shell prints the grandchild PID.
|
||||
scanner := bufio.NewScanner(r)
|
||||
require.True(t, scanner.Scan(), "expected grandchild PID on stdout")
|
||||
grandchildPID, err := strconv.Atoi(strings.TrimSpace(scanner.Text()))
|
||||
require.NoError(t, err)
|
||||
_ = r.Close()
|
||||
|
||||
// Sanity: grandchild must be alive before we cancel.
|
||||
grandchild, err := os.FindProcess(grandchildPID)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, grandchild.Signal(syscall.Signal(0)), "grandchild should be alive before cancel")
|
||||
|
||||
// Cancel the context
|
||||
cancel()
|
||||
_ = cmd.Wait()
|
||||
|
||||
// Subprocess should not exist after context cancel (killed by process group)
|
||||
assert.Eventually(t, func() bool {
|
||||
return grandchild.Signal(syscall.Signal(0)) != nil
|
||||
}, 5*time.Second, 10*time.Millisecond)
|
||||
}
|
||||
Reference in New Issue
Block a user