sdk-go icon indicating copy to clipboard operation
sdk-go copied to clipboard

Stopping dev server not supported on Windows

Open cretz opened this issue 2 years ago • 0 comments

Expected Behavior

Dev server should stop on Windows, but we foolishly used signals. We should make a util_nonwindows.go file with:

//go:build !windows

package main

import (
	"os"
	"syscall"
)

func sendInterrupt(process *os.Process) error {
	return process.Signal(syscall.SIGINT)
}

And a util_windows.go file with:

package main

import (
	"os"
	"syscall"

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

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
}

And then just call sendInterrupt to close the server.

cretz avatar May 02 '23 12:05 cretz