Skip to content

Commit adc027e

Browse files
committed
feat(UvBootstrapperService): enhance download management with concurrency control
- Introduce `activeDownloads` set to track ongoing downloads and prevent concurrent downloads of the same version. - Refactor download logic to include defensive checks ensuring no concurrent downloads occur. - Improve error handling during region detection to default to standard UV download sources if detection fails. - Ensure proper cleanup of `activeDownloads` set after download completion or failure. - Enhance logging to provide clearer insights into download initiation and status.
1 parent cd78da2 commit adc027e

1 file changed

Lines changed: 42 additions & 31 deletions

File tree

src/main/services/UvBootstrapperService.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ const PYPI_MIRRORS: PyPiMirror[] = [
193193
export class UvBootstrapperService {
194194
private downloadProgress = new Map<string, DownloadProgress>()
195195
private downloadController = new Map<string, AbortController>()
196+
private activeDownloads = new Set<string>()
196197
private readonly binariesDir: string
197198
private fastestMirror: PyPiMirror | null = null
198199
private mirrorTestPromise: Promise<PyPiMirror> | null = null
@@ -512,45 +513,55 @@ export class UvBootstrapperService {
512513
return true
513514
}
514515

515-
// 仅当请求的平台与当前进程一致时,才额外使用缓存的系统检测结果
516-
if (platform === process.platform && arch === process.arch) {
517-
const installation = await this.checkUvInstallation()
518-
if (installation.exists && installation.isDownloaded) {
519-
logger.info('uv 已存在,跳过下载', { platform, arch, path: installation.path })
520-
return true
521-
}
522-
}
523-
524-
// 检查是否正在下载
525-
if (this.downloadProgress.has(key) || this.downloadController.has(key)) {
516+
if (this.activeDownloads.has(key)) {
526517
logger.warn('uv 正在下载中', { platform, arch })
527518
return false
528519
}
529520

530-
if (this.regionDetectionPromise) {
531-
try {
532-
await this.regionDetectionPromise
533-
} catch (error) {
534-
logger.warn('地区检测失败,将使用默认 UV 下载源', {
535-
error: error instanceof Error ? error.message : String(error)
536-
})
521+
this.activeDownloads.add(key)
522+
try {
523+
// 仅当请求的平台与当前进程一致时,才额外使用缓存的系统检测结果
524+
if (platform === process.platform && arch === process.arch) {
525+
const installation = await this.checkUvInstallation()
526+
if (installation.exists && installation.isDownloaded) {
527+
logger.info('uv 已存在,跳过下载', { platform, arch, path: installation.path })
528+
return true
529+
}
537530
}
538-
}
539531

540-
const version = this.getUvVersion(platform, arch)
541-
if (!version) {
542-
logger.error('不支持的平台', { platform, arch })
543-
return false
544-
}
532+
if (this.regionDetectionPromise) {
533+
try {
534+
await this.regionDetectionPromise
535+
} catch (error) {
536+
logger.warn('地区检测失败,将使用默认 UV 下载源', {
537+
error: error instanceof Error ? error.message : String(error)
538+
})
539+
}
540+
}
545541

546-
logger.info('开始下载 uv', {
547-
platform,
548-
arch,
549-
version: version.version,
550-
url: version.url
551-
})
542+
const version = this.getUvVersion(platform, arch)
543+
if (!version) {
544+
logger.error('不支持的平台', { platform, arch })
545+
return false
546+
}
547+
548+
// 再次确认未有并发下载(防御性检查)
549+
if (this.downloadProgress.has(key) || this.downloadController.has(key)) {
550+
logger.warn('uv 正在下载中', { platform, arch })
551+
return false
552+
}
552553

553-
return await this.performDownload(platform, arch, version, onProgress)
554+
logger.info('开始下载 uv', {
555+
platform,
556+
arch,
557+
version: version.version,
558+
url: version.url
559+
})
560+
561+
return await this.performDownload(platform, arch, version, onProgress)
562+
} finally {
563+
this.activeDownloads.delete(key)
564+
}
554565
}
555566

556567
/**

0 commit comments

Comments
 (0)