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

Side by Side Diff: chrome/browser/process_singleton_win_uitest.cc

Issue 661339: Fixed a startup race condition.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 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 unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This test validates that the ProcessSingleton class properly makes sure
6 // that there is only one main browser process.
7 //
8 // It is currently compiled and ran on the windows platform only but has been
9 // written in a platform independent way (using the process/threads/sync
10 // routines from base). So it does compile fine on Mac and Linux but fails to
11 // launch the app and thus have not been tested for success/failures. Since it
12 // was written to validate a change made to fix a bug only seen on Windows, it
13 // was left as is until it gets to be needed on the other platforms.
14
15
16 #include <list>
17
18 #include "base/file_path.h"
19 #include "base/file_util.h"
20 #include "base/process_util.h"
21 #include "base/ref_counted.h"
22 #include "base/thread.h"
23 #include "base/waitable_event.h"
24 #include "chrome/common/chrome_constants.h"
25 #include "chrome/test/ui/ui_test.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace {
29
30 // This is for the code that is to be ran in multiple threads at once,
31 // to stress a race condition on first process start.
32 // We use the thread safe ref counted base class so that we can use the
33 // NewRunnableMethod class to run the StartChrome methods in many threads.
34 class ChromeStarter : public base::RefCountedThreadSafe<ChromeStarter> {
35 public:
36 ChromeStarter()
37 : ready_event_(false /* manual */, false /* signaled */),
38 done_event_(false /* manual */, false /* signaled */),
39 process_handle_(NULL),
40 process_terminated_(false) {
41 }
42
43 // We must reset some data members since we reuse the same ChromeStarter
44 // object and start/stop it a few times. We must start fresh! :-)
45 void Reset() {
46 ready_event_.Reset();
47 done_event_.Reset();
48 if (process_handle_ != NULL)
49 base::CloseProcessHandle(process_handle_);
50 process_handle_ = NULL;
51 process_terminated_ = false;
52 }
53
54 void StartChrome(base::WaitableEvent* start_event) {
55 // TODO(port): For some reason the LaunchApp call below fails even though
56 // we use the platform independent constant for the executable path.
57 // This is the current blocker for running this test on Mac & Linux.
58 CommandLine command_line(FilePath::FromWStringHack(
59 chrome::kBrowserProcessExecutablePath));
60
61 // Try to get all threads to launch the app at the same time.
62 // So let the test know we are ready.
63 ready_event_.Signal();
64 // And then wait for the test to tell us to GO!
65 ASSERT_NE(static_cast<base::WaitableEvent*>(NULL), start_event);
66 ASSERT_TRUE(start_event->Wait());
67
68 // Here we don't wait for the app to be terminated because one of the
69 // process will stay alive while the others will be restarted. If we would
70 // wait here, we would never get a handle to the main process...
71 base::LaunchApp(command_line, false /* wait */,
72 false /* hidden */, &process_handle_);
73 ASSERT_NE(static_cast<base::ProcessHandle>(NULL), process_handle_);
74
75 // We can wait on the handle here, we should get stuck on one and only
76 // one process. The test below will take care of killing that process
77 // to unstuck us once it confirms there is only one.
78 static const int64 kWaitForProcessDeath = 5000;
79 process_terminated_ = base::WaitForSingleProcess(process_handle_,
80 kWaitForProcessDeath);
81 // Let the test know we are done.
82 done_event_.Signal();
83 }
84
85 // Public access to simplify the test code using them.
86 base::WaitableEvent ready_event_;
87 base::WaitableEvent done_event_;
88 base::ProcessHandle process_handle_;
89 bool process_terminated_;
90
91 private:
92 friend class base::RefCountedThreadSafe<ChromeStarter>;
93 ~ChromeStarter() {
94 if (process_handle_ != NULL)
95 base::CloseProcessHandle(process_handle_);
96 }
97 DISALLOW_COPY_AND_ASSIGN(ChromeStarter);
98 };
99
100 // Our test fixture that initializes and holds onto a few global vars.
101 class ProcessSingletonWinTest : public UITest {
102 public:
103 ProcessSingletonWinTest()
104 // We use a manual reset so that all threads wake up at once when signaled
105 // and thus we must manually reset it for each attempt.
106 : threads_waker_(true /* manual */, false /* signaled */) {
107 }
108
109 void SetUp() {
110 // Start the threads and create the starters.
111 for (size_t i = 0; i < kNbThreads; ++i) {
112 chrome_starter_threads_[i].reset(new base::Thread("ChromeStarter"));
113 ASSERT_TRUE(chrome_starter_threads_[i]->Start());
114 chrome_starters_[i] = new ChromeStarter;
115 }
116 }
117
118 void TearDown() {
119 // Stop the threads.
120 for (size_t i = 0; i < kNbThreads; ++i)
121 chrome_starter_threads_[i]->Stop();
122 }
123
124 // This method is used to make sure we kill the main browser process after
125 // all of its child processes have successfully attached to it. This was added
126 // when we realized that if we just kill the parent process right away, we
127 // sometimes end up with dangling child processes. If we Sleep for a certain
128 // amount of time, we are OK... So we introduced this method to avoid a
129 // flaky wait. Instead, we kill all descendants of the main process after we
130 // killed it, relying on the fact that we can still get the parent id of a
131 // child process, even when the parent dies.
132 void KillProcessTree(base::ProcessHandle process_handle) {
133 class ProcessTreeFilter : public base::ProcessFilter {
134 public:
135 explicit ProcessTreeFilter(base::ProcessId parent_pid) {
136 ancestor_pids_.insert(parent_pid);
137 }
138 virtual bool Includes(base::ProcessId pid,
139 base::ProcessId parent_pid) const {
140 if (ancestor_pids_.find(parent_pid) != ancestor_pids_.end()) {
141 ancestor_pids_.insert(pid);
142 return true;
143 } else {
144 return false;
145 }
146 }
147 private:
148 mutable std::set<base::ProcessId> ancestor_pids_;
149 } process_tree_filter(base::GetProcId(process_handle));
150
151 // Start by explicitly killing the main process we know about...
152 static const int kExitCode = 42;
153 EXPECT_TRUE(base::KillProcess(process_handle, kExitCode, true /* wait */));
154
155 // Then loop until we can't find any of its descendant.
156 // But don't try more than kNbTries times...
157 static const int kNbTries = 10;
158 int num_tries = 0;
159 while (base::GetProcessCount(chrome::kBrowserProcessExecutablePath,
160 &process_tree_filter) > 0 && num_tries++ < kNbTries) {
161 base::KillProcesses(chrome::kBrowserProcessExecutablePath,
162 kExitCode, &process_tree_filter);
163 }
164 }
165
166 // Since this is a hard to reproduce problem, we make a few attempts.
167 // We stop the attempts at the first error, and when there are no errors,
168 // we don't time-out of any wait, so it executes quite fast anyway.
169 static const size_t kNbAttempts = 5;
170
171 // The idea is to start chrome from multiple threads all at once.
172 static const size_t kNbThreads = 5;
173 scoped_refptr<ChromeStarter> chrome_starters_[kNbThreads];
174 scoped_ptr<base::Thread> chrome_starter_threads_[kNbThreads];
175
176 // The event that will get all threads to wake up simultaneously and try
177 // to start a chrome process at the same time.
178 base::WaitableEvent threads_waker_;
179 };
180
181
182 TEST_F(ProcessSingletonWinTest, StartupRaceCondition) {
183 // We use this to stop the attempts loop on the first failure.
184 bool failed = false;
185 for (size_t attempt = 0; attempt < kNbAttempts && !failed; ++attempt) {
186 SCOPED_TRACE(testing::Message() << "Attempt: " << attempt << ".");
187 // We use a single event to get all threads to do the AppLaunch at the same
188 // time...
189 threads_waker_.Reset();
190
191 // Here we prime all the threads with a ChromeStarter that will wait for
192 // our signal to launch its chrome process.
193 for (size_t i = 0; i < kNbThreads; ++i) {
194 ASSERT_NE(static_cast<ChromeStarter*>(NULL), chrome_starters_[i].get());
195 chrome_starters_[i]->Reset();
196
197 ASSERT_TRUE(chrome_starter_threads_[i]->IsRunning());
198 ASSERT_NE(static_cast<MessageLoop*>(NULL),
199 chrome_starter_threads_[i]->message_loop());
200
201 chrome_starter_threads_[i]->message_loop()->PostTask(
202 FROM_HERE, NewRunnableMethod(chrome_starters_[i].get(),
203 &ChromeStarter::StartChrome,
204 &threads_waker_));
205 }
206
207 // Wait for all the starters to be ready.
208 // We could replace this loop if we ever implement a WaitAll().
209 for (size_t i = 0; i < kNbThreads; ++i) {
210 SCOPED_TRACE(testing::Message() << "Waiting on thread: " << i << ".");
211 ASSERT_TRUE(chrome_starters_[i]->ready_event_.Wait());
212 }
213 // GO!
214 threads_waker_.Signal();
215
216 // As we wait for all threads to signal that they are done, we remove their
217 // index from this vector so that we get left with only the index of
218 // the thread that started the main process.
219 std::vector<size_t> pending_starters(kNbThreads);
220 for (size_t i = 0; i < kNbThreads; ++i)
221 pending_starters[i] = i;
222
223 // We use a local array of starter's done events we must wait on...
224 // These are collected from the starters that we have not yet been removed
225 // from the pending_starters vector.
226 base::WaitableEvent* starters_done_events[kNbThreads];
227 // At the end, "There can be only one" main browser process alive.
228 while (pending_starters.size() > 1) {
229 SCOPED_TRACE(testing::Message() << pending_starters.size() <<
230 " starters left.");
231 for (size_t i = 0; i < pending_starters.size(); ++i) {
232 starters_done_events[i] =
233 &chrome_starters_[pending_starters[i]]->done_event_;
234 }
235 size_t done_index = base::WaitableEvent::WaitMany(
236 starters_done_events, pending_starters.size());
237 size_t starter_index = pending_starters[done_index];
238 // If the starter is done but has not marked itself as terminated,
239 // it is because it timed out of its WaitForSingleProcess(). Only the
240 // last one standing should be left waiting... So we failed...
241 EXPECT_TRUE(chrome_starters_[starter_index]->process_terminated_ ||
242 failed) << "There is more than one main process.";
243 if (!chrome_starters_[starter_index]->process_terminated_) {
244 // This will stop the "for kNbAttempts" loop.
245 failed = true;
246 // But we let the last loop turn finish so that we can properly
247 // kill all remaining processes. Starting with this one...
248 if (chrome_starters_[starter_index]->process_handle_ != NULL) {
249 KillProcessTree(chrome_starters_[starter_index]->process_handle_);
250 }
251 }
252 pending_starters.erase(pending_starters.begin() + done_index);
253 }
254
255 // "There can be only one!" :-)
256 ASSERT_EQ(static_cast<size_t>(1), pending_starters.size());
257 size_t last_index = pending_starters.front();
258 pending_starters.empty();
Nico 2012/04/11 03:19:00 Vectors are cleared with "clear()", empty() just r
259 if (chrome_starters_[last_index]->process_handle_ != NULL) {
260 KillProcessTree(chrome_starters_[last_index]->process_handle_);
261 chrome_starters_[last_index]->done_event_.Wait();
262 }
263 }
264 }
265
266 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698