Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

fix(desktop): retry transient Go dependency downloads

Leggett485667ada99b
diff --git a/.github/workflows/desktop-release.yml b/.github/workflows/desktop-release.yml
index f57851f8..49414a1d 100644
--- a/.github/workflows/desktop-release.yml
+++ b/.github/workflows/desktop-release.yml
@@ -214,7 +214,7 @@ jobs:
with:
path: |
.runtime/build/krillinai/${{ matrix.platform }}-${{ matrix.arch }}
- key: krillinai-build-${{ runner.os }}-${{ matrix.arch }}-${{ hashFiles('scripts/build-krillinai.mjs', 'runtime/krillinai/**') }}
+ key: krillinai-build-${{ runner.os }}-${{ matrix.arch }}-${{ hashFiles('scripts/build-krillinai.mjs', 'scripts/download-go-modules.mjs', 'runtime/krillinai/**') }}
- run: pnpm install --frozen-lockfile
- name: 生成 macOS 正式签名安装包
if: matrix.platform == 'darwin'
diff --git a/apps/desktop/scripts/prepare-creator-runtime.mjs b/apps/desktop/scripts/prepare-creator-runtime.mjs
index 9d9e7cc0..86a76f11 100644
--- a/apps/desktop/scripts/prepare-creator-runtime.mjs
+++ b/apps/desktop/scripts/prepare-creator-runtime.mjs
@@ -118,6 +118,7 @@ const buildRecord = {
},
integrationPatchSha256: hashFiles([
join(rootDir, 'scripts', 'build-krillinai.mjs'),
+ join(rootDir, 'scripts', 'download-go-modules.mjs'),
join(scriptDir, 'install-creator-runtime-dependencies.mjs'),
join(scriptDir, 'creator-runtime-releases.mjs'),
join(scriptDir, 'prepare-creator-runtime.mjs'),
diff --git a/apps/desktop/test/go-module-download.test.mjs b/apps/desktop/test/go-module-download.test.mjs
new file mode 100644
index 00000000..daad225f
--- /dev/null
+++ b/apps/desktop/test/go-module-download.test.mjs
@@ -0,0 +1,67 @@
+import { describe, expect, it, vi } from 'vitest';
+import { downloadGoModules } from '../../../scripts/download-go-modules.mjs';
+
+function downloadError(stderr) {
+ return Object.assign(new Error('go mod download failed'), { status: 1, stderr });
+}
+
+function fixture(execute) {
+ return { cwd: '/source/krillinai', env: { GOPROXY: 'https://proxy.golang.org' }, execute, wait: vi.fn(), report: vi.fn() };
+}
+
+describe('Go module downloads', () => {
+ it('recovers from the observed proxy timeout without retrying compilation', () => {
+ const error = downloadError('go: github.com/aliyun/[email protected]: Get "https://proxy.golang.org/github.com/aliyun/alibaba-cloud-sdk-go/@v/v1.63.72.mod": read tcp 192.168.64.2:49237->142.251.179.141:443: read: operation timed out\n');
+ const execute = vi.fn().mockImplementationOnce(() => { throw error; }).mockReturnValue('');
+ const options = fixture(execute);
+
+ downloadGoModules(options);
+
+ expect(execute).toHaveBeenCalledTimes(2);
+ for (const [command, args, settings] of execute.mock.calls) {
+ expect(command).toBe('go');
+ expect(args).toEqual(['mod', 'download']);
+ expect(settings).toMatchObject({ cwd: options.cwd, env: options.env });
+ }
+ expect(options.wait).toHaveBeenCalledExactlyOnceWith(1_000);
+ expect(options.report).toHaveBeenCalledWith(error.stderr);
+ });
+
+ it('stops after three failed downloads and preserves the final error', () => {
+ const error = downloadError('Get "https://proxy.golang.org/example": i/o timeout');
+ const options = fixture(vi.fn(() => { throw error; }));
+
+ expect(() => downloadGoModules(options)).toThrow(error);
+ expect(options.execute).toHaveBeenCalledTimes(3);
+ expect(options.wait.mock.calls).toEqual([[1_000], [2_000]]);
+ });
+
+ it.each([
+ 'TLS handshake timeout',
+ 'connection reset by peer',
+ 'unexpected EOF',
+ '429 Too Many Requests',
+ '502 Bad Gateway',
+ '503 Service Unavailable'
+ ])('retries transient download errors: %s', message => {
+ const execute = vi.fn().mockImplementationOnce(() => { throw downloadError(message); }).mockReturnValue('');
+ const options = fixture(execute);
+ downloadGoModules(options);
+ expect(execute).toHaveBeenCalledTimes(2);
+ });
+
+ it.each([
+ 'checksum mismatch\nSECURITY ERROR\noperation timed out',
+ 'unknown revision v1.2.3',
+ 'invalid version: example',
+ '404 Not Found\noperation timed out',
+ '403 Forbidden',
+ 'go executable not found'
+ ])('fails immediately for permanent errors: %s', message => {
+ const error = downloadError(message);
+ const options = fixture(vi.fn(() => { throw error; }));
+ expect(() => downloadGoModules(options)).toThrow(error);
+ expect(options.execute).toHaveBeenCalledTimes(1);
+ expect(options.wait).not.toHaveBeenCalled();
+ });
+});
diff --git a/scripts/build-krillinai.mjs b/scripts/build-krillinai.mjs
index 39d12d5c..11aa3292 100644
--- a/scripts/build-krillinai.mjs
+++ b/scripts/build-krillinai.mjs
@@ -14,6 +14,7 @@ import {
} from 'node:fs';
import { dirname, join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
+import { downloadGoModules } from './download-go-modules.mjs';
const scriptPath = fileURLToPath(import.meta.url);
const rootDir = resolve(dirname(scriptPath), '..');
@@ -42,7 +43,11 @@ const version = process.env.OPENCREATOR_VERSION?.trim()
|| JSON.parse(readFileSync(join(rootDir, 'apps', 'desktop', 'package.json'), 'utf8')).version;
const sourceCommit = gitOutput(['rev-parse', 'HEAD']) || 'unknown';
const sourceSha256 = hashSourceTree(sourceRoot);
-const buildRecipeSha256 = hashFile(scriptPath);
+const buildRecipeSha256 = createHash('sha256')
+ .update(readFileSync(scriptPath))
+ .update('\0')
+ .update(readFileSync(join(rootDir, 'scripts', 'download-go-modules.mjs')))
+ .digest('hex');
const testOnly = process.argv.includes('--test-only');
const runTests = testOnly || process.argv.includes('--test');
const targets = [
@@ -55,6 +60,7 @@ if (!existsSync(join(sourceRoot, 'go.mod'))) {
}
if (runTests) {
+ prepareGoModules(nativeGoEnvironment());
runGo(['test', './...'], nativeGoEnvironment());
}
if (testOnly) {
@@ -73,6 +79,7 @@ if (
mkdirSync(binDir, { recursive: true });
mkdirSync(artifactDir, { recursive: true });
+if (!runTests) prepareGoModules(targetGoEnvironment());
const binaries = {};
for (const targetBinary of targets) {
@@ -171,6 +178,10 @@ function runGo(args, env) {
});
}
+function prepareGoModules(env) {
+ downloadGoModules({ cwd: sourceRoot, env: { ...process.env, ...env } });
+}
+
function nativeGoEnvironment() {
return {
GOFLAGS: `${process.env.GOFLAGS ?? ''} -buildvcs=false`.trim()
diff --git a/scripts/download-go-modules.mjs b/scripts/download-go-modules.mjs
new file mode 100644
index 00000000..b26ea8dd
--- /dev/null
+++ b/scripts/download-go-modules.mjs
@@ -0,0 +1,39 @@
+import { execFileSync } from 'node:child_process';
+
+const retryDelays = [1_000, 2_000];
+
+export function downloadGoModules({
+ cwd,
+ env,
+ execute = execFileSync,
+ wait = milliseconds => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds),
+ report = output => process.stderr.write(output)
+}) {
+ for (let attempt = 0; ; attempt += 1) {
+ try {
+ const output = execute('go', ['mod', 'download'], {
+ cwd,
+ env,
+ encoding: 'utf8',
+ stdio: ['ignore', 'pipe', 'pipe'],
+ maxBuffer: 16 * 1024 * 1024
+ });
+ if (output) report(String(output));
+ return;
+ } catch (error) {
+ const output = `${error.stdout ?? ''}${error.stderr ?? ''}`;
+ if (output) report(output);
+ if (attempt >= retryDelays.length || !isTransientDownloadError(output)) throw error;
+ const delay = retryDelays[attempt];
+ report(`Go module download hit a transient network error; retrying in ${delay}ms (${attempt + 2}/3).\n`);
+ wait(delay);
+ }
+ }
+}
+
+function isTransientDownloadError(output) {
+ if (/checksum mismatch|SECURITY ERROR|unknown revision|invalid version|401 Unauthorized|403 Forbidden|404 Not Found|410 Gone/i.test(output)) {
+ return false;
+ }
+ return /operation timed out|i\/o timeout|TLS handshake timeout|connection (?:reset|refused)|network is unreachable|temporary failure in name resolution|temporary network failure|unexpected EOF|429 Too Many Requests|50[0234] (?:Bad Gateway|Service Unavailable|Gateway Timeout|Internal Server Error)/i.test(output);
+}