Skip to content

Commit

Permalink
Isolate Ctrl+Break to dev server instead of console (temporalio#1140)
Browse files Browse the repository at this point in the history
* Avoid sending Ctrl+C to all processes in console

* update windows interrupt

* start dev server in new proc group on windows
  • Loading branch information
feedmeapples authored Jun 16, 2023
1 parent ae9ed7b commit 00e8cec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions testsuite/devserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func StartDevServer(ctx context.Context, options DevServerOptions) (*DevServer,
}

args := prepareCommand(&options, host, port, clientOptions.Namespace)
cmd := exec.Command(exePath, args...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr

cmd := newCmd(exePath, args...)
clientOptions.Logger.Info("Starting DevServer", "ExePath", exePath, "Args", args)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed starting: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ package testsuite

import (
"os"
"os/exec"
"syscall"
)

// newCmd creates a new command with the given executable path and arguments.
func newCmd(exePath string, args ...string) *exec.Cmd {
cmd := exec.Command(exePath, args...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
return cmd
}

// sendInterrupt sends an interrupt signal to the given process for graceful shutdown.
func sendInterrupt(process *os.Process) error {
return process.Signal(syscall.SIGINT)
Expand Down
36 changes: 15 additions & 21 deletions testsuite/interrupt_windows.go → testsuite/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,35 @@ package testsuite

import (
"os"
"os/exec"
"syscall"

"golang.org/x/sys/windows"
)

// newCmd creates a new command with the given executable path and arguments.
func newCmd(exePath string, args ...string) *exec.Cmd {
cmd := exec.Command(exePath, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{
// isolate the process and signals sent to it from the current console
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
return cmd
}

// sendInterrupt calls the break event on the given process for graceful shutdown.
func sendInterrupt(process *os.Process) error {
dll, err := windows.LoadDLL("kernel32.dll")
if err != nil {
return err
}
defer dll.Release()
f, err := dll.FindProc("AttachConsole")
if err != nil {
return err
}
r1, _, err := f.Call(uintptr(process.Pid))
if r1 == 0 && err != syscall.ERROR_ACCESS_DENIED {
return err
}

f, err = dll.FindProc("SetConsoleCtrlHandler")
if err != nil {
return err
}
r1, _, err = f.Call(0, 1)
if r1 == 0 {
return err
}
f, err = dll.FindProc("GenerateConsoleCtrlEvent")
p, err := dll.FindProc("GenerateConsoleCtrlEvent")
if err != nil {
return err
}
r1, _, err = f.Call(windows.CTRL_BREAK_EVENT, uintptr(process.Pid))
if r1 == 0 {
r, _, err := p.Call(uintptr(windows.CTRL_BREAK_EVENT), uintptr(process.Pid))
if r == 0 {
return err
}
return nil
Expand Down

0 comments on commit 00e8cec

Please sign in to comment.