Skip to content
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
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ dapr run --run-file /path/to/directory -k
exitWithError = true
print.FailureStatusEvent(os.Stderr, fmt.Sprintf("Error exiting App: %s", output.AppErr))
} else if output.AppCMD != nil && output.AppCMD.Process != nil && (output.AppCMD.ProcessState == nil || !output.AppCMD.ProcessState.Exited()) {
err = output.AppCMD.Process.Kill()
err = killProcessGroup(output.AppCMD.Process)
if err != nil {
// If the process already exited on its own, treat this as a clean shutdown.
if errors.Is(err, os.ErrProcessDone) {
Expand Down Expand Up @@ -1007,7 +1007,7 @@ func killAppProcess(runE *runExec.RunExec) error {
// Process already exited, no need to kill it.
return nil
}
err := runE.AppCMD.Command.Process.Kill()
err := killProcessGroup(runE.AppCMD.Command.Process)
if err != nil {
// If the process already exited on its own
if errors.Is(err, os.ErrProcessDone) {
Expand Down
39 changes: 39 additions & 0 deletions cmd/run_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,54 @@
package cmd

import (
"errors"
"fmt"
"os"
"os/exec"
"syscall"
"time"

"github.com/dapr/cli/pkg/print"
runExec "github.com/dapr/cli/pkg/runexec"
)

// killProcessGroup kills the entire process group of the given process so that
// grandchild processes (e.g. the compiled binary spawned by `go run`) are also
// terminated. It sends SIGTERM first; if the process group is still alive after
// a 5-second grace period, it sends SIGKILL.
func killProcessGroup(process *os.Process) error {
pgid, err := syscall.Getpgid(process.Pid)
if err != nil {
if err == syscall.ESRCH {
return nil // process already gone
}
// Can't determine pgid for some other reason — fall back to single-process kill.
if killErr := process.Kill(); errors.Is(killErr, os.ErrProcessDone) {
return nil
} else {
return killErr
}
}

if err := syscall.Kill(-pgid, syscall.SIGTERM); err == syscall.ESRCH {

Check failure on line 48 in cmd/run_unix.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

shadow: declaration of "err" shadows declaration at line 35 (govet)
return nil // process group already gone
}

const gracePeriod = 5 * time.Second
deadline := time.Now().Add(gracePeriod)
for time.Now().Before(deadline) {
if err := syscall.Kill(-pgid, 0); err == syscall.ESRCH {

Check failure on line 55 in cmd/run_unix.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

shadow: declaration of "err" shadows declaration at line 35 (govet)
return nil // process group gone
}
time.Sleep(100 * time.Millisecond)
}
// Grace period elapsed — force kill.
if err := syscall.Kill(-pgid, syscall.SIGKILL); err == syscall.ESRCH {

Check failure on line 61 in cmd/run_unix.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

shadow: declaration of "err" shadows declaration at line 35 (govet)
return nil
}
return err
}

// setDaprProcessGroupForRun sets the process group on the daprd command so the
// sidecar can be managed independently (e.g. when the app is started via exec).
func setDaprProcessGroupForRun(cmd *exec.Cmd) {
Expand Down
6 changes: 6 additions & 0 deletions cmd/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
runExec "github.com/dapr/cli/pkg/runexec"
)

// killProcessGroup on Windows delegates to Process.Kill; this helper does not manage
// child processes via job objects in the single-app run path.
func killProcessGroup(process *os.Process) error {
return process.Kill()
}

// setDaprProcessGroupForRun is a no-op on Windows (SysProcAttr.Setpgid does not exist).
func setDaprProcessGroupForRun(cmd *exec.Cmd) {
// no-op on Windows
Expand Down
Loading