Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(591)

Unified Diff: base/synchronization/waitable_event_watcher_unittest.cc

Issue 2839213002: [Synchronization] Fix a crash in WaitableEventWatcher (Closed)
Patch Set: [Synchronization] Fix a crash in WaitableEventWatcher. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/synchronization/waitable_event_watcher_unittest.cc
diff --git a/base/synchronization/waitable_event_watcher_unittest.cc b/base/synchronization/waitable_event_watcher_unittest.cc
index 9df72892c3c3813adf8d1286c7bdf6dfefe48930..d99a75699a2d51c12937f093e27da517a70939fb 100644
--- a/base/synchronization/waitable_event_watcher_unittest.cc
+++ b/base/synchronization/waitable_event_watcher_unittest.cc
@@ -38,6 +38,7 @@ class DecrementCountContainer {
explicit DecrementCountContainer(int* counter) : counter_(counter) {
}
void OnWaitableEventSignaled(WaitableEvent* object) {
+ // NOTE: |object| may be already deleted.
--(*counter_);
}
private:
@@ -117,7 +118,7 @@ void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) {
}
}
-void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) {
+void RunTest_DeleteUnder(MessageLoop::Type message_loop_type, bool with_delay) {
// Delete the WaitableEvent out from under the Watcher. This is explictly
// allowed by the interface.
@@ -126,12 +127,35 @@ void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) {
{
WaitableEventWatcher watcher;
- WaitableEvent* event =
- new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC,
- WaitableEvent::InitialState::NOT_SIGNALED);
+ auto event = base::MakeUnique<WaitableEvent>(
danakj 2017/04/27 20:01:08 needs ptr_util.h
atuchin 2017/04/28 06:44:29 Done.
+ WaitableEvent::ResetPolicy::AUTOMATIC,
+ WaitableEvent::InitialState::NOT_SIGNALED);
- watcher.StartWatching(event, BindOnce(&QuitWhenSignaled));
- delete event;
+ watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled));
+ event.reset();
+ if (with_delay)
+ PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
danakj 2017/04/27 20:01:08 What is this sleep meant to do?
atuchin 2017/04/28 06:44:29 Unfortunately, without that sleep the test fails t
danakj 2017/04/28 17:54:49 IIUC we don't have the ability to run code on this
atuchin 2017/05/02 07:41:19 Yes, we don't have a such ability. But I have foun
+ }
+}
+
+void RunTest_SignalAndDelete(MessageLoop::Type message_loop_type,
+ bool with_delay) {
+ // Signal and immediately delete the WaitableEvent out from under the Watcher.
+
+ MessageLoop message_loop(message_loop_type);
+
+ {
+ WaitableEventWatcher watcher;
+
+ auto event = base::MakeUnique<WaitableEvent>(
+ WaitableEvent::ResetPolicy::AUTOMATIC,
+ WaitableEvent::InitialState::NOT_SIGNALED);
+
+ watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled));
+ event->Signal();
+ event.reset();
+ if (with_delay)
+ PlatformThread::Sleep(TimeDelta::FromMilliseconds(10));
danakj 2017/04/27 20:01:08 Also this sleep?
atuchin 2017/04/28 06:44:29 See the answer in another one.
}
}
@@ -165,7 +189,15 @@ TEST(WaitableEventWatcherTest, OutlivesMessageLoop) {
TEST(WaitableEventWatcherTest, DeleteUnder) {
for (int i = 0; i < kNumTestingMessageLoops; i++) {
- RunTest_DeleteUnder(testing_message_loops[i]);
+ RunTest_DeleteUnder(testing_message_loops[i], false);
+ RunTest_DeleteUnder(testing_message_loops[i], true);
+ }
+}
+
+TEST(WaitableEventWatcherTest, SignalAndDelete) {
+ for (int i = 0; i < kNumTestingMessageLoops; i++) {
+ RunTest_SignalAndDelete(testing_message_loops[i], false);
+ RunTest_SignalAndDelete(testing_message_loops[i], true);
}
}

Powered by Google App Engine
This is Rietveld 408576698