Skip to content

Restarting the controller after devices are registered is throwing a panic

Restarting the controller after devices have been registered results in a crash while initializing the controller.

Description

Within the initialization process of the controller the SubToDevices method of the DeviceWatcher is called (see here). During this procedure the GetAll method of the DeviceService is called (see here). This method is also the reason for the panic, since the changes are mode on a copy and therefore the ConvertFunction stays nil.

A quick demonstration of the problem: https://go.dev/play/p/Pmh8-Xbl_jI

A related issue: #233 (closed)

Expected Behavior

The GetAll method of the DeviceService shouldn't result in a panic.

Actual Behavior

A panic is thrown because the ConvertFunction is always nil

Possible Fix

One possible solution could be:

// GetAll returns all stored devices.
func (s *DeviceService) GetAll() ([]device.LoadedDevice, error) {
	loadedDevices, err := s.deviceStore.GetAll()
	if err != nil {
		return nil, err
	}

	for i, _ := range loadedDevices {
		loadedDevices[i].SetConvertFunction(s.createDeviceFromStore)
	}

	return loadedDevices, nil
}

But it might be better to just switch to []*device.LoadedDevice.

Steps to Reproduce

  1. Start controller
  2. Add device
  3. Restart

Context

Your Environment

  • Version used:
  • Environment name and version (e.g. go v1.16.3 on FreeBSD 13.0-current):
  • Server type and version:
  • Operating System and version:
Edited by Malte Bauch