From d7cc187ec00410b949ffae1625835f8ac9f36c29 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 27 Apr 2022 13:36:40 +0100 Subject: [PATCH] Prevent JetStream from handling OS signals, allow running as a Windows service (#2385) * Prevent JetStream from handling OS signals, allow running as a Windows service (fixes #2374) * Remove double import --- go.mod | 1 + go.sum | 1 + setup/base/base.go | 9 +++++++-- setup/jetstream/nats.go | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index ba222ed8f..d51c3f75d 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/h2non/filetype v1.1.3 // indirect github.com/hashicorp/golang-lru v0.5.4 github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494 // indirect + github.com/kardianos/minwinsvc v1.0.0 // indirect github.com/lib/pq v1.10.5 github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91 diff --git a/go.sum b/go.sum index 8bb306a82..f8daca79e 100644 --- a/go.sum +++ b/go.sum @@ -721,6 +721,7 @@ github.com/julienschmidt/httprouter v1.1.1-0.20151013225520-77a895ad01eb/go.mod github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= +github.com/kardianos/minwinsvc v1.0.0 h1:+JfAi8IBJna0jY2dJGZqi7o15z13JelFIklJCAENALA= github.com/kardianos/minwinsvc v1.0.0/go.mod h1:Bgd0oc+D0Qo3bBytmNtyRKVlp85dAloLKhfxanPFFRc= github.com/kataras/golog v0.0.10/go.mod h1:yJ8YKCmyL+nWjERB90Qwn+bdyBZsaQwU3bTVFgkFIp8= github.com/kataras/iris/v12 v12.1.8/go.mod h1:LMYy4VlP67TQ3Zgriz8RE2h2kMZV2SgMYbq3UhfoFmE= diff --git a/setup/base/base.go b/setup/base/base.go index 43d613b0c..281153444 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -42,6 +42,7 @@ import ( userdb "github.com/matrix-org/dendrite/userapi/storage" "github.com/gorilla/mux" + "github.com/kardianos/minwinsvc" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" asinthttp "github.com/matrix-org/dendrite/appservice/inthttp" @@ -462,7 +463,8 @@ func (b *BaseDendrite) SetupAndServeHTTP( }() } - <-b.ProcessContext.WaitForShutdown() + minwinsvc.SetOnExit(b.ProcessContext.ShutdownDendrite) + b.WaitForShutdown() ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -475,7 +477,10 @@ func (b *BaseDendrite) SetupAndServeHTTP( func (b *BaseDendrite) WaitForShutdown() { sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - <-sigs + select { + case <-sigs: + case <-b.ProcessContext.WaitForShutdown(): + } signal.Reset(syscall.SIGINT, syscall.SIGTERM) logrus.Warnf("Shutdown signal received") diff --git a/setup/jetstream/nats.go b/setup/jetstream/nats.go index 1c8a89e8d..8d5289697 100644 --- a/setup/jetstream/nats.go +++ b/setup/jetstream/nats.go @@ -44,6 +44,7 @@ func Prepare(process *process.ProcessContext, cfg *config.JetStream) (natsclient StoreDir: string(cfg.StoragePath), NoSystemAccount: true, MaxPayload: 16 * 1024 * 1024, + NoSigs: true, }) if err != nil { panic(err)