Skip to content

Commit

Permalink
fix comments and callback type
Browse files Browse the repository at this point in the history
  • Loading branch information
jack0x2 committed Dec 10, 2024
1 parent 2a34a30 commit 5c31b72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cmd/agent/subcommands/run/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ func StartAgentWithDefaults(ctxChan <-chan context.Context) (<-chan error, error
return errChan, nil
}

// Re-register the ctrl handler because Go uses SetConsoleCtrlHandler and Python uses posix signal calls.
// This is only needed when using the embedded Python module.
// When Python imports the signal module, it overrides the ctrl handler set by Go and installs the Windows posix signal handler.
// Linux uses the POSIX signal handler for both Go and Python, so this is not an issue on Linux.
// As Python checks if the signal handler is not the default handler before overwritting it.
// If CPython adds a way to avoid overriding the ctrl handler, we can remove this workaround.
// If Go adds support to use the posix signal handler on Windows, we can remove this workaround.
// All calls to signal.Notify will no longer work after the Python module is started.
func reRegisterCtrlHandler(log log.Component, _ collector.Component) {
log.Info("Re-registering Ctrl+C handler")
err := winutil.SetConsoleCtrlHandler(func(ctrlType uint32) bool {
Expand Down
14 changes: 9 additions & 5 deletions pkg/util/winutil/winctrlhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
// Package winutil contains Windows OS utilities
package winutil

import "syscall"
import "golang.org/x/sys/windows"

var (
setConsoleCtrlHandler = k32.NewProc("SetConsoleCtrlHandler")
)

// Console control signal constants
//
// https://learn.microsoft.com/en-us/windows/console/handlerroutine

const (
// CtrlCEvent is code for Cntrl+C event
CtrlCEvent = 0
// CtrlBreakEvent is code for Cntrl+Break event
CtrlCEvent = 0
CtrlBreakEvent = 1
)

Expand All @@ -29,9 +31,11 @@ func boolToInt(b bool) int {
}

// SetConsoleCtrlHandler sets the handler function for console control events.
//
// https://learn.microsoft.com/en-us/windows/console/setconsolectrlhandler
func SetConsoleCtrlHandler(handler func(uint32) bool, add bool) error {
ret, _, err := setConsoleCtrlHandler.Call(
uintptr(syscall.NewCallbackCDecl(func(sig uint32) uintptr {
uintptr(windows.NewCallback(func(sig uint32) uintptr {
if handler(sig) {
return 1
}
Expand Down

0 comments on commit 5c31b72

Please sign in to comment.