Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit adc9ed0f authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

Fix concurrency issues on state machine shutdown

We should wait for the state machine thread to finish any message
handling as it can still call into the main service. In some cases
the race condition manifested itself when the state machine was
calling okToConnect() on the service when mAdapterService was
already set to null by the main service's stop() call.

Bug: 150670922
Tag: #feature
Test: atest BluetoothInstrumentationTests
Sponsor: jpawlowski@
Change-Id: I75278ccc904881fe20ca7a9451d88ba8720c2740
parent 96a7ffd2
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -216,8 +216,13 @@ public class A2dpService extends ProfileService {
        }

        if (mStateMachinesThread != null) {
            try {
                mStateMachinesThread.quitSafely();
                mStateMachinesThread.join();
                mStateMachinesThread = null;
            } catch (InterruptedException e) {
                // Do not rethrow as we are shutting down anyway
            }
        }
        // Step 2: Reset maximum number of connected audio devices
        mMaxConnectedAudioDevices = 1;
+7 −2
Original line number Diff line number Diff line
@@ -182,8 +182,13 @@ public class CsipSetCoordinatorService extends ProfileService {
        }

        if (mStateMachinesThread != null) {
            try {
                mStateMachinesThread.quitSafely();
                mStateMachinesThread.join();
                mStateMachinesThread = null;
            } catch (InterruptedException e) {
                // Do not rethrow as we are shutting down anyway
            }
        }

        mDeviceGroupIdMap.clear();
+7 −2
Original line number Diff line number Diff line
@@ -208,8 +208,13 @@ public class HapClientService extends ProfileService {
        mPresetsMap.clear();

        if (mStateMachinesThread != null) {
            try {
                mStateMachinesThread.quitSafely();
                mStateMachinesThread.join();
                mStateMachinesThread = null;
            } catch (InterruptedException e) {
                // Do not rethrow as we are shutting down anyway
            }
        }

        // Clear AdapterService
+7 −2
Original line number Diff line number Diff line
@@ -189,8 +189,13 @@ public class HearingAidService extends ProfileService {
        mHiSyncIdConnectedMap.clear();

        if (mStateMachinesThread != null) {
            try {
                mStateMachinesThread.quitSafely();
                mStateMachinesThread.join();
                mStateMachinesThread = null;
            } catch (InterruptedException e) {
                // Do not rethrow as we are shutting down anyway
            }
        }

        // Clear AdapterService, HearingAidNativeInterface
+8 −2
Original line number Diff line number Diff line
@@ -236,8 +236,14 @@ public class HeadsetService extends ProfileService {
        // Step 3: Destroy system interface
        mSystemInterface.stop();
        // Step 2: Stop handler thread
        try {
            mStateMachinesThread.quitSafely();
            mStateMachinesThread.join();
            mStateMachinesThread = null;
        } catch (InterruptedException e) {
            // Do not rethrow as we are shutting down anyway
        }

        mStateMachinesThreadHandler = null;
        // Step 1: Clear
        synchronized (mStateMachines) {
Loading