Bug 1551990 - Firefox hangs on wake up from OS X Power Nap r=spohl a=jcristau
authorHaik Aftandilian <haftandilian@mozilla.com>
Tue, 04 Jun 2019 20:04:24 +0000
changeset 536718 9337bf07777d1a004af086c50cb201f91466f0bb
parent 536717 236675dcbcab341c475e85e0f6999df67eec3e22
child 536719 f60cd8f26e67f81dfcaf5b943e6e289650d09d79
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersspohl, jcristau
bugs1551990
milestone68.0
Bug 1551990 - Firefox hangs on wake up from OS X Power Nap r=spohl a=jcristau Disable App Nap in Firefox to avoid macOS App Nap'ing the parent process while child processes continue to run normally. Differential Revision: https://phabricator.services.mozilla.com/D33130
toolkit/xre/MacApplicationDelegate.h
toolkit/xre/MacApplicationDelegate.mm
toolkit/xre/nsAppRunner.cpp
--- a/toolkit/xre/MacApplicationDelegate.h
+++ b/toolkit/xre/MacApplicationDelegate.h
@@ -7,10 +7,11 @@
 // C++, so it itself cannot have any Obj-C bits in it.
 
 #ifndef MacApplicationDelegate_h_
 #define MacApplicationDelegate_h_
 
 void EnsureUseCocoaDockAPI(void);
 void SetupMacApplicationDelegate(void);
 void ProcessPendingGetURLAppleEvents(void);
+void DisableAppNap(void);
 
 #endif
--- a/toolkit/xre/MacApplicationDelegate.mm
+++ b/toolkit/xre/MacApplicationDelegate.mm
@@ -59,16 +59,29 @@ static bool sProcessedGetURLEvent = fals
 void EnsureUseCocoaDockAPI() {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   [GeckoNSApplication sharedApplication];
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
+void DisableAppNap() {
+  // Prevent the parent process from entering App Nap. macOS does not put our
+  // child processes into App Nap and, as a result, when the parent is in
+  // App Nap, child processes continue to run normally generating IPC messages
+  // for the parent which can end up being queued. This can cause the browser
+  // to be unresponsive for a period of time after the App Nap until the parent
+  // process "catches up." NSAppSleepDisabled has to be set early during
+  // startup before the OS reads the value for the process.
+  [[NSUserDefaults standardUserDefaults] registerDefaults:@{
+    @"NSAppSleepDisabled" : @YES,
+  }];
+}
+
 void SetupMacApplicationDelegate() {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   // this is called during startup, outside an event loop, and therefore
   // needs an autorelease pool to avoid cocoa object leakage (bug 559075)
   AutoAutoreleasePool pool;
 
   // Ensure that ProcessPendingGetURLAppleEvents() doesn't regress bug 377166.
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -3007,16 +3007,20 @@ int XREMain::XRE_mainInit(bool* aExitFla
   auto expectedShutdown = mozilla::MakeScopeExit([&] { MozExpectedExit(); });
 
   StartupTimeline::Record(StartupTimeline::MAIN);
 
   if (CheckForUserMismatch()) {
     return 1;
   }
 
+#ifdef XP_MACOSX
+  DisableAppNap();
+#endif
+
   if (PR_GetEnv("MOZ_CHAOSMODE")) {
     ChaosFeature feature = ChaosFeature::Any;
     long featureInt = strtol(PR_GetEnv("MOZ_CHAOSMODE"), nullptr, 16);
     if (featureInt) {
       // NOTE: MOZ_CHAOSMODE=0 or a non-hex value maps to Any feature.
       feature = static_cast<ChaosFeature>(featureInt);
     }
     ChaosMode::SetChaosFeature(feature);