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 56e46134d3)
(cherry picked from commit 9c1d56576e)
This commit is contained in:
Michael Plass
2017-07-13 10:09:07 -07:00
committed by Kamaljeet Maini
parent 1c0dba5014
commit 02d802e71e

View File

@@ -768,9 +768,10 @@ public class AsyncChannel {
/** Handle of the reply message */ /** Handle of the reply message */
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
mResultMsg = Message.obtain(); Message msgCopy = Message.obtain();
mResultMsg.copyFrom(msg); msgCopy.copyFrom(msg);
synchronized(mLockObject) { synchronized(mLockObject) {
mResultMsg = msgCopy;
mLockObject.notify(); mLockObject.notify();
} }
} }
@@ -812,22 +813,26 @@ public class AsyncChannel {
*/ */
private static Message sendMessageSynchronously(Messenger dstMessenger, Message msg) { private static Message sendMessageSynchronously(Messenger dstMessenger, Message msg) {
SyncMessenger sm = SyncMessenger.obtain(); SyncMessenger sm = SyncMessenger.obtain();
Message resultMsg = null;
try { try {
if (dstMessenger != null && msg != null) { if (dstMessenger != null && msg != null) {
msg.replyTo = sm.mMessenger; msg.replyTo = sm.mMessenger;
synchronized (sm.mHandler.mLockObject) { synchronized (sm.mHandler.mLockObject) {
if (sm.mHandler.mResultMsg != null) {
Slog.wtf(TAG, "mResultMsg should be null here");
sm.mHandler.mResultMsg = null;
}
dstMessenger.send(msg); dstMessenger.send(msg);
sm.mHandler.mLockObject.wait(); sm.mHandler.mLockObject.wait();
resultMsg = sm.mHandler.mResultMsg;
sm.mHandler.mResultMsg = null;
} }
} else {
sm.mHandler.mResultMsg = null;
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
sm.mHandler.mResultMsg = null; Slog.e(TAG, "error in sendMessageSynchronously", e);
} catch (RemoteException e) { } catch (RemoteException e) {
sm.mHandler.mResultMsg = null; Slog.e(TAG, "error in sendMessageSynchronously", e);
} }
Message resultMsg = sm.mHandler.mResultMsg;
sm.recycle(); sm.recycle();
return resultMsg; return resultMsg;
} }