Skip to content

Commit

Permalink
Fix stopping dev server on Windows (#1129)
Browse files Browse the repository at this point in the history
* Kill dev server on Windows when stopping

* add windows dev server interrupt

* change interrupt with terminate signal

* license

* change to interrupt signal

* lint
  • Loading branch information
feedmeapples authored Jun 13, 2023
1 parent 11c2cb9 commit 5075346
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
3 changes: 1 addition & 2 deletions testsuite/devserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"path/filepath"
"runtime"
"strings"
"syscall"
"time"

"go.temporal.io/sdk/client"
Expand Down Expand Up @@ -361,7 +360,7 @@ func retryFor(maxAttempts int, interval time.Duration, cond func() error) error

// Stop the running server and wait for shutdown to complete. Error is propagated from server shutdown.
func (s *DevServer) Stop() error {
if err := s.cmd.Process.Signal(syscall.SIGTERM); err != nil {
if err := sendInterrupt(s.cmd.Process); err != nil {
return err
}
return s.cmd.Wait()
Expand Down
35 changes: 35 additions & 0 deletions testsuite/interrupt_nonwindows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// The MIT License
//
// Copyright (c) 2023 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

//go:build !windows

package testsuite

import (
"os"
"syscall"
)

// sendInterrupt sends an interrupt signal to the given process for graceful shutdown.
func sendInterrupt(process *os.Process) error {
return process.Signal(syscall.SIGINT)
}
65 changes: 65 additions & 0 deletions testsuite/interrupt_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// The MIT License
//
// Copyright (c) 2023 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package testsuite

import (
"os"
"syscall"

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

// 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")
if err != nil {
return err
}
r1, _, err = f.Call(windows.CTRL_BREAK_EVENT, uintptr(process.Pid))
if r1 == 0 {
return err
}
return nil
}

0 comments on commit 5075346

Please sign in to comment.