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

This commit is contained in:
Ivan Krasin
2013-08-14 00:34:00 -07:00
parent e5e703175c
commit f0ae807096

View File

@@ -12,7 +12,7 @@ import (
type Context struct { type Context struct {
ctx *C.libusb_context ctx *C.libusb_context
done chan struct{} done chan bool
} }
func (c *Context) Debug(level int) { func (c *Context) Debug(level int) {
@@ -21,7 +21,7 @@ func (c *Context) Debug(level int) {
func NewContext() *Context { func NewContext() *Context {
c := &Context{ c := &Context{
done: make(chan struct{}), done: make(chan bool),
} }
if errno := C.libusb_init(&c.ctx); errno != 0 { if errno := C.libusb_init(&c.ctx); errno != 0 {
@@ -29,13 +29,14 @@ func NewContext() *Context {
} }
go func() { go func() {
tv := C.struct_timeval{0, 100000}
for { for {
select { select {
case <-c.done: case <-c.done:
return return
default: 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)) log.Printf("handle_events: error: %s", usbError(errno))
continue continue
} }
@@ -88,6 +89,7 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
} }
func (c *Context) Close() error { func (c *Context) Close() error {
c.done <- true
close(c.done) close(c.done)
if c.ctx != nil { if c.ctx != nil {
C.libusb_exit(c.ctx) C.libusb_exit(c.ctx)