Skip to content

Commit cc105b9

Browse files
graciegould“graciegould”
andauthored
fix(resolveModelFile): deduplicate concurrent downloads (#570)
Co-authored-by: “graciegould” <“graciegould5@gmail.com”>
1 parent 5a44506 commit cc105b9

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

src/utils/resolveModelFile.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import fs from "fs-extra";
33
import chalk from "chalk";
4+
import {acquireLock} from "lifecycle-utils";
45
import {cliModelsDirectory} from "../config.js";
56
import {getReadablePath} from "../cli/utils/getReadablePath.js";
67
import {resolveSplitGgufParts} from "../gguf/utils/resolveSplitGgufParts.js";
@@ -12,7 +13,6 @@ import {genericFilePartNumber} from "./parseModelUri.js";
1213
import {isFilePartText} from "./parseModelFileName.js";
1314
import {pushAll} from "./pushAll.js";
1415

15-
1616
export type ResolveModelFileOptions = {
1717
/**
1818
* The directory to resolve models from, and download models to.
@@ -270,19 +270,29 @@ export async function resolveModelFile(
270270
}
271271
}
272272

273-
if (resolvedCli)
274-
console.info(`Downloading to ${chalk.yellow(getReadablePath(resolvedDirectory))}${
275-
downloader.splitBinaryParts != null
276-
? chalk.gray(` (combining ${downloader.splitBinaryParts} parts into a single file)`)
277-
: ""
278-
}`);
273+
const lock = await acquireLock([resolveModelFile, "download", downloader.entrypointFilePath]);
274+
try {
275+
if (await fs.pathExists(downloader.entrypointFilePath)) {
276+
await downloader.cancel({deleteTempFile: false});
277+
return downloader.entrypointFilePath;
278+
}
279279

280-
await downloader.download({signal});
280+
if (resolvedCli)
281+
console.info(`Downloading to ${chalk.yellow(getReadablePath(resolvedDirectory))}${
282+
downloader.splitBinaryParts != null
283+
? chalk.gray(` (combining ${downloader.splitBinaryParts} parts into a single file)`)
284+
: ""
285+
}`);
281286

282-
if (resolvedCli)
283-
console.info(`Downloaded to ${chalk.yellow(getReadablePath(downloader.entrypointFilePath))}`);
287+
await downloader.download({signal});
284288

285-
return downloader.entrypointFilePath;
289+
if (resolvedCli)
290+
console.info(`Downloaded to ${chalk.yellow(getReadablePath(downloader.entrypointFilePath))}`);
291+
292+
return downloader.entrypointFilePath;
293+
} finally {
294+
lock.dispose();
295+
}
286296
}
287297

288298
async function findMatchingFilesInDirectory(dirPath: string, fileNames: (string | `${string}${typeof genericFilePartNumber}${string}`)[]) {

0 commit comments

Comments
 (0)