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

Commit 02d802e7 authored by Michael Plass's avatar Michael Plass Committed by Kamaljeet Maini
Browse files

DO NOT MERGE ANYWHERE: [AsyncChannel] Fix race in handling of sync result

Bug: 62866191
Bug: 63074860
Bug: 65267749
Test: wifi unit tests
Change-Id: I1d59eb8d599de9d9041e0b9b7d731363675a40c9
(cherry picked from commit 56e46134)
(cherry picked from commit 9c1d5657)
parent 1c0dba50
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -768,9 +768,10 @@ public class AsyncChannel {
            /** Handle of the reply message */
            @Override
            public void handleMessage(Message msg) {
                mResultMsg = Message.obtain();
                mResultMsg.copyFrom(msg);
                Message msgCopy = Message.obtain();
                msgCopy.copyFrom(msg);
                synchronized(mLockObject) {
                    mResultMsg = msgCopy;
                    mLockObject.notify();
                }
            }
@@ -812,22 +813,26 @@ public class AsyncChannel {
         */
        private static Message sendMessageSynchronously(Messenger dstMessenger, Message msg) {
            SyncMessenger sm = SyncMessenger.obtain();
            Message resultMsg = null;
            try {
                if (dstMessenger != null && msg != null) {
                    msg.replyTo = sm.mMessenger;
                    synchronized (sm.mHandler.mLockObject) {
                        if (sm.mHandler.mResultMsg != null) {
                            Slog.wtf(TAG, "mResultMsg should be null here");
                            sm.mHandler.mResultMsg = null;
                        }
                        dstMessenger.send(msg);
                        sm.mHandler.mLockObject.wait();
                    }
                } else {
                        resultMsg = sm.mHandler.mResultMsg;
                        sm.mHandler.mResultMsg = null;
                    }
                }
            } catch (InterruptedException e) {
                sm.mHandler.mResultMsg = null;
                Slog.e(TAG, "error in sendMessageSynchronously", e);
            } catch (RemoteException e) {
                sm.mHandler.mResultMsg = null;
                Slog.e(TAG, "error in sendMessageSynchronously", e);
            }
            Message resultMsg = sm.mHandler.mResultMsg;
            sm.recycle();
            return resultMsg;
        }