OSDN Git Service

Make sure task_runner is valid when we call PostTask on it
authorJakub Pawlowski <jpawlowski@google.com>
Wed, 7 Feb 2018 02:50:10 +0000 (18:50 -0800)
committerJakub Pawlowski <jpawlowski@google.com>
Wed, 7 Feb 2018 03:05:10 +0000 (19:05 -0800)
Test: runtest -j32 bluetooth -c com.android.bluetooth.btservice.ProfileServiceTest
Change-Id: I89426f7eb1204af9def0fa927dfeef1d5aec689f

bta/sys/bta_sys_main.cc
btif/src/btif_core.cc

index 8d155db..a196c79 100644 (file)
@@ -553,13 +553,19 @@ void bta_sys_sendmsg(void* p_msg) {
 void do_in_bta_thread(const tracked_objects::Location& from_here,
                       const base::Closure& task) {
   base::MessageLoop* bta_message_loop = get_message_loop();
-
-  if (!bta_message_loop || !bta_message_loop->task_runner().get()) {
+  if (!bta_message_loop) {
     APPL_TRACE_ERROR("%s: MessageLooper not initialized", __func__);
     return;
   }
 
-  bta_message_loop->task_runner()->PostTask(from_here, task);
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner =
+      bta_message_loop->task_runner();
+  if (!task_runner.get()) {
+    APPL_TRACE_ERROR("%s: task runner is dead", __func__);
+    return;
+  }
+
+  task_runner->PostTask(from_here, task);
 }
 
 /*******************************************************************************
index 8250c2a..e3508ad 100644 (file)
@@ -225,14 +225,20 @@ bt_status_t btif_transfer_context(tBTIF_CBACK* p_cback, uint16_t event,
  **/
 bt_status_t do_in_jni_thread(const tracked_objects::Location& from_here,
                              const base::Closure& task) {
-  if (!message_loop_ || !message_loop_->task_runner().get()) {
+  if (!message_loop_) {
     BTIF_TRACE_WARNING("%s: Dropped message, message_loop not initialized yet!",
                        __func__);
     return BT_STATUS_FAIL;
   }
 
-  if (message_loop_->task_runner()->PostTask(from_here, task))
-    return BT_STATUS_SUCCESS;
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner =
+      message_loop_->task_runner();
+  if (!task_runner.get()) {
+    BTIF_TRACE_WARNING("%s: task runner is dead", __func__);
+    return BT_STATUS_FAIL;
+  }
+
+  if (task_runner->PostTask(from_here, task)) return BT_STATUS_SUCCESS;
 
   BTIF_TRACE_ERROR("%s: Post task to task runner failed!", __func__);
   return BT_STATUS_FAIL;