Hi
   The android implementation of EnableThread can cause a race
condition during repeated BT On/Off. Following error is seen
==> "Re-entrant call to dbus_connection_set_watch_functions is not
allowed"

Problem:

EnableThread.run() [-> called when bluetooth is turned on], performs
the following

1) calls enable native to power on the chip (blocking)
2) starts the eventLoop
3) waits till eventLoop is running
4) loadBond state

Within startEventLoopNative, the mutex lock ensures that poll data and
eventLoop is setup before context switching can happen.

The check for isEventLoopRunningNative will return a true always,
since the nat->pollData is already created at this point of time

It will proceed to the next step to make the native call to BT stack
(bluez) through dbus (mBondState.loadBondState fn causes this)

So far it looks good, but we have a problem in startEventLoopNative
where it spawns a new thread eventLoopMain, which calls the
dbus_connection_set_watch_functions. The execution of this thread is
not guaranteed when we are sending the listBondings native.

So there would be a race condition under the following scenario

listBondings() calls protected_change_watch()
{
...
connection->watches = NULL
CONNECTION_UNLOCK (connection); ------> at this point the
eventLoopMain thread is scheduled and
 
preemption occurs
..
}

eventLoopMain() calls dbus_connection_set_watch_functions()
{
...
CONNECTION_LOCK (connection);
if (connection->watches == NULL) -------> this check will return true
because of the setting through
 
protected_change_watch() and hence generate a re-entrancy error.
}

Pls let me know if this is a known issue.

Workaround:
Introducing a small 10ms sleep just after isEventLoopRunning returns
true, allows the eventLoopMain thread to execute and minimize the race
(not the best way to resolve !).

Possible Fix:
The isEventLoopRunning should not return true till the eventLoopMain
is executed and watch setting is complete.

regards,
Prabhashankar

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to