A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(desktop): abort pending connection attempts on timeout
Hampus37ee515cd904
diff --git a/fluxer_desktop/src/main/DesktopHappyEyeballs.ts b/fluxer_desktop/src/main/DesktopHappyEyeballs.tsindex 538cb896b..a655e70d1 100644--- a/fluxer_desktop/src/main/DesktopHappyEyeballs.ts+++ b/fluxer_desktop/src/main/DesktopHappyEyeballs.ts@@ -1,5 +1,6 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import type {LookupAddress, LookupAllOptions} from 'node:dns';import http from 'node:http';import https from 'node:https';import net, {type LookupFunction} from 'node:net';@@ -14,25 +15,26 @@ interface ConnectAddress {}type ConnectCallback = (error: Error | null, socket: net.Socket | null) => void;-type Dial = (options: net.TcpNetConnectOpts) => net.Socket;interface RaceOptions {- readonly attemptDelayMs?: number;- readonly dial?: Dial;+ readonly connectionSignal?: AbortSignal;readonly keepAlive?: boolean;readonly keepAliveInitialDelay?: number;readonly noDelay?: boolean;}type CreatedConnectionCallback = (error: Error | null, stream: Duplex) => void;--interface HappyEyeballsConnectOptions {+type ConnectionCompletion = (...args: [error: Error, stream: undefined] | [error: null, stream: Duplex]) => void;+type LookupAll = (+ hostname: string,+ options: LookupAllOptions,+ callback: (error: NodeJS.ErrnoException | null, addresses: Array<LookupAddress>) => void,+) => void;++interface HappyEyeballsConnectOptions extends RaceOptions {readonly host?: string | null;readonly hostname?: string | null;- readonly keepAlive?: boolean;- readonly keepAliveInitialDelay?: number;readonly lookup?: LookupFunction;- readonly noDelay?: boolean;readonly port?: number | string | null;}@@ -73,14 +75,12 @@ function interleaveFamilies(addresses: ReadonlyArray<ConnectAddress>): ReadonlyAreturn ordered;}-export function connectToFirstAnsweringAddress(+function connectToFirstAnsweringAddress(addresses: ReadonlyArray<ConnectAddress>,port: number,options: RaceOptions,callback: ConnectCallback,): void {- const attemptDelayMs = options.attemptDelayMs ?? HAPPY_EYEBALLS_ATTEMPT_DELAY_MS;- const dial = options.dial ?? net.connect;const ordered = interleaveFamilies(addresses);const pending = new Set<net.Socket>();const errors: Array<Error> = [];@@ -92,6 +92,7 @@ export function connectToFirstAnsweringAddress(return;}settled = true;+ options.connectionSignal?.removeEventListener('abort', onAbort);clearTimeout(attemptTimer);clearTimeout(deadline);for (const socket of pending) {@@ -102,6 +103,11 @@ export function connectToFirstAnsweringAddress(pending.clear();callback(error, winner);};+ const onAbort = (): void => {+ const reason: unknown = options.connectionSignal?.reason;+ const error = reason instanceof Error ? reason : new DOMException('The operation was aborted', 'AbortError');+ finish(error, null);+ };const startNext = (): void => {clearTimeout(attemptTimer);if (settled || next >= ordered.length) {@@ -109,7 +115,7 @@ export function connectToFirstAnsweringAddress(}const {address, family} = ordered[next];next += 1;- const socket = dial({+ const socket = net.connect({family,host: address,keepAlive: options.keepAlive,@@ -136,7 +142,7 @@ export function connectToFirstAnsweringAddress(finish(null, socket);});if (next < ordered.length) {- attemptTimer = setTimeout(startNext, attemptDelayMs);+ attemptTimer = setTimeout(startNext, HAPPY_EYEBALLS_ATTEMPT_DELAY_MS);}};const deadline = setTimeout(() => {@@ -144,6 +150,11 @@ export function connectToFirstAnsweringAddress(finish(connectFailure(errors), null);}, HAPPY_EYEBALLS_CONNECT_TIMEOUT_MS);deadline.unref();+ options.connectionSignal?.addEventListener('abort', onAbort, {once: true});+ if (options.connectionSignal?.aborted === true) {+ onAbort();+ return;+ }if (ordered.length === 0) {finish(connectTimeoutError(), null);return;@@ -151,21 +162,26 @@ export function connectToFirstAnsweringAddress(startNext();}-function connectThroughLookup(options: HappyEyeballsConnectOptions, callback: ConnectCallback): void {- const lookup = options.lookup as LookupFunction;+function connectThroughLookup(+ options: HappyEyeballsConnectOptions,+ lookup: LookupFunction,+ callback: ConnectCallback,+): void {+ const lookupAll = lookup as LookupAll;const hostname = options.host ?? options.hostname ?? '';- lookup(hostname, {all: true}, (error, resolved) => {+ lookupAll(hostname, {all: true}, (error, resolved) => {if (error != null) {callback(error, null);return;}- const addresses = (resolved as unknown as ReadonlyArray<{address: string; family: number}>).map(+ const addresses = resolved.map((entry): ConnectAddress => ({address: entry.address, family: entry.family === 6 ? 6 : 4}),);connectToFirstAnsweringAddress(addresses,Number(options.port),{+ connectionSignal: options.connectionSignal,keepAlive: options.keepAlive,keepAliveInitialDelay: options.keepAliveInitialDelay,noDelay: options.noDelay,@@ -183,12 +199,13 @@ export class HappyEyeballsHttpAgent extends http.Agent {if (options.lookup == null || callback == null) {return super.createConnection(options as http.ClientRequestArgs, callback);}- connectThroughLookup(options, (error, socket) => {+ const complete = callback as ConnectionCompletion;+ connectThroughLookup(options, options.lookup, (error, socket) => {if (error != null || socket == null) {- callback(error ?? connectTimeoutError(), undefined as unknown as Duplex);+ complete(error ?? connectTimeoutError(), undefined);return;}- callback(null, socket);+ complete(null, socket);});return undefined;}@@ -202,12 +219,13 @@ export class HappyEyeballsHttpsAgent extends https.Agent {if (options.lookup == null || callback == null) {return super.createConnection(options as http.ClientRequestArgs, callback);}- connectThroughLookup(options, (error, socket) => {+ const complete = callback as ConnectionCompletion;+ connectThroughLookup(options, options.lookup, (error, socket) => {if (error != null || socket == null) {- callback(error ?? connectTimeoutError(), undefined as unknown as Duplex);+ complete(error ?? connectTimeoutError(), undefined);return;}- callback(null, super.createConnection({...options, socket} as http.ClientRequestArgs) as Duplex);+ complete(null, super.createConnection({...options, socket} as http.ClientRequestArgs) as Duplex);});return undefined;}diff --git a/fluxer_desktop/src/main/DesktopOutboundHTTP.ts b/fluxer_desktop/src/main/DesktopOutboundHTTP.tsindex bd7698d9f..7a4c0aba3 100644--- a/fluxer_desktop/src/main/DesktopOutboundHTTP.ts+++ b/fluxer_desktop/src/main/DesktopOutboundHTTP.ts@@ -376,13 +376,9 @@ function absoluteLookupHostname(hostname: string): string {return hostname;}-export function createPinnedHostLookup(- hostname: string,- pinned: PinnedAddress | ReadonlyArray<PinnedAddress>,-): LookupFunction {+function createPinnedHostLookup(hostname: string, pinned: ReadonlyArray<PinnedAddress>): LookupFunction {const expected = normalizeLookupHostname(hostname);- const addresses: ReadonlyArray<PinnedAddress> = Array.isArray(pinned) ? pinned : [pinned as PinnedAddress];- const primary = addresses[0];+ const primary = pinned[0];return (requestedHostname, options, callback) => {process.nextTick(() => {if (normalizeLookupHostname(requestedHostname) !== expected) {@@ -390,7 +386,7 @@ export function createPinnedHostLookup(return;}if (options.all === true) {- (callback as unknown as (error: null, addresses: ReadonlyArray<PinnedAddress>) => void)(null, addresses);+ callback(null, [...pinned]);return;}callback(null, primary.address, primary.family);@@ -921,13 +917,18 @@ export class DesktopOutboundHTTP {return new Promise<DesktopOutboundHTTPMessage>((resolve, reject) => {const secure = request.url.protocol === 'https:';const transport = secure ? https : http;- const clientRequest = transport.request(request.url, {+ const connection = new AbortController();+ const connectionOptions = {agent: secure ? this.httpsAgent : this.httpAgent,+ connectionSignal: connection.signal,lookup: createPinnedHostLookup(request.url.hostname, pinned),method: 'GET',- });+ };+ const clientRequest = transport.request(request.url, connectionOptions);const deadline = setTimeout(() => {- clientRequest.destroy(new DesktopOutboundHTTPTimeoutError());+ const error = new DesktopOutboundHTTPTimeoutError();+ clientRequest.destroy(error);+ connection.abort(error);}, request.timeoutMs);deadline.unref();let settled = false;@@ -1072,6 +1073,11 @@ export class DesktopOutboundHTTP {let attemptsRemaining = isReplayableRequest(request) ? DESKTOP_OUTBOUND_HTTP_STALE_SOCKET_ATTEMPTS : 0;let active: http.ClientRequest | null = null;let terminated = false;+ const connection = new AbortController();+ const destroyActive = (error: Error): void => {+ active?.destroy(error);+ connection.abort(error);+ };const releaseUnconnectedBinding = (): void => {const socket = active?.socket;if (socket == null || socket.connecting) {@@ -1081,22 +1087,19 @@ export class DesktopOutboundHTTP {const timeout = setTimeout(() => {terminated = true;releaseUnconnectedBinding();- active?.destroy(new DesktopOutboundHTTPRequestTimeoutError(target.toString(), request.timeoutMs));+ destroyActive(new DesktopOutboundHTTPRequestTimeoutError(target.toString(), request.timeoutMs));}, request.timeoutMs);timeout.unref();const onAbort = (): void => {terminated = true;releaseUnconnectedBinding();- active?.destroy(new DesktopOutboundHTTPRequestAbortedError(target.toString()));+ destroyActive(new DesktopOutboundHTTPRequestAbortedError(target.toString()));};request.signal?.addEventListener('abort', onAbort, {once: true});const settle = (): void => {clearTimeout(timeout);request.signal?.removeEventListener('abort', onAbort);};- const destroyActive = (error: Error): void => {- active?.destroy(error);- };if (request.signal?.aborted === true) {settle();reject(new DesktopOutboundHTTPRequestAbortedError(target.toString()));@@ -1104,12 +1107,14 @@ export class DesktopOutboundHTTP {}const attempt = (): void => {const transport = target.protocol === 'https:' ? https : http;- const clientRequest = transport.request(target, {+ const connectionOptions = {agent: target.protocol === 'https:' ? this.originRequestHttpsAgent : this.originRequestHttpAgent,+ connectionSignal: connection.signal,headers: requestHeaders(request.headers),lookup: createPinnedHostLookup(target.hostname, binding.addresses),method: request.method,- });+ };+ const clientRequest = transport.request(target, connectionOptions);active = clientRequest;clientRequest.on('error', (error) => {if (