From 737f45f2c0dd4c1a15788132c81a33f44ad2834a Mon Sep 17 00:00:00 2001 From: Dragana Damjanovic Date: Fri, 8 Jan 2016 10:20:00 -0500 Subject: [PATCH] Bug 1238017 - Remove ClosingService. r=mcmanus --- netwerk/base/ClosingService.cpp | 308 ------------------ netwerk/base/ClosingService.h | 71 ---- netwerk/base/moz.build | 1 - netwerk/base/nsIOService.cpp | 9 - netwerk/base/nsIncrementalDownload.cpp | 5 +- netwerk/base/nsSocketTransport2.cpp | 4 - netwerk/base/nsUDPSocket.cpp | 2 - netwerk/protocol/rtsp/rtsp/ARTPConnection.cpp | 4 - netwerk/protocol/rtsp/rtsp/ARTPSession.cpp | 2 - netwerk/protocol/rtsp/rtsp/ARTPWriter.cpp | 2 - .../protocol/rtsp/rtsp/ARTSPConnection.cpp | 2 - netwerk/protocol/rtsp/rtsp/UDPPusher.cpp | 2 - 12 files changed, 3 insertions(+), 409 deletions(-) delete mode 100644 netwerk/base/ClosingService.cpp delete mode 100644 netwerk/base/ClosingService.h diff --git a/netwerk/base/ClosingService.cpp b/netwerk/base/ClosingService.cpp deleted file mode 100644 index 61f9df1549bf..000000000000 --- a/netwerk/base/ClosingService.cpp +++ /dev/null @@ -1,308 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set sw=2 ts=8 et tw=80 : */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "ClosingService.h" -#include "nsIOService.h" -#ifdef MOZ_NUWA_PROCESS -#include "ipc/Nuwa.h" -#endif - -class ClosingLayerSecret -{ -public: - explicit ClosingLayerSecret(mozilla::net::ClosingService *aClosingService) - : mClosingService(aClosingService) - { - } - - ~ClosingLayerSecret() - { - mClosingService = nullptr; - } - - RefPtr mClosingService; -}; - -namespace mozilla { -namespace net { - -static PRIOMethods sTcpUdpPRCloseLayerMethods; -static PRIOMethods *sTcpUdpPRCloseLayerMethodsPtr = nullptr; -static PRDescIdentity sTcpUdpPRCloseLayerId; - -static PRStatus -TcpUdpPRCloseLayerClose(PRFileDesc *aFd) -{ - if (!aFd) { - return PR_FAILURE; - } - - PRFileDesc* layer = PR_PopIOLayer(aFd, PR_TOP_IO_LAYER); - MOZ_RELEASE_ASSERT(layer && - layer->identity == sTcpUdpPRCloseLayerId, - "Closing Layer not on top of stack"); - - ClosingLayerSecret *closingLayerSecret = - reinterpret_cast(layer->secret); - - PRStatus status = PR_SUCCESS; - - if (aFd) { - // If this is called during shutdown do not call ..method->close(fd) and - // let it leak. - if (gIOService->IsNetTearingDown()) { - // If the ClosingService layer is the first layer above PR_NSPR_IO_LAYER - // we are not going to leak anything, but the PR_Close will not be called. - PR_Free(aFd); - } else if (closingLayerSecret->mClosingService) { - closingLayerSecret->mClosingService->PostRequest(aFd); - } else { - // Socket is created before closing service has been started or there was - // a problem with starting it. - PR_Close(aFd); - } - } - - layer->secret = nullptr; - layer->dtor(layer); - delete closingLayerSecret; - return status; -} - -ClosingService* ClosingService::sInstance = nullptr; - -ClosingService::ClosingService() - : mShutdown(false) - , mMonitor("ClosingService.mMonitor") -{ - MOZ_ASSERT(!sInstance, - "multiple ClosingService instances!"); -} - -// static -void -ClosingService::Start() -{ - if (!sTcpUdpPRCloseLayerMethodsPtr) { - sTcpUdpPRCloseLayerId = PR_GetUniqueIdentity("TCP and UDP PRClose layer"); - PR_ASSERT(PR_INVALID_IO_LAYER != sTcpUdpPRCloseLayerId); - - sTcpUdpPRCloseLayerMethods = *PR_GetDefaultIOMethods(); - sTcpUdpPRCloseLayerMethods.close = TcpUdpPRCloseLayerClose; - sTcpUdpPRCloseLayerMethodsPtr = &sTcpUdpPRCloseLayerMethods; - } - - if (!sInstance) { - ClosingService* service = new ClosingService(); - if (NS_SUCCEEDED(service->StartInternal())) { - NS_ADDREF(service); - sInstance = service; - } else { - delete service; - } - } -} - -nsresult -ClosingService::StartInternal() -{ - mThread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, this, - PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, - PR_JOINABLE_THREAD, 32 * 1024); - if (!mThread) { - return NS_ERROR_FAILURE; - } - return NS_OK; -} - -// static -nsresult -ClosingService::AttachIOLayer(PRFileDesc *aFd) -{ - // We are going to remove ClosingService soon. - // This change is going to turn it off, so ClosingService is not used. - // Bug 1238010. - return NS_OK; - - if (!sTcpUdpPRCloseLayerMethodsPtr) { - return NS_OK; - } - - PRFileDesc * layer; - PRStatus status; - - layer = PR_CreateIOLayerStub(sTcpUdpPRCloseLayerId, - sTcpUdpPRCloseLayerMethodsPtr); - - if (!layer) { - return NS_OK; - } - - ClosingLayerSecret *secret = new ClosingLayerSecret(sInstance); - layer->secret = reinterpret_cast(secret); - - status = PR_PushIOLayer(aFd, PR_NSPR_IO_LAYER, layer); - - if (status == PR_FAILURE) { - delete secret; - PR_DELETE(layer); - } - return NS_OK; -} - -void -ClosingService::PostRequest(PRFileDesc *aFd) -{ - mozilla::MonitorAutoLock mon(mMonitor); - - // Check if shutdown is called. - if (mShutdown) { - // Let the socket leak. We are in shutdown and some PRClose can take a long - // time. To prevent shutdown crash (bug 1152046) do not accept sockets any - // more. - // If the ClosingService layer is the first layer above PR_NSPR_IO_LAYER - // we are not going to leak anything, but PR_Close will not be called. - PR_Free(aFd); - return; - } - - mQueue.AppendElement(aFd); - if (mQueue.Length() == 1) { - mon.Notify(); - } -} - -// static -void -ClosingService::Shutdown() -{ - MOZ_ASSERT(NS_IsMainThread()); - - if (sInstance) { - sInstance->ShutdownInternal(); - NS_RELEASE(sInstance); - } -} - -void -ClosingService::ShutdownInternal() -{ - { - mozilla::MonitorAutoLock mon(mMonitor); - if (mShutdown) { - // This should not happen. - return; - } - - mShutdown = true; - // If it is waiting on the empty queue, wake it up. - if (mQueue.Length() == 0) { - mon.Notify(); - } - } - - if (mThread) { - PR_JoinThread(mThread); - mThread = nullptr; - } -} - -void -ClosingService::ThreadFunc() -{ - PR_SetCurrentThreadName("Closing Service"); -#ifdef MOZ_NUWA_PROCESS - if (IsNuwaProcess()) { - NuwaMarkCurrentThread(nullptr, nullptr); - } -#endif - - for (;;) { - PRFileDesc *fd; - { - mozilla::MonitorAutoLock mon(mMonitor); - while (!mShutdown && (mQueue.Length() == 0)) { - mon.Wait(); - } - - if (mShutdown) { - // If we are in shutdown leak the rest of the sockets. - for (uint32_t i = 0; i < mQueue.Length(); i++) { - fd = mQueue[i]; - // If the ClosingService layer is the first layer above - // PR_NSPR_IO_LAYER we are not going to leak anything, but PR_Close - // will not be called. - PR_Free(fd); - } - mQueue.Clear(); - return; - } - - fd = mQueue[0]; - mQueue.RemoveElementAt(0); - } - // Leave lock before closing socket. It can block for a long time and in - // case we accidentally attach this layer twice this would cause deadlock. - - bool tcp = (PR_GetDescType(PR_GetIdentitiesLayer(fd, PR_NSPR_IO_LAYER)) == - PR_DESC_SOCKET_TCP); - - PRIntervalTime closeStarted = PR_IntervalNow(); - fd->methods->close(fd); - - // Post telemetry. - if (tcp) { - SendPRCloseTelemetry(closeStarted, - Telemetry::PRCLOSE_TCP_BLOCKING_TIME_NORMAL, - Telemetry::PRCLOSE_TCP_BLOCKING_TIME_SHUTDOWN, - Telemetry::PRCLOSE_TCP_BLOCKING_TIME_CONNECTIVITY_CHANGE, - Telemetry::PRCLOSE_TCP_BLOCKING_TIME_LINK_CHANGE, - Telemetry::PRCLOSE_TCP_BLOCKING_TIME_OFFLINE); - } else { - SendPRCloseTelemetry(closeStarted, - Telemetry::PRCLOSE_UDP_BLOCKING_TIME_NORMAL, - Telemetry::PRCLOSE_UDP_BLOCKING_TIME_SHUTDOWN, - Telemetry::PRCLOSE_UDP_BLOCKING_TIME_CONNECTIVITY_CHANGE, - Telemetry::PRCLOSE_UDP_BLOCKING_TIME_LINK_CHANGE, - Telemetry::PRCLOSE_UDP_BLOCKING_TIME_OFFLINE); - } - } -} - -void -ClosingService::SendPRCloseTelemetry(PRIntervalTime aStart, - mozilla::Telemetry::ID aIDNormal, - mozilla::Telemetry::ID aIDShutdown, - mozilla::Telemetry::ID aIDConnectivityChange, - mozilla::Telemetry::ID aIDLinkChange, - mozilla::Telemetry::ID aIDOffline) -{ - PRIntervalTime now = PR_IntervalNow(); - if (gIOService->IsNetTearingDown()) { - Telemetry::Accumulate(aIDShutdown, - PR_IntervalToMilliseconds(now - aStart)); - - } else if (PR_IntervalToSeconds(now - gIOService->LastConnectivityChange()) - < 60) { - Telemetry::Accumulate(aIDConnectivityChange, - PR_IntervalToMilliseconds(now - aStart)); - } else if (PR_IntervalToSeconds(now - gIOService->LastNetworkLinkChange()) - < 60) { - Telemetry::Accumulate(aIDLinkChange, - PR_IntervalToMilliseconds(now - aStart)); - - } else if (PR_IntervalToSeconds(now - gIOService->LastOfflineStateChange()) - < 60) { - Telemetry::Accumulate(aIDOffline, - PR_IntervalToMilliseconds(now - aStart)); - } else { - Telemetry::Accumulate(aIDNormal, - PR_IntervalToMilliseconds(now - aStart)); - } -} - -} //namwspacw mozilla -} //namespace net diff --git a/netwerk/base/ClosingService.h b/netwerk/base/ClosingService.h deleted file mode 100644 index 3e7f941230bc..000000000000 --- a/netwerk/base/ClosingService.h +++ /dev/null @@ -1,71 +0,0 @@ -/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set sw=2 ts=8 et tw=80 : */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#ifndef ClosingService_h__ -#define ClosingService_h__ - -#include "nsTArray.h" -#include "nspr.h" -#include "mozilla/Telemetry.h" -#include "mozilla/Monitor.h" - -//----------------------------------------------------------------------------- -// ClosingService -//----------------------------------------------------------------------------- - -// A helper class carrying call to PR_Close on FD to a separate thread - -// closingThread. This may be a workaround for shutdown blocks that are caused -// by serial calls to close on UDP and TCP sockets. -// This service is started by nsIOService and also the class adds itself as an -// observer to "xpcom-shutdown-threads" notification where we join the thread -// and remove reference. -// During worktime of the thread the class is also self-referenced, -// since observer service might throw the reference away sooner than the thread -// is actually done. - -namespace mozilla { -namespace net { - -class ClosingService final -{ -public: - NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ClosingService) - - ClosingService(); - - // Attaching this layer on tcp or udp sockets PRClose will be send to the - // closingThread. - static nsresult AttachIOLayer(PRFileDesc *aFd); - static void Start(); - static void Shutdown(); - void PostRequest(PRFileDesc *aFd); - -private: - ~ClosingService() {} - nsresult StartInternal(); - void ShutdownInternal(); - void ThreadFunc(); - static void ThreadFunc(void *aClosure) - { static_cast(aClosure)->ThreadFunc(); } - - void SendPRCloseTelemetry(PRIntervalTime aStart, - mozilla::Telemetry::ID aIDNormal, - mozilla::Telemetry::ID aIDShutdown, - mozilla::Telemetry::ID aIDConnectivityChange, - mozilla::Telemetry::ID aIDLinkChange, - mozilla::Telemetry::ID aIDOffline); - - static ClosingService* sInstance; - Atomic mShutdown; - nsTArray mQueue; - mozilla::Monitor mMonitor; - PRThread *mThread; -}; - -} // namespace net -} // namespace mozilla - -#endif // ClosingService_h_ diff --git a/netwerk/base/moz.build b/netwerk/base/moz.build index 3444092bd2f8..39d76323905e 100644 --- a/netwerk/base/moz.build +++ b/netwerk/base/moz.build @@ -193,7 +193,6 @@ UNIFIED_SOURCES += [ 'CaptivePortalService.cpp', 'ChannelDiverterChild.cpp', 'ChannelDiverterParent.cpp', - 'ClosingService.cpp', 'Dashboard.cpp', 'EventTokenBucket.cpp', 'LoadContextInfo.cpp', diff --git a/netwerk/base/nsIOService.cpp b/netwerk/base/nsIOService.cpp index bc616d71abad..a72e71579328 100644 --- a/netwerk/base/nsIOService.cpp +++ b/netwerk/base/nsIOService.cpp @@ -49,7 +49,6 @@ #include "mozilla/Telemetry.h" #include "mozilla/net/DNS.h" #include "CaptivePortalService.h" -#include "ClosingService.h" #include "ReferrerPolicy.h" #include "nsContentSecurityManager.h" #include "nsHttpHandler.h" @@ -65,7 +64,6 @@ using namespace mozilla; using mozilla::net::IsNeckoChild; -using mozilla::net::ClosingService; using mozilla::net::CaptivePortalService; using mozilla::net::gHttpHandler; @@ -260,10 +258,6 @@ nsIOService::Init() InitializeNetworkLinkService(); - // Start the closing service. Actual PR_Close() will be carried out on - // a separate "closing" thread. Start the closing servicee here since this - // point is executed only once per session. - ClosingService::Start(); SetOffline(false); return NS_OK; @@ -1077,9 +1071,6 @@ nsIOService::SetOffline(bool offline) DebugOnly rv = mSocketTransportService->Shutdown(); NS_ASSERTION(NS_SUCCEEDED(rv), "socket transport service shutdown failed"); } - if (mShutdown) { - ClosingService::Shutdown(); - } } mSettingOffline = false; diff --git a/netwerk/base/nsIncrementalDownload.cpp b/netwerk/base/nsIncrementalDownload.cpp index 554eeb1eef3b..42cd6faa5e0c 100644 --- a/netwerk/base/nsIncrementalDownload.cpp +++ b/netwerk/base/nsIncrementalDownload.cpp @@ -6,6 +6,7 @@ #include "mozilla/Attributes.h" #include "mozilla/UniquePtrExtensions.h" +#include "mozilla/UniquePtr.h" #include "nsIIncrementalDownload.h" #include "nsIRequestObserver.h" @@ -150,7 +151,7 @@ private: nsCOMPtr mDest; nsCOMPtr mChannel; nsCOMPtr mTimer; - UniquePtr mChunk; + mozilla::UniquePtr mChunk; int32_t mChunkLen; int32_t mChunkSize; int32_t mInterval; @@ -712,7 +713,7 @@ nsIncrementalDownload::OnStartRequest(nsIRequest *request, if (diff < int64_t(mChunkSize)) mChunkSize = uint32_t(diff); - mChunk = MakeUniqueFallible(mChunkSize); + mChunk = mozilla::MakeUniqueFallible(mChunkSize); if (!mChunk) rv = NS_ERROR_OUT_OF_MEMORY; diff --git a/netwerk/base/nsSocketTransport2.cpp b/netwerk/base/nsSocketTransport2.cpp index 58720508119d..7199985b0953 100644 --- a/netwerk/base/nsSocketTransport2.cpp +++ b/netwerk/base/nsSocketTransport2.cpp @@ -16,7 +16,6 @@ #include "nsProxyInfo.h" #include "nsNetCID.h" #include "nsNetUtil.h" -#include "ClosingService.h" #include "nsAutoPtr.h" #include "nsCOMPtr.h" #include "plstr.h" @@ -1325,9 +1324,6 @@ nsSocketTransport::InitiateSocket() // Attach network activity monitor mozilla::net::NetworkActivityMonitor::AttachIOLayer(fd); - // Attach closing service. - ClosingService::AttachIOLayer(fd); - PRStatus status; // Make the socket non-blocking... diff --git a/netwerk/base/nsUDPSocket.cpp b/netwerk/base/nsUDPSocket.cpp index 03e9d5e325fe..375f00ff5b0b 100644 --- a/netwerk/base/nsUDPSocket.cpp +++ b/netwerk/base/nsUDPSocket.cpp @@ -30,7 +30,6 @@ #include "nsIDNSRecord.h" #include "nsIDNSService.h" #include "nsICancelable.h" -#include "ClosingService.h" #ifdef MOZ_WIDGET_GONK #include "NetStatistics.h" @@ -666,7 +665,6 @@ nsUDPSocket::InitWithAddress(const NetAddr *aAddr, nsIPrincipal *aPrincipal, // create proxy via NetworkActivityMonitor NetworkActivityMonitor::AttachIOLayer(mFD); - ClosingService::AttachIOLayer(mFD); // wait until AsyncListen is called before polling the socket for // client connections. diff --git a/netwerk/protocol/rtsp/rtsp/ARTPConnection.cpp b/netwerk/protocol/rtsp/rtsp/ARTPConnection.cpp index 410954b60429..e4ce82ffb180 100644 --- a/netwerk/protocol/rtsp/rtsp/ARTPConnection.cpp +++ b/netwerk/protocol/rtsp/rtsp/ARTPConnection.cpp @@ -36,7 +36,6 @@ #include "prerr.h" #include "prerror.h" #include "NetworkActivityMonitor.h" -#include "ClosingService.h" using namespace mozilla::net; @@ -145,9 +144,6 @@ void ARTPConnection::MakePortPair( NetworkActivityMonitor::AttachIOLayer(*rtpSocket); NetworkActivityMonitor::AttachIOLayer(*rtcpSocket); - ClosingService::AttachIOLayer(*rtpSocket); - ClosingService::AttachIOLayer(*rtcpSocket); - // Reduce the chance of using duplicate port numbers. srand(time(NULL)); // rand() * 1000 may overflow int type, use long long. diff --git a/netwerk/protocol/rtsp/rtsp/ARTPSession.cpp b/netwerk/protocol/rtsp/rtsp/ARTPSession.cpp index 9af10065da91..b052adb464e8 100644 --- a/netwerk/protocol/rtsp/rtsp/ARTPSession.cpp +++ b/netwerk/protocol/rtsp/rtsp/ARTPSession.cpp @@ -34,7 +34,6 @@ #include "prnetdb.h" #include "prerr.h" #include "NetworkActivityMonitor.h" -#include "ClosingService.h" using namespace mozilla::net; @@ -110,7 +109,6 @@ void ARTPSession::MakeUDPSocket(PRFileDesc **s, unsigned port) { } NetworkActivityMonitor::AttachIOLayer(*s); - ClosingService::AttachIOLayer(*s); PRNetAddr addr; addr.inet.family = PR_AF_INET; diff --git a/netwerk/protocol/rtsp/rtsp/ARTPWriter.cpp b/netwerk/protocol/rtsp/rtsp/ARTPWriter.cpp index 62ec35ebab75..c075d90d6f51 100644 --- a/netwerk/protocol/rtsp/rtsp/ARTPWriter.cpp +++ b/netwerk/protocol/rtsp/rtsp/ARTPWriter.cpp @@ -31,7 +31,6 @@ #include #include -#include "ClosingService.h" #include "NetworkActivityMonitor.h" using namespace mozilla::net; @@ -65,7 +64,6 @@ ARTPWriter::ARTPWriter(int fd) } NetworkActivityMonitor::AttachIOLayer(mSocket); - ClosingService::AttachIOLayer(mSocket); mRTPAddr.inet.family = PR_AF_INET; diff --git a/netwerk/protocol/rtsp/rtsp/ARTSPConnection.cpp b/netwerk/protocol/rtsp/rtsp/ARTSPConnection.cpp index 3a5f84cb8c22..2a15973aa7da 100644 --- a/netwerk/protocol/rtsp/rtsp/ARTSPConnection.cpp +++ b/netwerk/protocol/rtsp/rtsp/ARTSPConnection.cpp @@ -34,7 +34,6 @@ #include "nsCOMPtr.h" #include "nsString.h" #include "nsNetCID.h" -#include "ClosingService.h" #include "nsIServiceManager.h" #include "nsICryptoHash.h" @@ -284,7 +283,6 @@ void ARTSPConnection::onConnect(const sp &msg) { } NetworkActivityMonitor::AttachIOLayer(mSocket); - ClosingService::AttachIOLayer(mSocket); MakeSocketBlocking(mSocket, false); diff --git a/netwerk/protocol/rtsp/rtsp/UDPPusher.cpp b/netwerk/protocol/rtsp/rtsp/UDPPusher.cpp index 4b29dec64e35..1f3350894c2b 100644 --- a/netwerk/protocol/rtsp/rtsp/UDPPusher.cpp +++ b/netwerk/protocol/rtsp/rtsp/UDPPusher.cpp @@ -27,7 +27,6 @@ #include "prnetdb.h" #include "prerr.h" #include "NetworkActivityMonitor.h" -#include "ClosingService.h" using namespace mozilla::net; @@ -45,7 +44,6 @@ UDPPusher::UDPPusher(const char *filename, unsigned port) } NetworkActivityMonitor::AttachIOLayer(mSocket); - ClosingService::AttachIOLayer(mSocket); PRNetAddr addr; addr.inet.family = PR_AF_INET;