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

Commit 47da9051 authored by Yorke Lee's avatar Yorke Lee
Browse files

Fix possible deadlock in BluetoothPhoneService

If MainThreadRequest.setResult is called with null, this will
cause a deadlock.

Bug: 18460910

Change-Id: I787511ea55a69b2f2b9b1dc293cc6fbab3dd8b5b
parent f17a31c4
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.telecom.CallState;
@@ -53,7 +54,8 @@ public final class BluetoothPhoneService extends Service {
     * Request object for performing synchronous requests to the main thread.
     */
    private static class MainThreadRequest {
        Object result;
        private static final Object RESULT_NOT_SET = new Object();
        Object result = RESULT_NOT_SET;
        int param;

        MainThreadRequest(int param) {
@@ -539,14 +541,19 @@ public final class BluetoothPhoneService extends Service {
    }

    private <T> T sendSynchronousRequest(int message, int param) {
        if (Looper.myLooper() == mHandler.getLooper()) {
            Log.w(TAG, "This method will deadlock if called from the main thread.");
        }

        MainThreadRequest request = new MainThreadRequest(param);
        mHandler.obtainMessage(message, request).sendToTarget();
        synchronized (request) {
            while (request.result == null) {
            while (request.result == MainThreadRequest.RESULT_NOT_SET) {
                try {
                    request.wait();
                } catch (InterruptedException e) {
                    // Do nothing, go back and wait until the request is complete.
                    Log.e(TAG, e, "InterruptedException");
                }
            }
        }