From d6ce364ced2c6c5f7d7ca5ff1e17ed0930b7645c Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Fri, 13 Jul 2018 17:41:34 +0200 Subject: [PATCH] Bug 1470306 - Adding some MOZ_DIAGNOSTIC_ASSERT in MessagePort, r=smaug --- dom/messagechannel/MessagePort.cpp | 28 ++++++++++++++++------------ dom/messagechannel/MessagePort.h | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/dom/messagechannel/MessagePort.cpp b/dom/messagechannel/MessagePort.cpp index 3ab74b19c4a0..c9304f16536c 100644 --- a/dom/messagechannel/MessagePort.cpp +++ b/dom/messagechannel/MessagePort.cpp @@ -23,6 +23,7 @@ #include "mozilla/ipc/BackgroundChild.h" #include "mozilla/ipc/PBackgroundChild.h" #include "mozilla/MessagePortTimelineMarker.h" +#include "mozilla/ScopeExit.h" #include "mozilla/TimelineConsumers.h" #include "mozilla/TimelineMarker.h" #include "mozilla/Unused.h" @@ -253,7 +254,7 @@ MessagePort::UnshippedEntangle(MessagePort* aEntangledPort) void MessagePort::Initialize(const nsID& aUUID, const nsID& aDestinationUUID, - uint32_t aSequenceID, bool mNeutered, + uint32_t aSequenceID, bool aNeutered, ErrorResult& aRv) { MOZ_ASSERT(mIdentifier); @@ -261,7 +262,7 @@ MessagePort::Initialize(const nsID& aUUID, mIdentifier->destinationUuid() = aDestinationUUID; mIdentifier->sequenceId() = aSequenceID; - if (mNeutered) { + if (aNeutered) { // If this port is neutered we don't want to keep it alive artificially nor // we want to add listeners or WorkerRefs. mState = eStateDisentangled; @@ -508,11 +509,10 @@ MessagePort::CloseInternal(bool aSoftly) } if (mState == eStateUnshippedEntangled) { - MOZ_ASSERT(mUnshippedEntangledPort); + MOZ_DIAGNOSTIC_ASSERT(mUnshippedEntangledPort); // This avoids loops. RefPtr port = std::move(mUnshippedEntangledPort); - MOZ_ASSERT(mUnshippedEntangledPort == nullptr); mState = eStateDisentangledForClose; port->CloseInternal(aSoftly); @@ -743,14 +743,14 @@ MessagePort::CloneAndDisentangle(MessagePortIdentifier& aIdentifier) MOZ_ASSERT(mUnshippedEntangledPort); MOZ_ASSERT(mMessagesForTheOtherPort.IsEmpty()); + RefPtr port = std::move(mUnshippedEntangledPort); + // Disconnect the entangled port and connect it to PBackground. - if (!mUnshippedEntangledPort->ConnectToPBackground()) { + if (!port->ConnectToPBackground()) { // We are probably shutting down. We cannot proceed. return; } - mUnshippedEntangledPort = nullptr; - // In this case, we don't need to be connected to the PBackground service. if (mMessages.IsEmpty()) { aIdentifier.sequenceId() = mIdentifier->sequenceId(); @@ -800,7 +800,11 @@ MessagePort::Closed() bool MessagePort::ConnectToPBackground() { - mState = eStateEntangling; + RefPtr self = this; + auto raii = MakeScopeExit([self] { + self->mState = eStateDisentangled; + self->UpdateMustKeepAlive(); + }); mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(); @@ -820,6 +824,9 @@ MessagePort::ConnectToPBackground() MOZ_ASSERT(mActor); mActor->SetPort(this); + mState = eStateEntangling; + + raii.release(); return true; } @@ -847,10 +854,7 @@ MessagePort::UpdateMustKeepAlive() void MessagePort::DisconnectFromOwner() { - if (mIsKeptAlive) { - CloseForced(); - } - + CloseForced(); DOMEventTargetHelper::DisconnectFromOwner(); } diff --git a/dom/messagechannel/MessagePort.h b/dom/messagechannel/MessagePort.h index 7208bd9feb46..517b62eb6b00 100644 --- a/dom/messagechannel/MessagePort.h +++ b/dom/messagechannel/MessagePort.h @@ -140,7 +140,7 @@ private: void DisconnectFromOwner() override; void Initialize(const nsID& aUUID, const nsID& aDestinationUUID, - uint32_t aSequenceID, bool mNeutered, ErrorResult& aRv); + uint32_t aSequenceID, bool aNeutered, ErrorResult& aRv); bool ConnectToPBackground();