Skip to content

Commit

Permalink
fix(usbdevice): close closed channel exception
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <jack.yu@suse.com>
  • Loading branch information
Yu-Jack committed Aug 19, 2024
1 parent eb6178b commit a20076c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/deviceplugins/usb_device_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type USBDevicePlugin struct {
update chan struct{}
deregistered chan struct{}
server *grpc.Server
serverDone chan struct{}
resourceName string
device *PluginDevice
logger *log.FilteredLogger
Expand Down Expand Up @@ -101,6 +102,10 @@ func (plugin *USBDevicePlugin) DeviceName() string {
}

func (plugin *USBDevicePlugin) stopDevicePlugin() error {
defer func() {
close(plugin.serverDone)
}()

// Give the device plugin one second to properly deregister
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
Expand All @@ -115,6 +120,7 @@ func (plugin *USBDevicePlugin) stopDevicePlugin() error {

func (plugin *USBDevicePlugin) startDevicePlugin() error {
plugin.deregistered = make(chan struct{})
plugin.serverDone = make(chan struct{})

err := plugin.cleanup()
if err != nil {
Expand Down Expand Up @@ -294,6 +300,7 @@ func (plugin *USBDevicePlugin) ListAndWatch(_ *pluginapi.Empty, lws pluginapi.De
if err := sendUpdate(plugin.devicesToKubeVirtDevicePlugin()); err != nil {
return err
}

done := false
for !done {
select {
Expand All @@ -303,14 +310,18 @@ func (plugin *USBDevicePlugin) ListAndWatch(_ *pluginapi.Empty, lws pluginapi.De
}
case <-plugin.stop:
done = true
case <-plugin.serverDone:
done = true
}
}

if err := sendUpdate([]*pluginapi.Device{}); err != nil {
plugin.logger.Reason(err).Warningf("Failed to deregister device plugin %s",
plugin.resourceName)
}

close(plugin.deregistered)

return nil
}

Expand Down

0 comments on commit a20076c

Please sign in to comment.