From b135752577f5d4cd0720fa9acf138fa4ac107062 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 22 Jun 2026 01:44:26 +0800 Subject: [PATCH] fix(highway): retry chunk upload on transient transport errors (#118) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Large private files (e.g. 48 MB .flac via send_private_msg) failed with "connection closed before response (received 0 bytes, headerSeen=false)". uploadHighwayHttp sends one fresh TCP connection per 1 MB chunk with no retry, so a 48 MB file is ~48 sequential connections and a single transient peer-FIN / reset among them aborted the entire upload. Small files (1 chunk) always worked; NapCat is unaffected because it delegates to QQ's native highway client, which retries. Wrap each chunk's connect + POST in a bounded retry (3 attempts, 300 ms × attempt backoff, fresh connection each time). Re-sending the same offset range is idempotent server-side. Only transport failures retry; a decoded highway error_code is a definitive server reject and is not retried. --- .../protocol/src/highway/highway-client.ts | 54 ++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/packages/protocol/src/highway/highway-client.ts b/packages/protocol/src/highway/highway-client.ts index 7455e08a..98bdf173 100644 --- a/packages/protocol/src/highway/highway-client.ts +++ b/packages/protocol/src/highway/highway-client.ts @@ -18,6 +18,17 @@ const log = createLogger('Highway'); const HIGHWAY_APP_ID = 1600001604; const HIGHWAY_BLOCK_SIZE = 1024 * 1024; +// Per-chunk transport retry. A large file uploads as many sequential +// one-shot TCP connections; QQ's highway edge nodes (and proxies/NAT on the +// path) sporadically FIN a fresh connection before responding, so a single +// transient close among a 48 MB file's ~48 chunks must not abort the whole +// upload (issue #118). Only transport failures retry — a decoded highway +// error_code is a definitive server reject and is never retried. +const HIGHWAY_MAX_CHUNK_ATTEMPTS = 3; +const HIGHWAY_RETRY_BASE_MS = 300; + +const sleepMs = (ms: number): Promise => new Promise((resolve) => setTimeout(resolve, ms)); + export const PRIVATE_IMAGE_CMD_ID = 1003; export const GROUP_IMAGE_CMD_ID = 1004; @@ -261,14 +272,45 @@ export async function uploadHighwayHttp( ); const frame = packHighwayFrame(head, chunk); - const socket = await tcpConnect(session.host, session.port); - let responseBody: Uint8Array; - try { - responseBody = await httpPostFrame(socket, session.host, pathStr, frame); - } finally { - socket.destroy(); + // Retry the connect + POST for this chunk on transient transport errors + // (peer FIN before response, ECONNRESET, connect refused/timeout). Each + // attempt uses a fresh connection; re-sending the same offset range is + // idempotent on the server. + let responseBody: Uint8Array | undefined; + for (let attempt = 1; ; attempt++) { + let socket: net.Socket; + try { + socket = await tcpConnect(session.host, session.port); + } catch (err) { + if (attempt >= HIGHWAY_MAX_CHUNK_ATTEMPTS) { + throw new Error( + `highway connect failed after ${attempt} attempts ` + + `(cmdId=${commandId} offset=${offset}/${bytes.length}): ${String(err)}`, + ); + } + await sleepMs(HIGHWAY_RETRY_BASE_MS * attempt); + continue; + } + try { + responseBody = await httpPostFrame(socket, session.host, pathStr, frame); + break; + } catch (err) { + if (attempt >= HIGHWAY_MAX_CHUNK_ATTEMPTS) { + throw new Error( + `highway upload transport failed after ${attempt} attempts ` + + `(cmdId=${commandId} chunk=${chunkSize}/${bytes.length} offset=${offset}): ${String(err)}`, + ); + } + log.trace('chunk offset=%d attempt %d failed (%s), retrying', offset, attempt, String(err)); + await sleepMs(HIGHWAY_RETRY_BASE_MS * attempt); + } finally { + socket.destroy(); + } } + // Unreachable: the retry loop only exits via break (responseBody set) or + // throw — the guard just narrows the type for the compiler. + if (!responseBody) throw new Error('highway upload: missing response'); const { head: respHead } = unpackHighwayFrame(responseBody); const resp = protobuf_decode(respHead); if (resp?.errorCode && resp.errorCode !== 0) {