openxc / openxc-android

Android library for accessing vehicle data from an OpenXC vehicle interface
BSD 3-Clause "New" or "Revised" License
236 stars 115 forks source link

Bluetooth can still deadlock #76

Closed peplin closed 11 years ago

peplin commented 11 years ago

Continuing this never ending saga, today I disabled the BT interface but noticed it was still trying to connect in the background. This thread was stuck:

ReentrantLock$NonfairSync(AbstractQueuedSynchronizer).parkAndCheckInterrupt() line: 810 
ReentrantLock$NonfairSync(AbstractQueuedSynchronizer).acquireQueued(AbstractQueuedSynchronizer$Node, int) line: 843 
ReentrantLock$NonfairSync(AbstractQueuedSynchronizer).acquire(int) line: 1173   
ReentrantLock$NonfairSync.lock() line: 183  
ReentrantLock.lock() line: 259  
BluetoothVehicleInterface(BytestreamDataSource).lockConnection() line: 118  
BluetoothVehicleInterface.isConnected() line: 76    
DataPipeline.sourceDisconnected(VehicleDataSource) line: 199    
NetworkVehicleInterface(BaseVehicleDataSource).disconnected() line: 53  
NetworkVehicleInterface.disconnect() line: 148  
NetworkVehicleInterface.waitForConnection() line: 161   
NetworkVehicleInterface(BytestreamDataSource).run() line: 56    
Thread.run() line: 841  

as was this one:

BluetoothVehicleInterface(BytestreamDataSource).lockConnection() line: 118  
BluetoothVehicleInterface.isConnected() line: 76    
DataPipeline.sourceDisconnected(VehicleDataSource) line: 199    
NetworkVehicleInterface(BaseVehicleDataSource).disconnected() line: 53  
NetworkVehicleInterface.disconnect() line: 148  
NetworkVehicleInterface(BytestreamDataSource).stop() line: 46   
NetworkVehicleInterface.stop() line: 89 
VehicleService.removeVehicleInterface(VehicleInterface) line: 256   
VehicleService.removeVehicleInterface(String) line: 251 
VehicleService.access$500(VehicleService, String) line: 53  
VehicleService$1.removeVehicleInterface(String) line: 182   
VehicleService$1(VehicleServiceInterface$Stub).onTransact(int, Parcel, Parcel, int) line: 143   
VehicleService$1(Binder).execTransact(int, int, int, int) line: 388 
NativeStart.run() line: not available [native method]   

there were 2 blocked threads for the NetworkDataSource, too, so this is probably directly related to #75.

peplin commented 11 years ago

I refactored all of the BytestreamDataSource implementations so it's easier to follow and reason to be thread safe. Part of the problem was that if you switched the MAC address, we took a really naive approach to change the resource - we changed it in the preferences, stopped and started the source. Of course not everything was restarted properly when we did this. I changed it so that we keep the thread running, but break the socket so the main data source thread will attempt to reconnect to the latest URI.