From 05c09ef769dd5d6589ee9a1889f972022943c94a Mon Sep 17 00:00:00 2001 From: trevor-signal <131492920+trevor-signal@users.noreply.github.com> Date: Wed, 13 Mar 2024 15:57:08 -0400 Subject: [PATCH] Add timeout to DNS lookup --- ts/util/dns.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ts/util/dns.ts b/ts/util/dns.ts index 83ae342fb7e8..fc40191f9c96 100644 --- a/ts/util/dns.ts +++ b/ts/util/dns.ts @@ -9,11 +9,14 @@ import type { } from 'dns'; import { ipcRenderer, net } from 'electron'; import type { ResolvedHost, ResolvedEndpoint } from 'electron'; +import pTimeout from 'p-timeout'; import { strictAssert } from './assert'; import { drop } from './drop'; import type { DNSFallbackType } from '../types/DNSFallback'; +import { SECOND } from './durations'; +const LOOKUP_TIMEOUT_MS = 1 * SECOND; const fallbackAddrs = new Map>(); export function setFallback(dnsFallback: DNSFallbackType): void { @@ -49,15 +52,19 @@ function lookupAll( try { if (net) { // Main process - result = await net.resolveHost(hostname, { - queryType, - }); + result = await pTimeout( + net.resolveHost(hostname, { + queryType, + }), + LOOKUP_TIMEOUT_MS, + 'lookupAll: Electron lookup timed out' + ); } else { // Renderer - result = await ipcRenderer.invoke( - 'net.resolveHost', - hostname, - queryType + result = await pTimeout( + ipcRenderer.invoke('net.resolveHost', hostname, queryType), + LOOKUP_TIMEOUT_MS, + 'lookupAll: Electron lookup timed out' ); } } catch (error) {