Skip to content

Commit

Permalink
fix: vaults locking and transactional locking
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Aug 11, 2022
1 parent 4c2520b commit 3810cbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
27 changes: 14 additions & 13 deletions src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,24 @@ class VaultInternal {
return this.db.withTransactionF((tran) => this.writeF(f, tran));
}

// This should really be an internal property
// get whether this is remote, and the remote address
// if it is, we consider this repo an "attached repo"
// this vault is a "mirrored" vault
if (
(await tran.get([
...this.vaultMetadataDbPath,
VaultInternal.remoteKey,
])) != null
) {
// Mirrored vaults are immutable
throw new vaultsErrors.ErrorVaultRemoteDefined();
}
return withF([this.lock.write()], async () => {
await tran.lock(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey].toString(),
);

// This should really be an internal property
// get whether this is remote, and the remote address
// if it is, we consider this repo an "attached repo"
// this vault is a "mirrored" vault
if (
(await tran.get([
...this.vaultMetadataDbPath,
VaultInternal.remoteKey,
])) != null
) {
// Mirrored vaults are immutable
throw new vaultsErrors.ErrorVaultRemoteDefined();
}
await tran.put(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey],
true,
Expand Down
14 changes: 8 additions & 6 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,20 +365,22 @@ class VaultManager {
);
}

const vaultMeta = await this.getVaultMeta(vaultId, tran);
if (vaultMeta == null) return;
const vaultName = vaultMeta.vaultName;
this.logger.info(`Destroying Vault ${vaultsUtils.encodeVaultId(vaultId)}`);
const vaultIdString = vaultId.toString() as VaultIdString;
await this.vaultLocks.withF([vaultId, RWLockWriter, 'write'], async () => {
const vaultMeta = await this.getVaultMeta(vaultId, tran);
if (vaultMeta == null) return;
const vaultName = vaultMeta.vaultName;
await tran.lock([...this.vaultsNamesDbPath, vaultName].toString());
this.logger.info(
`Destroying Vault ${vaultsUtils.encodeVaultId(vaultId)}`,
);
const vaultIdString = vaultId.toString() as VaultIdString;
const vault = await this.getVault(vaultId, tran);
// Destroying vault state and metadata
await vault.stop();
await vault.destroy(tran);
// Removing from map
this.vaultMap.delete(vaultIdString);
// Removing name->id mapping
await tran.lock([...this.vaultsNamesDbPath, vaultName].toString());
await tran.del([...this.vaultsNamesDbPath, vaultName]);
});
this.logger.info(`Destroyed Vault ${vaultsUtils.encodeVaultId(vaultId)}`);
Expand Down

0 comments on commit 3810cbb

Please sign in to comment.