From f0ae80709655e8c51055d2d5bacd1d4547b349e3 Mon Sep 17 00:00:00 2001 From: Ivan Krasin Date: Wed, 14 Aug 2013 00:34:00 -0700 Subject: [PATCH] Fix race condition with C.libusb_handle_events that might still being called while libusb_exit is executed. Use C.libusb_handle_events_timeout_completed which accepts timeouts and pass 100 ms timeout. This is the recommended way to deal with this issue, see http://libusb.sourceforge.net/api-1.0/group__poll.html#ga43e52b912a760b41a0cf8a4a472fbd5b and http://libusb.sourceforge.net/api-1.0/group__poll.html#ga4989086e3f0327f3886a4c474ec7c327 --- usb/usb.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/usb/usb.go b/usb/usb.go index 0681d06..48a54bf 100644 --- a/usb/usb.go +++ b/usb/usb.go @@ -12,7 +12,7 @@ import ( type Context struct { ctx *C.libusb_context - done chan struct{} + done chan bool } func (c *Context) Debug(level int) { @@ -21,7 +21,7 @@ func (c *Context) Debug(level int) { func NewContext() *Context { c := &Context{ - done: make(chan struct{}), + done: make(chan bool), } if errno := C.libusb_init(&c.ctx); errno != 0 { @@ -29,13 +29,14 @@ func NewContext() *Context { } go func() { + tv := C.struct_timeval{0, 100000} for { select { case <-c.done: return default: } - if errno := C.libusb_handle_events(c.ctx); errno < 0 { + if errno := C.libusb_handle_events_timeout_completed(c.ctx, &tv, nil); errno < 0 { log.Printf("handle_events: error: %s", usbError(errno)) continue } @@ -88,6 +89,7 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro } func (c *Context) Close() error { + c.done <- true close(c.done) if c.ctx != nil { C.libusb_exit(c.ctx)