From 1ef9b21e7f6b49d118a3ce3754f0a043384b2173 Mon Sep 17 00:00:00 2001 From: Roman Perekhod Date: Mon, 21 Oct 2024 18:17:50 +0200 Subject: [PATCH] Fix panic when stopping the nats --- changelog/unreleased/fix-panic-nats.md | 6 ++++++ services/idm/pkg/command/server.go | 2 +- services/nats/pkg/command/server.go | 2 +- services/nats/pkg/server/nats/nats.go | 5 +++++ services/postprocessing/pkg/command/server.go | 2 +- 5 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 changelog/unreleased/fix-panic-nats.md diff --git a/changelog/unreleased/fix-panic-nats.md b/changelog/unreleased/fix-panic-nats.md new file mode 100644 index 000000000..09c24d530 --- /dev/null +++ b/changelog/unreleased/fix-panic-nats.md @@ -0,0 +1,6 @@ +Bugfix: Fix panic when stopping the nats + +The nats server itself runs signal handling that the Shutdown() call in the ocis code is redundant and led to a panic. + +https://github.com/owncloud/ocis/pull/10363 +https://github.com/owncloud/ocis/issues/10360 diff --git a/services/idm/pkg/command/server.go b/services/idm/pkg/command/server.go index 19a3f6d9e..87d058de9 100644 --- a/services/idm/pkg/command/server.go +++ b/services/idm/pkg/command/server.go @@ -76,7 +76,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(func() error { - err := make(chan error) + err := make(chan error, 1) select { case <-ctx.Done(): return nil diff --git a/services/nats/pkg/command/server.go b/services/nats/pkg/command/server.go index c5401b2db..4a7df8d18 100644 --- a/services/nats/pkg/command/server.go +++ b/services/nats/pkg/command/server.go @@ -90,7 +90,7 @@ func Server(cfg *config.Config) *cli.Command { } gr.Add(func() error { - err := make(chan error) + err := make(chan error, 1) select { case <-ctx.Done(): return nil diff --git a/services/nats/pkg/server/nats/nats.go b/services/nats/pkg/server/nats/nats.go index e346596fd..984d01d74 100644 --- a/services/nats/pkg/server/nats/nats.go +++ b/services/nats/pkg/server/nats/nats.go @@ -14,6 +14,7 @@ type NATSServer struct { server *nserver.Server } +// NatsOption configures the new NATSServer instance func NewNATSServer(ctx context.Context, logger nserver.Logger, opts ...NatsOption) (*NATSServer, error) { natsOpts := &nserver.Options{} @@ -23,6 +24,8 @@ func NewNATSServer(ctx context.Context, logger nserver.Logger, opts ...NatsOptio // enable JetStream natsOpts.JetStream = true + // The NATS server itself runs the signal handling. We set `natsOpts.NoSigs = true` because we want to handle signals ourselves + natsOpts.NoSigs = true server, err := nserver.NewServer(natsOpts) if err != nil { @@ -44,6 +47,8 @@ func (n *NATSServer) ListenAndServe() (err error) { return nil } +// Shutdown stops the NATSServer gracefully func (n *NATSServer) Shutdown() { n.server.Shutdown() + n.server.WaitForShutdown() } diff --git a/services/postprocessing/pkg/command/server.go b/services/postprocessing/pkg/command/server.go index 3b633ee7b..51eba2a38 100644 --- a/services/postprocessing/pkg/command/server.go +++ b/services/postprocessing/pkg/command/server.go @@ -66,7 +66,7 @@ func Server(cfg *config.Config) *cli.Command { return err } gr.Add(func() error { - err := make(chan error) + err := make(chan error, 1) select { case <-ctx.Done(): return nil