Skip to content

Commit e81db43

Browse files
committed
fix(MongoBinary): ensure lockfile gets unlocked in case of error
1 parent 4258eb7 commit e81db43

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

packages/mongodb-memory-server-core/src/util/MongoBinary.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,27 @@ export class MongoBinary {
6262
const lock = await LockFile.lock(lockfile);
6363
log('getDownloadPath: Download lock acquired');
6464

65-
// check cache if it got already added to the cache
66-
if (!this.cache.get(version)) {
67-
log(`getDownloadPath: Adding version ${version} to cache`);
68-
const downloader = new MongoBinaryDownload({
69-
downloadDir,
70-
platform,
71-
arch,
72-
version,
73-
checkMD5,
74-
});
75-
this.cache.set(version, await downloader.getMongodPath());
65+
// this is to ensure that the lockfile gets removed in case of an error
66+
try {
67+
// check cache if it got already added to the cache
68+
if (!this.cache.has(version)) {
69+
log(`getDownloadPath: Adding version ${version} to cache`);
70+
const downloader = new MongoBinaryDownload({
71+
downloadDir,
72+
platform,
73+
arch,
74+
version,
75+
checkMD5,
76+
});
77+
this.cache.set(version, await downloader.getMongodPath());
78+
}
79+
} finally {
80+
log('getDownloadPath: Removing Download lock');
81+
// remove lock
82+
await lock.unlock();
83+
log('getDownloadPath: Download lock removed');
7684
}
7785

78-
log('getDownloadPath: Removing Download lock');
79-
// remove lock
80-
await lock.unlock();
81-
log('getDownloadPath: Download lock removed');
82-
8386
const cachePath = this.cache.get(version);
8487
// ensure that "path" exists, so the return type does not change
8588
assertion(

0 commit comments

Comments
 (0)