Bug 1470306 - Adding some MOZ_DIAGNOSTIC_ASSERT in MessagePort, r=smaug

This commit is contained in:
Andrea Marchesini 2018-07-13 17:41:34 +02:00
parent 1494e3f878
commit d6ce364ced
2 changed files with 17 additions and 13 deletions

View file

@ -23,6 +23,7 @@
#include "mozilla/ipc/BackgroundChild.h" #include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/PBackgroundChild.h" #include "mozilla/ipc/PBackgroundChild.h"
#include "mozilla/MessagePortTimelineMarker.h" #include "mozilla/MessagePortTimelineMarker.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/TimelineConsumers.h" #include "mozilla/TimelineConsumers.h"
#include "mozilla/TimelineMarker.h" #include "mozilla/TimelineMarker.h"
#include "mozilla/Unused.h" #include "mozilla/Unused.h"
@ -253,7 +254,7 @@ MessagePort::UnshippedEntangle(MessagePort* aEntangledPort)
void void
MessagePort::Initialize(const nsID& aUUID, MessagePort::Initialize(const nsID& aUUID,
const nsID& aDestinationUUID, const nsID& aDestinationUUID,
uint32_t aSequenceID, bool mNeutered, uint32_t aSequenceID, bool aNeutered,
ErrorResult& aRv) ErrorResult& aRv)
{ {
MOZ_ASSERT(mIdentifier); MOZ_ASSERT(mIdentifier);
@ -261,7 +262,7 @@ MessagePort::Initialize(const nsID& aUUID,
mIdentifier->destinationUuid() = aDestinationUUID; mIdentifier->destinationUuid() = aDestinationUUID;
mIdentifier->sequenceId() = aSequenceID; mIdentifier->sequenceId() = aSequenceID;
if (mNeutered) { if (aNeutered) {
// If this port is neutered we don't want to keep it alive artificially nor // If this port is neutered we don't want to keep it alive artificially nor
// we want to add listeners or WorkerRefs. // we want to add listeners or WorkerRefs.
mState = eStateDisentangled; mState = eStateDisentangled;
@ -508,11 +509,10 @@ MessagePort::CloseInternal(bool aSoftly)
} }
if (mState == eStateUnshippedEntangled) { if (mState == eStateUnshippedEntangled) {
MOZ_ASSERT(mUnshippedEntangledPort); MOZ_DIAGNOSTIC_ASSERT(mUnshippedEntangledPort);
// This avoids loops. // This avoids loops.
RefPtr<MessagePort> port = std::move(mUnshippedEntangledPort); RefPtr<MessagePort> port = std::move(mUnshippedEntangledPort);
MOZ_ASSERT(mUnshippedEntangledPort == nullptr);
mState = eStateDisentangledForClose; mState = eStateDisentangledForClose;
port->CloseInternal(aSoftly); port->CloseInternal(aSoftly);
@ -743,14 +743,14 @@ MessagePort::CloneAndDisentangle(MessagePortIdentifier& aIdentifier)
MOZ_ASSERT(mUnshippedEntangledPort); MOZ_ASSERT(mUnshippedEntangledPort);
MOZ_ASSERT(mMessagesForTheOtherPort.IsEmpty()); MOZ_ASSERT(mMessagesForTheOtherPort.IsEmpty());
RefPtr<MessagePort> port = std::move(mUnshippedEntangledPort);
// Disconnect the entangled port and connect it to PBackground. // Disconnect the entangled port and connect it to PBackground.
if (!mUnshippedEntangledPort->ConnectToPBackground()) { if (!port->ConnectToPBackground()) {
// We are probably shutting down. We cannot proceed. // We are probably shutting down. We cannot proceed.
return; return;
} }
mUnshippedEntangledPort = nullptr;
// In this case, we don't need to be connected to the PBackground service. // In this case, we don't need to be connected to the PBackground service.
if (mMessages.IsEmpty()) { if (mMessages.IsEmpty()) {
aIdentifier.sequenceId() = mIdentifier->sequenceId(); aIdentifier.sequenceId() = mIdentifier->sequenceId();
@ -800,7 +800,11 @@ MessagePort::Closed()
bool bool
MessagePort::ConnectToPBackground() MessagePort::ConnectToPBackground()
{ {
mState = eStateEntangling; RefPtr<MessagePort> self = this;
auto raii = MakeScopeExit([self] {
self->mState = eStateDisentangled;
self->UpdateMustKeepAlive();
});
mozilla::ipc::PBackgroundChild* actorChild = mozilla::ipc::PBackgroundChild* actorChild =
mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread(); mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
@ -820,6 +824,9 @@ MessagePort::ConnectToPBackground()
MOZ_ASSERT(mActor); MOZ_ASSERT(mActor);
mActor->SetPort(this); mActor->SetPort(this);
mState = eStateEntangling;
raii.release();
return true; return true;
} }
@ -847,10 +854,7 @@ MessagePort::UpdateMustKeepAlive()
void void
MessagePort::DisconnectFromOwner() MessagePort::DisconnectFromOwner()
{ {
if (mIsKeptAlive) {
CloseForced(); CloseForced();
}
DOMEventTargetHelper::DisconnectFromOwner(); DOMEventTargetHelper::DisconnectFromOwner();
} }

View file

@ -140,7 +140,7 @@ private:
void DisconnectFromOwner() override; void DisconnectFromOwner() override;
void Initialize(const nsID& aUUID, const nsID& aDestinationUUID, void Initialize(const nsID& aUUID, const nsID& aDestinationUUID,
uint32_t aSequenceID, bool mNeutered, ErrorResult& aRv); uint32_t aSequenceID, bool aNeutered, ErrorResult& aRv);
bool ConnectToPBackground(); bool ConnectToPBackground();