-
-
Notifications
You must be signed in to change notification settings - Fork 185
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
I think I've identified a race condition #25
Comments
Thanks for identifying this. I'm going to have to try to reproduce it myself. I'd rather not use |
I'm unable to reproduce this with a RFDuino, which uses an FTDI chip for its USB interface. printf() debugging does indicate that one of the |
Keep trying! The deadlock behavior I observed was hard to reproduce, but hasn't occurred since my fix. Also, I had multiple USB devices connected. Perhaps that's a differentiator. |
I was finally able to reproduce this today using a Prolific USB to serial adapter. I'm still not able to reproduce it with a Keyspan or FTDI adapter, so I think it may be driver dependent. |
OK, I've synchronized calls to |
Hey, were you testing with FTDI's drivers or the one built into Mac OS X? I am running with FTDI's drivers (FTDIUSBSerialDriver.kext, 2.2.18) because Apple's built-in driver doesn't allow programmatic toggling of the DTR line. It's with the FTDI driver that I had the deadlocks. I don't think I ever deadlocked using Apple's driver, but then again, I didn't use it that much. |
Good point. I was testing with the built in drivers. I'll have to try with the FTDI drivers (just to confirm). Weird and annoying that Apple's driver doesn't allow toggling the DTR line. I didn't know that, and it's useful information. |
I've been having trouble when disconnecting an FTDI based USB device. Occasionally, unplugging the device messes up my USB bus and 'disconnects' Xcode's debugger from my code (I can neither stop or pause my code), making this a particularly nasty problem to debug. Also, I usually have audio from iTunes playing through a USB interface and that stops as well. The only thing that clears this up is restarting the machine, which I sometimes have to do via Terminal.
I've tracked this down to what I believe is a race condition.
When a device is unplugged, eventually -[ORSSerialPort close] is called on the main thread. Inside -[ORSSerialPort close] are calls to tcsetattr().
Meanwhile in another thread, a while loop executing in the read poller calls select() on each iteration.
Using printf() debugging, I have come to believe that if select() is called while a call to tcsetattr() is in flight (or vice-versa), some sort of deadlock occurs.
My fix for this is to wrap the critical parts of -[ORSSerialPort close] in a @synchronized block
Similarly, I wrap the read poller's call to select() in a @synchronized block:
I've not had a USB lock up when disconnecting my device since making this change.
The text was updated successfully, but these errors were encountered: