diff --git a/src/examples/OPFSCoopSyncVFS.js b/src/examples/OPFSCoopSyncVFS.js index 90630e6f..44028251 100644 --- a/src/examples/OPFSCoopSyncVFS.js +++ b/src/examples/OPFSCoopSyncVFS.js @@ -519,17 +519,24 @@ export class OPFSCoopSyncVFS extends FacadeVFS { this._module.retryOps.push((async () => { // Acquire the Web Lock. file.persistentFile.handleLockReleaser = await this.#acquireLock(file.persistentFile); - - // Get access handles for the database and releated files in parallel. - this.log?.(`creating access handles for ${file.path}`) - await Promise.all(DB_RELATED_FILE_SUFFIXES.map(async suffix => { - const persistentFile = this.persistentFiles.get(file.path + suffix); - if (persistentFile) { - persistentFile.accessHandle = - await persistentFile.fileHandle.createSyncAccessHandle(); - } - })); - file.persistentFile.isRequestInProgress = false; + try { + // Get access handles for the database and releated files in parallel. + this.log?.(`creating access handles for ${file.path}`) + await Promise.all(DB_RELATED_FILE_SUFFIXES.map(async suffix => { + const persistentFile = this.persistentFiles.get(file.path + suffix); + if (persistentFile) { + persistentFile.accessHandle = + await persistentFile.fileHandle.createSyncAccessHandle(); + } + })); + } catch (e) { + this.log?.(`failed to create access handles for ${file.path}`, e); + // Close any of the potentially opened access handles + this.#releaseAccessHandle(file); + throw e; + } finally { + file.persistentFile.isRequestInProgress = false; + } })()); return this._module.retryOps.at(-1); } @@ -539,8 +546,8 @@ export class OPFSCoopSyncVFS extends FacadeVFS { /** * @param {File} file */ - async #releaseAccessHandle(file) { - DB_RELATED_FILE_SUFFIXES.forEach(async suffix => { + #releaseAccessHandle(file) { + DB_RELATED_FILE_SUFFIXES.forEach(suffix => { const persistentFile = this.persistentFiles.get(file.path + suffix); if (persistentFile) { persistentFile.accessHandle?.close(); diff --git a/src/sqlite-api.js b/src/sqlite-api.js index 2b3c04f7..a13e68bf 100644 --- a/src/sqlite-api.js +++ b/src/sqlite-api.js @@ -873,8 +873,11 @@ export function Factory(Module) { // Wait for all pending retry operations to complete. This is // normally empty on the first loop iteration. if (Module.retryOps.length) { - await Promise.all(Module.retryOps); - Module.retryOps = []; + try { + await Promise.all(Module.retryOps); + } finally { + Module.retryOps = []; + } } rc = await f();