webview icon indicating copy to clipboard operation
webview copied to clipboard

how to catch the control C, so I can do a clean shutdown?

Open marcelloh opened this issue 7 years ago • 4 comments

OS: MacOs Programming language: Go go version go1.12 darwin/amd64

What did you expect to see and what you saw instead?

^Csignal: interrupt and then the program stopped.

I expect to gain control again and to a nice shutdown of my application This is the webview part of my application

	url := "http://localhost:" + sPort + "/splash"
	wv := webview.New(webview.Settings{
		Title:     "MyTitle",
		URL:       url,
		Width:     1024,
		Height:    800,
		Resizable: true,
	})

	defer wv.Exit()
	wv.Run()

It would be nice if the interrupt could be handled or deferred or something... (but I can't figure out how)

marcelloh avatar Mar 15 '19 13:03 marcelloh

https://stackoverflow.com/questions/11268943/is-it-possible-to-capture-a-ctrlc-signal-and-run-a-cleanup-function-in-a-defe

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
    for sig := range c {
        // sig is a ^C, handle it
    }
}()

gjvnq avatar Mar 15 '19 14:03 gjvnq

Unfortunetally that does not catch cmd+Q or closing the window. An Exit Hook might be nice :-)

phaus avatar Jan 24 '20 16:01 phaus

Simply defer func() {}() works on Linux, but somehow doesn't work in macOS.

I found a solution to all platforms using cgo.

package main

/*
#include <stdlib.h>

extern int OnExit();

void _cleanup() {
	OnExit();
}

void set_cleanup() {
	atexit(_cleanup);
}
*/
import "C"

// OnExit execute on exit, including SIGTERM, SIGINT, and manually closing the window.
//export OnExit
func OnExit() C.int {
	log.Println("Executing clean-up function")
	// time.Sleep(2 * time.Second)
	log.Println("Clean-up finished")

	// Cannot seem to use C.void
	return C.int(0)
}

func main() {
	debug := true
	w := webview.New(debug)

	C.set_cleanup()
	defer w.Destroy()

	w.Run()
}

patarapolw avatar Aug 30 '20 09:08 patarapolw

Getting this error @patarapolw

/opt/homebrew/opt/go/libexec/pkg/tool/darwin_arm64/link: running clang++ failed: exit status 1
duplicate symbol '_set_cleanup' in:
    /var/folders/47/yj2fhzp16zl37k9_6249n_7m0000gn/T/go-link-4190987470/000000.o
    /var/folders/47/yj2fhzp16zl37k9_6249n_7m0000gn/T/go-link-4190987470/000001.o
duplicate symbol '__cleanup' in:
    /var/folders/47/yj2fhzp16zl37k9_6249n_7m0000gn/T/go-link-4190987470/000000.o
    /var/folders/47/yj2fhzp16zl37k9_6249n_7m0000gn/T/go-link-4190987470/000001.o
ld: 2 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ansxuman avatar Feb 04 '23 22:02 ansxuman