Bug 1884628 - Revert Bug 1853497, r=necko-reviewers,valentin

Differential Revision: https://phabricator.services.mozilla.com/D204199
This commit is contained in:
Kershaw Chang 2024-03-11 15:13:24 +00:00
Родитель 73e160f12c
Коммит a1ec0a1e69
3 изменённых файлов: 51 добавлений и 65 удалений

Просмотреть файл

@ -1304,9 +1304,6 @@ nsDNSService::Observe(nsISupports* subject, const char* topic,
nsAutoCString converted = NS_ConvertUTF16toUTF8(data);
if (!strcmp(converted.get(), NS_NETWORK_LINK_DATA_CHANGED)) {
flushCache = true;
if (mResolver) {
mResolver->ResetDNSConfigurations();
}
}
} else if (!strcmp(topic, "last-pb-context-exited")) {
flushCache = true;

Просмотреть файл

@ -10,6 +10,7 @@
# include <arpa/inet.h>
# include <arpa/nameser.h>
# include <resolv.h>
# define RES_RETRY_ON_FAILURE
#endif
#include <stdlib.h>
@ -44,9 +45,6 @@
#include "mozilla/Telemetry.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Preferences.h"
#if defined(HAVE_RES_NINIT)
# include "mozilla/RWLock.h"
#endif
#include "mozilla/StaticPrefs_network.h"
// Put DNSLogging.h at the end to avoid LOG being overwritten by other headers.
#include "DNSLogging.h"
@ -100,6 +98,46 @@ LazyLogModule gHostResolverLog("nsHostResolver");
//----------------------------------------------------------------------------
#if defined(RES_RETRY_ON_FAILURE)
// this class represents the resolver state for a given thread. if we
// encounter a lookup failure, then we can invoke the Reset method on an
// instance of this class to reset the resolver (in case /etc/resolv.conf
// for example changed). this is mainly an issue on GNU systems since glibc
// only reads in /etc/resolv.conf once per thread. it may be an issue on
// other systems as well.
class nsResState {
public:
nsResState()
// initialize mLastReset to the time when this object
// is created. this means that a reset will not occur
// if a thread is too young. the alternative would be
// to initialize this to the beginning of time, so that
// the first failure would cause a reset, but since the
// thread would have just started up, it likely would
// already have current /etc/resolv.conf info.
: mLastReset(PR_IntervalNow()) {}
bool Reset() {
// reset no more than once per second
if (PR_IntervalToSeconds(PR_IntervalNow() - mLastReset) < 1) {
return false;
}
mLastReset = PR_IntervalNow();
auto result = res_ninit(&_res);
LOG(("nsResState::Reset() > 'res_ninit' returned %d", result));
return (result == 0);
}
private:
PRIntervalTime mLastReset;
};
#endif // RES_RETRY_ON_FAILURE
class DnsThreadListener final : public nsIThreadPoolListener {
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSITHREADPOOLLISTENER
@ -143,12 +181,6 @@ static void DnsPrefChanged(const char* aPref, void* aSelf) {
}
}
#if defined(HAVE_RES_NINIT)
static mozilla::StaticRWLock sGetAddrInfoLock;
static mozilla::Atomic<bool, mozilla::Relaxed> sResInitCalled{false};
static mozilla::Atomic<PRIntervalTime> sLastReset{PR_IntervalNow()};
#endif
NS_IMPL_ISUPPORTS0(nsHostResolver)
nsHostResolver::nsHostResolver(uint32_t maxCacheEntries,
@ -200,7 +232,8 @@ nsresult nsHostResolver::Init() MOZ_NO_THREAD_SAFETY_ANALYSIS {
// during application startup.
static int initCount = 0;
if (initCount++ > 0) {
ResetDNSConfigurations();
auto result = res_ninit(&_res);
LOG(("nsHostResolver::Init > 'res_ninit' returned %d", result));
}
#endif
@ -245,43 +278,6 @@ nsresult nsHostResolver::Init() MOZ_NO_THREAD_SAFETY_ANALYSIS {
return NS_OK;
}
#if defined(HAVE_RES_NINIT)
static void DoResetDNSConfigurations() {
// reset no more than once per second
if (PR_IntervalToSeconds(PR_IntervalNow() - sLastReset) < 1) {
return;
}
sLastReset = PR_IntervalNow();
StaticAutoWriteLock lock(sGetAddrInfoLock);
if (sResInitCalled) {
res_nclose(&_res);
sResInitCalled = false;
}
auto result = res_ninit(&_res);
LOG(("DoResetDNSConfigurations 'res_ninit' returned %d", result));
if (result == 0) {
sResInitCalled = true;
}
}
#endif
void nsHostResolver::ResetDNSConfigurations() {
#if defined(HAVE_RES_NINIT)
LOG(("nsHostResolver::ResetDNSConfigurations"));
if (NS_IsMainThread()) {
NS_DispatchBackgroundTask(
NS_NewRunnableFunction("nsHostResolver::ResetDNSConfigurations",
[]() { DoResetDNSConfigurations(); }));
return;
}
DoResetDNSConfigurations();
#endif
}
void nsHostResolver::ClearPendingQueue(
LinkedList<RefPtr<nsHostRecord>>& aPendingQ) {
// loop through pending queue, erroring out pending lookups.
@ -1807,18 +1803,12 @@ size_t nsHostResolver::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const {
return n;
}
#if defined(HAVE_RES_NINIT)
static nsresult GetAddrInfoLocked(const nsACString& aHost,
uint16_t aAddressFamily, uint16_t aFlags,
AddrInfo** aAddrInfo, bool aGetTtl) {
StaticAutoReadLock lock(sGetAddrInfoLock);
return GetAddrInfo(aHost, aAddressFamily, aFlags, aAddrInfo, aGetTtl);
}
#endif
void nsHostResolver::ThreadFunc() {
LOG(("DNS lookup thread - starting execution.\n"));
#if defined(RES_RETRY_ON_FAILURE)
nsResState rs;
#endif
RefPtr<nsHostRecord> rec;
RefPtr<AddrInfo> ai;
@ -1858,12 +1848,13 @@ void nsHostResolver::ThreadFunc() {
continue;
}
#if defined(HAVE_RES_NINIT)
nsresult status = GetAddrInfoLocked(rec->host, rec->af, rec->flags,
getter_AddRefs(ai), getTtl);
#else
nsresult status =
GetAddrInfo(rec->host, rec->af, rec->flags, getter_AddRefs(ai), getTtl);
#if defined(RES_RETRY_ON_FAILURE)
if (NS_FAILED(status) && rs.Reset()) {
status = GetAddrInfo(rec->host, rec->af, rec->flags, getter_AddRefs(ai),
getTtl);
}
#endif
mozilla::glean::networking::dns_native_count

Просмотреть файл

@ -222,8 +222,6 @@ class nsHostResolver : public nsISupports, public AHostResolver {
// available/enabled.
bool TRRServiceEnabledForRecord(nsHostRecord* aRec) MOZ_REQUIRES(mLock);
void ResetDNSConfigurations();
private:
explicit nsHostResolver(uint32_t maxCacheEntries,
uint32_t defaultCacheEntryLifetime,