From 28d8907c172a79da6973df513d049c9e513ea67c Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 06:39:40 +0000 Subject: [PATCH] fix(lns): make lifecycle hooks nil-safe --- lns.go | 10 +++++++--- lns_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lns.go b/lns.go index 5c32ec4..99c957c 100644 --- a/lns.go +++ b/lns.go @@ -123,6 +123,10 @@ var ( } = (*Service)(nil) ) +func okResult() core.Result { + return core.Result{OK: true} +} + // serviceOptions holds configuration for the LNS service. type serviceOptions struct{} @@ -510,13 +514,13 @@ func (s *Service) GetDecodeResource(raw []byte) (*Resource, error) { // OnStartup satisfies core.Startable so LNS participates in Core lifecycle // discovery without requiring any explicit setup. func (s *Service) OnStartup(context.Context) core.Result { - return core.Result{OK: true} + return okResult() } // OnShutdown satisfies core.Stoppable so LNS participates in Core lifecycle // discovery without requiring any explicit teardown. func (s *Service) OnShutdown(context.Context) core.Result { - return core.Result{OK: true} + return okResult() } // HandleIPCEvents satisfies Core's IPC handler discovery. @@ -524,7 +528,7 @@ func (s *Service) OnShutdown(context.Context) core.Result { // core.New(core.WithService(lns.Register)) // // LNS registers a no-op IPC handler so it can join the bus cleanly. func (s *Service) HandleIPCEvents(*core.Core, core.Message) core.Result { - return core.Result{OK: true} + return okResult() } func canonicalizeName(name any) (string, bool) { diff --git a/lns_test.go b/lns_test.go index aa56000..1431bc8 100644 --- a/lns_test.go +++ b/lns_test.go @@ -25,6 +25,16 @@ func TestServiceLifecycleHooks(t *testing.T) { if got := svc.OnShutdown(nil); !got.OK { t.Fatalf("OnShutdown returned %#v, want OK", got) } + + var nilSvc *Service + + if got := nilSvc.OnStartup(nil); !got.OK { + t.Fatalf("nil OnStartup returned %#v, want OK", got) + } + + if got := nilSvc.OnShutdown(nil); !got.OK { + t.Fatalf("nil OnShutdown returned %#v, want OK", got) + } } func TestServiceHandleIPCEvents(t *testing.T) { @@ -33,6 +43,12 @@ func TestServiceHandleIPCEvents(t *testing.T) { if got := svc.HandleIPCEvents(nil, nil); !got.OK { t.Fatalf("HandleIPCEvents returned %#v, want OK", got) } + + var nilSvc *Service + + if got := nilSvc.HandleIPCEvents(nil, nil); !got.OK { + t.Fatalf("nil HandleIPCEvents returned %#v, want OK", got) + } } func TestServiceResolve(t *testing.T) {