Skip to content

test demonstrating orphaned process are not killed with their parent #20264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions modules/process/manager_exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package process

import (
"context"
"fmt"
"os/exec"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func retry(t *testing.T, fun func() error, tries int) {
var err interface{}
for i := 0; i < tries; i++ {
err = fun()
if err == nil {
return
}
<-time.After(1 * time.Second)
}
assert.Fail(t, fmt.Sprintf("Retry: failed \n%v", err))
}

func TestManagerKillGrandChildren(t *testing.T) {
tmp := t.TempDir()

ctx, cancel := context.WithCancel(context.Background())
pm := &Manager{
processMap: make(map[IDType]*process),
next: 1,
}

go func() {
// blocks forever because of the firewall at 4.4.4.4
_, _, _ = pm.ExecDir(ctx, -1, tmp, "GIT description", "git", "clone", "https://4.4.4.4", "something")
}()

// the git clone process forks a grand child git-remote-https, wait for it
pattern := "git-remote-https origin https://4.4.4.4"
ps := func() string {
cmd := exec.Command("ps", "-x", "-o", "pid,ppid,pgid,args")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UNIX-only command, tests can also be run on windows machine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The implementation will need to be different and I'll address that if @zeripath thinks this is a problem that deserves fixing since he recently added a feature to set Gitea children process to be process group leaders.


source

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a build tag to avoid running in windows.

output, err := cmd.CombinedOutput()
assert.NoError(t, err)
return string(output)
}

retry(t, func() error {
out := ps()
if !strings.Contains(out, pattern) {
return fmt.Errorf(out + "Does not contain " + pattern)
}
return nil
}, 5)

// canceling the parent context will cause the child process to be killed
cancel()
<-ctx.Done()

// wait for the git-remote-https grand child process to terminate
retry(t, func() error {
out := ps()
if strings.Contains(out, pattern) {
return fmt.Errorf(out + "Contains " + pattern)
}
return nil
}, 5)
}