fix: shutdown context, double IPC registration

- Run() uses context.Background() for shutdown (c.context is cancelled)
- Stoppable closure uses context.Background() for OnShutdown
- WithService delegates HandleIPCEvents to RegisterService only

Fixes Codex review findings 1, 2, 3.

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-24 22:28:15 +00:00
parent f72c5782fd
commit 95076be4b3
3 changed files with 5 additions and 11 deletions

View file

@ -169,15 +169,7 @@ func WithService(factory func(*Core) Result) CoreOption {
return Result{E("core.WithService", Sprintf("service name could not be discovered for type %T", instance), nil), false}
}
// IPC handler discovery
instanceValue := reflect.ValueOf(instance)
handlerMethod := instanceValue.MethodByName("HandleIPCEvents")
if handlerMethod.IsValid() {
if handler, ok := handlerMethod.Interface().(func(*Core, Message) Result); ok {
c.RegisterAction(handler)
}
}
// RegisterService handles Startable/Stoppable/HandleIPCEvents discovery
return c.RegisterService(name, instance)
}
}

View file

@ -80,7 +80,7 @@ func (c *Core) Run() {
r = cli.Run()
}
c.ServiceShutdown(c.context)
c.ServiceShutdown(context.Background())
if !r.OK {
if err, ok := r.Value.(error); ok {

View file

@ -17,6 +17,8 @@
package core
import "context"
// Service is a managed component with optional lifecycle.
type Service struct {
Name string
@ -109,7 +111,7 @@ func (c *Core) RegisterService(name string, instance any) Result {
}
if s, ok := instance.(Stoppable); ok {
srv.OnStop = func() Result {
if err := s.OnShutdown(c.context); err != nil {
if err := s.OnShutdown(context.Background()); err != nil {
return Result{err, false}
}
return Result{OK: true}