Skip to content

Commit

Permalink
fix: fixed binary exit in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Sep 12, 2023
1 parent 4d5dd55 commit f41969f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions modules/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package modules
import (
"bufio"
"os/exec"
"runtime"
"syscall"
"time"

Expand Down Expand Up @@ -85,11 +86,34 @@ func (b *Binary) Init() error {
return err
}

// sendCtrlBreak sends a ctrl break signal to the process, this is a replacement for syscall.SIGTERM in windows
func sendCtrlBreak(pid int) error {
d, e := syscall.LoadDLL("kernel32.dll")

Check failure on line 91 in modules/binary.go

View workflow job for this annotation

GitHub Actions / GRPC Test End to End

undefined: syscall.LoadDLL

Check failure on line 91 in modules/binary.go

View workflow job for this annotation

GitHub Actions / Nats Test End to End

undefined: syscall.LoadDLL

Check failure on line 91 in modules/binary.go

View workflow job for this annotation

GitHub Actions / Unit Test

undefined: syscall.LoadDLL
if e != nil {
return e
}
p, e := d.FindProc("GenerateConsoleCtrlEvent")
if e != nil {
return e
}
r, _, e := p.Call(uintptr(syscall.CTRL_BREAK_EVENT), uintptr(pid))

Check failure on line 99 in modules/binary.go

View workflow job for this annotation

GitHub Actions / GRPC Test End to End

undefined: syscall.CTRL_BREAK_EVENT

Check failure on line 99 in modules/binary.go

View workflow job for this annotation

GitHub Actions / Nats Test End to End

undefined: syscall.CTRL_BREAK_EVENT

Check failure on line 99 in modules/binary.go

View workflow job for this annotation

GitHub Actions / Unit Test

undefined: syscall.CTRL_BREAK_EVENT
if r == 0 {
return e // syscall.GetLastError()
}
return nil
}

// Shutdown shutdowns the binary module
func (b *Binary) Shutdown() error {
err := b.cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return err
if runtime.GOOS == "windows" { // windows does not support SIGTERM
if err := sendCtrlBreak(b.cmd.Process.Pid); err != nil {
return err
}
} else {
err := b.cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return err
}
}
timeout := time.After(b.gracefulShutdownInterval)
select {
Expand Down

0 comments on commit f41969f

Please sign in to comment.