Merge "[AsyncChannel] Fix race in handling of sync result"

This commit is contained in:
TreeHugger Robot
2017-07-18 19:51:58 +00:00
committed by Android (Google) Code Review

View File

@@ -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();
resultMsg = sm.mHandler.mResultMsg;
sm.mHandler.mResultMsg = null;
}
} else {
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;
}