]> khamidou — GitHub mirrors - electron.git/commitdiff
linux: Delay listening to socket until message loop is ready
authorCheng Zhao <zcbenz@gmail.com>
Thu, 22 Oct 2015 07:54:58 +0000 (15:54 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 22 Oct 2015 07:54:58 +0000 (15:54 +0800)
chromium_src/chrome/browser/process_singleton.h
chromium_src/chrome/browser/process_singleton_posix.cc

index 3265c9597a77de23a7bb25fa40a494e5700e3493..05288dc37cfe3fce2d01b9f2f94bf98ad9daa32a 100644 (file)
@@ -133,6 +133,9 @@ class ProcessSingleton : public base::NonThreadSafe {
   base::FilePath user_data_dir_;
   ShouldKillRemoteProcessCallback should_kill_remote_process_callback_;
 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
+  // Start listening to the socket.
+  void StartListening(int sock);
+
   // Return true if the given pid is one of our child processes.
   // Assumes that the current pid is the root of all pids of the current
   // instance.
index 4117dff8863e182b89dcebc577c405106e4d4d74..5a8aac90793ea9bce830d8796bec74b8de9f3dee 100644 (file)
@@ -316,8 +316,7 @@ bool IsChromeProcess(pid_t pid) {
   PathService::Get(base::FILE_EXE, &exec_path);
 
   return (!other_chrome_path.empty() &&
-          other_chrome_path.BaseName() ==
-          exec_path.BaseName());
+          other_chrome_path.BaseName() == exec_path.BaseName());
 }
 
 // A helper class to hold onto a socket.
@@ -988,13 +987,15 @@ bool ProcessSingleton::Create() {
   if (listen(sock, 5) < 0)
     NOTREACHED() << "listen failed: " << base::safe_strerror(errno);
 
-  DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
-  BrowserThread::PostTask(
-      BrowserThread::IO,
+  // In Electron the ProcessSingleton is created earlier than the IO
+  // thread gets created, so we have to postpone the call until message
+  // loop is up an running.
+  scoped_refptr<base::SingleThreadTaskRunner> task_runner(
+      base::ThreadTaskRunnerHandle::Get());
+  task_runner->PostTask(
       FROM_HERE,
-      base::Bind(&ProcessSingleton::LinuxWatcher::StartListening,
-                 watcher_.get(),
-                 sock));
+      base::Bind(&ProcessSingleton::StartListening,
+                 base::Unretained(this), sock));
 
   return true;
 }
@@ -1005,6 +1006,16 @@ void ProcessSingleton::Cleanup() {
   UnlinkPath(lock_path_);
 }
 
+void ProcessSingleton::StartListening(int sock) {
+  DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
+  BrowserThread::PostTask(
+      BrowserThread::IO,
+      FROM_HERE,
+      base::Bind(&ProcessSingleton::LinuxWatcher::StartListening,
+                 watcher_.get(),
+                 sock));
+}
+
 bool ProcessSingleton::IsSameChromeInstance(pid_t pid) {
   pid_t cur_pid = current_pid_;
   while (pid != cur_pid) {