From 4fbdeb4e3fd77dfc2bef8f9784bf14213d5461e4 Mon Sep 17 00:00:00 2001 From: Revertron <105154+Revertron@users.noreply.github.com> Date: Thu, 25 Jul 2024 14:55:14 +0200 Subject: [PATCH] Fixed Windows service life-cycle. (#1153) This fix fixes two issues: https://github.com/yggdrasil-network/yggdrasil-go/issues/993 & https://github.com/yggdrasil-network/yggdrasil-go/issues/1098 --- cmd/yggdrasil/main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 82b85cd4..29afdf5d 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -53,13 +53,13 @@ func main() { getpkey := flag.Bool("publickey", false, "use in combination with either -useconf or -useconffile, outputs your public key") loglevel := flag.String("loglevel", "info", "loglevel to enable") flag.Parse() - + + done := make(chan struct{}) + defer close(done) + // Catch interrupts from the operating system to exit gracefully. ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) - // Capture the service being stopped on Windows. - minwinsvc.SetOnExit(cancel) - // Create a new logger that logs output to stdout. var logger *log.Logger switch *logto { @@ -271,6 +271,14 @@ func main() { n.tun.SetupAdminHandlers(n.admin) } } + + //Windows service shutdown + minwinsvc.SetOnExit(func() { + logger.Infof("Shutting down service ...") + cancel() + // Wait for all parts to shutdown properly + <-done + }) // Block until we are told to shut down. <-ctx.Done()