Cancel context of broken devices to release resources (#28423)

This commit is contained in:
andig 2026-03-21 15:26:13 +01:00 • committed by GitHub
parent 5220bbfa0b
commit d96b045117
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -236,23 +236,28 @@ NEXT:
type newFromConfFunc[T any] func(context.Context, string, map[string]any) (T, error)
func staticInstance[T any](typ string, cc config.Named, newFromConf newFromConfFunc[T], h config.Handler[T]) error {
ctx := util.WithLogger(context.TODO(), util.NewLogger(cc.Name))
ctx, cancel := context.WithCancel(util.WithLogger(context.TODO(), util.NewLogger(cc.Name))) //nolint:govet
instance, err := newFromConf(ctx, cc.Type, cc.Other)
if err != nil {
return &DeviceError{cc.Name, fmt.Errorf("cannot create %s '%s': %w", typ, cc.Name, err)}
err = &DeviceError{cc.Name, fmt.Errorf("cannot create %s '%s': %w", typ, cc.Name, err)}
}
if err := h.Add(config.NewStaticDevice(cc, instance)); err != nil {
return &DeviceError{cc.Name, err}
if e := h.Add(config.NewStaticDevice(cc, instance)); e != nil && err == nil {
err = &DeviceError{cc.Name, e}
}
return nil
// release resources
if err != nil {
cancel()
}
return err //nolint:govet
}
func configurableInstance[T any](typ string, conf *config.Config, newFromConf newFromConfFunc[T], h config.Handler[T]) error {
cc := conf.Named()
ctx := util.WithLogger(context.TODO(), util.NewLogger(cc.Name))
ctx, cancel := context.WithCancel(util.WithLogger(context.TODO(), util.NewLogger(cc.Name))) //nolint:govet
props, err := customDevice(cc.Other)
if err != nil {
@ -271,7 +276,12 @@ func configurableInstance[T any](typ string, conf *config.Config, newFromConf ne
err = &DeviceError{cc.Name, e}
}
return err
// release resources
if err != nil {
cancel()
}
return err //nolint:govet
}
func configureMeters(static []config.Named, names ...string) error {