--- a/netwerk/base/nsUDPSocket.cpp
+++ b/netwerk/base/nsUDPSocket.cpp
@@ -742,60 +742,75 @@ nsUDPSocket::GetLocalAddr(nsINetAddr * *
result.forget(aResult);
return NS_OK;
}
void
nsUDPSocket::CloseSocket()
{
- if (mFD) {
- if (gIOService->IsNetTearingDown() &&
- ((PR_IntervalNow() - gIOService->NetTearingDownStarted()) >
- gSocketTransportService->MaxTimeForPrClosePref())) {
- // If shutdown last to long, let the socket leak and do not close it.
- UDPSOCKET_LOG(("Intentional leak"));
- } else {
+ if (!mFD) {
+ return;
+ }
+
+ PRFileDesc* fd = mFD;
+ mFD = nullptr;
- PRIntervalTime closeStarted = 0;
- if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
- closeStarted = PR_IntervalNow();
- }
+ if (gIOService->IsNetTearingDown() &&
+ ((PR_IntervalNow() - gIOService->NetTearingDownStarted()) >
+ gSocketTransportService->MaxTimeForPrClosePref())) {
+ // If shutdown last to long, let the socket leak and do not close it.
+ UDPSOCKET_LOG(("Intentional leak"));
+ return;
+ }
- PR_Close(mFD);
+ nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableFunction([fd]() {
+ PRIntervalTime closeStarted = 0;
+ if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
+ closeStarted = PR_IntervalNow();
+ }
+
+ PR_Close(fd);
- if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
- PRIntervalTime now = PR_IntervalNow();
- if (gIOService->IsNetTearingDown()) {
- Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_SHUTDOWN,
- PR_IntervalToMilliseconds(now - closeStarted));
+ if (gSocketTransportService->IsTelemetryEnabledAndNotSleepPhase()) {
+ PRIntervalTime now = PR_IntervalNow();
+ if (gIOService->IsNetTearingDown()) {
+ Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_SHUTDOWN,
+ PR_IntervalToMilliseconds(now - closeStarted));
- } else if (PR_IntervalToSeconds(now - gIOService->LastConnectivityChange())
- < 60) {
- Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_CONNECTIVITY_CHANGE,
- PR_IntervalToMilliseconds(now - closeStarted));
+ } else if (PR_IntervalToSeconds(now - gIOService->LastConnectivityChange())
+ < 60) {
+ Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_CONNECTIVITY_CHANGE,
+ PR_IntervalToMilliseconds(now - closeStarted));
- } else if (PR_IntervalToSeconds(now - gIOService->LastNetworkLinkChange())
- < 60) {
- Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_LINK_CHANGE,
- PR_IntervalToMilliseconds(now - closeStarted));
+ } else if (PR_IntervalToSeconds(now - gIOService->LastNetworkLinkChange())
+ < 60) {
+ Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_LINK_CHANGE,
+ PR_IntervalToMilliseconds(now - closeStarted));
- } else if (PR_IntervalToSeconds(now - gIOService->LastOfflineStateChange())
- < 60) {
- Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_OFFLINE,
- PR_IntervalToMilliseconds(now - closeStarted));
+ } else if (PR_IntervalToSeconds(now - gIOService->LastOfflineStateChange())
+ < 60) {
+ Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_OFFLINE,
+ PR_IntervalToMilliseconds(now - closeStarted));
- } else {
- Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_NORMAL,
- PR_IntervalToMilliseconds(now - closeStarted));
- }
+ } else {
+ Telemetry::Accumulate(Telemetry::PRCLOSE_UDP_BLOCKING_TIME_NORMAL,
+ PR_IntervalToMilliseconds(now - closeStarted));
}
}
- mFD = nullptr;
+ });
+
+ bool onSTSThread;
+ gSocketTransportService->IsOnCurrentThread(&onSTSThread);
+ if (!onSTSThread) {
+ gSocketTransportService->Dispatch(runnable, NS_DISPATCH_NORMAL);
+ return;
}
+
+ runnable->Run();
}
NS_IMETHODIMP
nsUDPSocket::GetAddress(NetAddr *aResult)
{
// no need to enter the lock here
memcpy(aResult, &mAddr, sizeof(mAddr));
return NS_OK;