Check the isInUse with the lock held in MessageQueue

Fix a potential race condtion when the same message object
is being enqueued by two threads.

Bug: 150263007
Bug: 154867444
Test: atest android.os.cts.MessageQueueTest
Change-Id: Id79448ba8719479cbdb054f2cdd172fdcbe86d47
(cherry picked from commit 96a6fec4d6)
This commit is contained in:
Jing Ji
2020-03-04 10:31:53 -08:00
parent 511c3aa955
commit baf2d6448e

View File

@@ -550,11 +550,12 @@ public final class MessageQueue {
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
synchronized (this) {
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");