From 17d0a01b8f4c294accba9faac3c1bdbf02166630 Mon Sep 17 00:00:00 2001 From: Roger Wang Date: Thu, 22 Oct 2020 08:03:12 -0600 Subject: [PATCH] Fix race condition in renderer message loop Ref nwjs/nw.js#7497 Ref nwjs/nw.js#7590 --- base/message_loop/message_pump_uv.cc | 9 +++++++++ base/message_loop/message_pump_uv.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/base/message_loop/message_pump_uv.cc b/base/message_loop/message_pump_uv.cc index 708fa0e17b50a..864bfebc41eb8 100644 --- a/base/message_loop/message_pump_uv.cc +++ b/base/message_loop/message_pump_uv.cc @@ -111,6 +111,13 @@ void MessagePumpUV::Run(Delegate* delegate) { continue; } + if (event_.IsSignaled()) { + //there is a chance that wake_event_ got processed in uv loop + //run above. then the next wait is stalled. + event_.Reset(); + g_msg_pump_did_work_fn(&ctx); + continue; + } if (next_work_info.delayed_run_time.is_max()) { // (*node::g_nw_uv_run)(loop, UV_RUN_ONCE); g_msg_pump_need_work_fn(&ctx); @@ -123,6 +130,7 @@ void MessagePumpUV::Run(Delegate* delegate) { // uv_timer_stop(&delay_timer); g_msg_pump_delay_work_fn(&ctx, delay.InMilliseconds()); } + event_.Reset(); // Since event_ is auto-reset, we don't need to do anything special here // other than service each delegate method. } @@ -161,6 +169,7 @@ void MessagePumpUV::ScheduleWork() { // #else // uv_async_send(wakeup_event_); // #endif + event_.Signal(); g_msg_pump_sched_work_fn(wakeup_event_); } diff --git a/base/message_loop/message_pump_uv.h b/base/message_loop/message_pump_uv.h index a464e2a9963b7..5e7ddd920167f 100644 --- a/base/message_loop/message_pump_uv.h +++ b/base/message_loop/message_pump_uv.h @@ -9,6 +9,7 @@ #include "base/message_loop/message_pump.h" #include "base/time/time.h" #include "content/common/content_export.h" +#include "base/synchronization/waitable_event.h" #include @@ -39,6 +40,7 @@ class BASE_EXPORT MessagePumpUV : public MessagePump { // Handle to wake up loop. std::vector wakeup_events_; void* wakeup_event_; + WaitableEvent event_; TimeTicks delayed_work_time_;