Skip to content

subprocess Add support for terminating processes instead of killing them #624

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

Merged
merged 18 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions changes/20250530171020.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `subprocess` Add support for terminating processes instead of killing them
31 changes: 27 additions & 4 deletions utils/subprocess/command_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,50 @@ func (c *cmdWrapper) Run() error {
return ConvertCommandError(c.cmd.Run())
}

func (c *cmdWrapper) Stop() error {
type interruptType int

const (
kill interruptType = 9
term interruptType = 15
)

func (c *cmdWrapper) interrupt(interrupt interruptType) error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
return fmt.Errorf("%w:undefined command", commonerrors.ErrUndefined)
return commonerrors.New(commonerrors.ErrUndefined, "undefined command")
}
subprocess := c.cmd.Process
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var stopErr error
if subprocess != nil {
pid := subprocess.Pid
parallelisation.ScheduleAfter(ctx, 10*time.Millisecond, func(time.Time) {
process, err := proc.FindProcess(ctx, pid)
if process == nil || err != nil {
return
}
_ = process.KillWithChildren(ctx)
switch interrupt {
case kill:
_ = process.KillWithChildren(ctx)
case term:
_ = process.Terminate(ctx)
default:
stopErr = commonerrors.New(commonerrors.ErrInvalid, "unknown interrupt type for process")
}
})
}
_ = c.cmd.Wait()
return nil
return stopErr
}

func (c *cmdWrapper) Stop() error {
return c.interrupt(kill)
}

func (c *cmdWrapper) Interrupt() error {
return c.interrupt(term)
}

func (c *cmdWrapper) Pid() (pid int, err error) {
Expand Down
4 changes: 4 additions & 0 deletions utils/subprocess/command_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func TestCmdStartStop(t *testing.T) {
require.Error(t, err)
err = wrapper.Stop()
require.NoError(t, err)
err = wrapper.Start()
require.Error(t, err)
err = wrapper.Interrupt()
require.NoError(t, err)
})
}
}
29 changes: 29 additions & 0 deletions utils/subprocess/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ func (s *Subprocess) Stop() (err error) {
return s.stop(true)
}

// Interrupt stops the process via the TERM signal.
// This method should be used in combination with `Start`.
// This method is idempotent
func (s *Subprocess) Interrupt() (err error) {
return s.interrupt()
}

// Restart restarts a process. It will stop the process if currently running.
func (s *Subprocess) Restart() (err error) {
err = s.stop(false)
Expand Down Expand Up @@ -314,3 +321,25 @@ func (s *Subprocess) stop(cancel bool) (err error) {
s.messaging.LogEnd(nil)
return
}

func (s *Subprocess) interrupt() (err error) {
if !s.IsOn() {
return
}
err = s.Check()
if err != nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
defer s.Cancel()
if !s.IsOn() {
return
}
s.messaging.LogStopping()
err = s.getCmd().Interrupt()
s.command.Reset()
s.isRunning.Store(false)
s.messaging.LogEnd(nil)
return
}
68 changes: 68 additions & 0 deletions utils/subprocess/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,74 @@ func TestStartStop(t *testing.T) {
}
}

func TestStartInterrupt(t *testing.T) {
currentDir, err := os.Getwd()
require.NoError(t, err)
tests := []struct {
name string
cmdWindows string
argWindows []string
cmdOther string
argOther []string
}{
{
name: "ShortProcess",
cmdWindows: "cmd",
argWindows: []string{"dir", currentDir},
cmdOther: "ls",
argOther: []string{"-l", currentDir},
},
{
name: "LongProcess",
cmdWindows: "cmd",
argWindows: []string{"SLEEP 1"},
cmdOther: "sleep",
argOther: []string{"1"},
},
}

for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
defer goleak.VerifyNone(t)
loggers, err := logs.NewLogrLogger(logstest.NewTestLogger(t), "test")
require.NoError(t, err)

var p *Subprocess
if platform.IsWindows() {
p, err = New(context.Background(), loggers, "", "", "", test.cmdWindows, test.argWindows...)
} else {
p, err = New(context.Background(), loggers, "", "", "", test.cmdOther, test.argOther...)
}
require.NoError(t, err)
require.NotNil(t, p)
assert.False(t, p.IsOn())
err = p.Start()
require.NoError(t, err)
assert.True(t, p.IsOn())

// Checking idempotence
err = p.Start()
require.NoError(t, err)
err = p.Check()
require.NoError(t, err)

time.Sleep(200 * time.Millisecond)
err = p.Restart()
require.NoError(t, err)
assert.True(t, p.IsOn())
err = p.Interrupt()
require.NoError(t, err)
assert.False(t, p.IsOn())
// Checking idempotence
err = p.Interrupt()
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)
err = p.Execute()
require.NoError(t, err)
})
}
}
func TestExecute(t *testing.T) {
currentDir, err := os.Getwd()
require.NoError(t, err)
Expand Down
Loading