Skip to content

Commit

Permalink
Bug 1715414 - Start PBackground earlier in content processes. r=nika
Browse files Browse the repository at this point in the history
Previously we were staring `PBackground` in content processes in
response to receiving the `SetXPCOMProcessAttributes` IPC message, which
is sent immediately after the process is launched.  Meanwhile, the
idle scheduler tries to use PBackground when the main thread considers
itself idle.  But if thread scheduling is such that the content process
main thread becomes idle before the IPC I/O thread has received and
dispatched that message, then we have a problem (signaled by an assertion
failure).

This patch moves content process `PBackground` startup earlier, to the
end of `ContentProcess::Init`; that point is after enough of IPC and
XPCOM is started for it to work, but before we start spinning the main
thread event loop.

Differential Revision: https://phabricator.services.mozilla.com/D126144
  • Loading branch information
jld committed Sep 28, 2021
1 parent 6ccf78f commit fb5243f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 0 additions & 4 deletions dom/ipc/ContentChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,10 +1285,6 @@ void ContentChild::InitSharedUASheets(const Maybe<SharedMemoryHandle>& aHandle,
void ContentChild::InitXPCOM(
XPCOMInitData&& aXPCOMInit,
const mozilla::dom::ipc::StructuredCloneData& aInitialData) {
// Do this as early as possible to get the parent process to initialize the
// background thread since we'll likely need database information very soon.
BackgroundChild::Startup();

#ifdef MOZ_WIDGET_GTK
// LookAndFeel::NativeInit takes a long time to run on Linux, here we schedule
// it as soon as possible after BackgroundChild::Startup to give
Expand Down
5 changes: 5 additions & 0 deletions dom/ipc/ContentProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#endif

#include "nsAppRunner.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/ProcessUtils.h"

using mozilla::ipc::IOThreadChild;
Expand Down Expand Up @@ -218,6 +219,10 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
SetUpSandboxEnvironment();
#endif

// Do this as early as possible to get the parent process to initialize the
// background thread since we'll likely need database information very soon.
mozilla::ipc::BackgroundChild::Startup();

return true;
}

Expand Down
8 changes: 3 additions & 5 deletions ipc/glue/BackgroundChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ namespace mozilla {
namespace dom {

class BlobImpl;
class ContentChild;
class ContentParent;
class ContentProcess;

} // namespace dom

namespace net {

class SocketProcessImpl;
class SocketProcessChild;

} // namespace net
Expand Down Expand Up @@ -55,9 +54,8 @@ class PBackgroundChild;
// The PBackgroundChild actor and all its sub-protocol actors will be
// automatically destroyed when its designated thread completes.
class BackgroundChild final {
friend class mozilla::dom::ContentChild;
friend class mozilla::dom::ContentParent;
friend class mozilla::net::SocketProcessImpl;
friend class mozilla::dom::ContentProcess;
friend class mozilla::net::SocketProcessChild;

typedef mozilla::ipc::Transport Transport;
Expand All @@ -82,7 +80,7 @@ class BackgroundChild final {
nsIEventTarget* aMainEventTarget = nullptr);

private:
// Only called by ContentChild or ContentParent.
// Only called by this class's friends.
static void Startup();
};

Expand Down

0 comments on commit fb5243f

Please sign in to comment.