Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(usbdevice): close closed channel exception #86

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading