Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

chore: address read after write issue for secret folders

Sheen Capadngan8d9fde292182
diff --git a/backend/src/services/secret-folder/secret-folder-service.ts b/backend/src/services/secret-folder/secret-folder-service.ts
index 033efdb14c..46c26c1117 100644
--- a/backend/src/services/secret-folder/secret-folder-service.ts
+++ b/backend/src/services/secret-folder/secret-folder-service.ts
@@ -231,19 +231,19 @@ export const secretFolderServiceFactory = ({
tx
);
- return doc;
- });
+ const [folderWithFullPath] = await folderDAL.findSecretPathByFolderIds(projectId, [doc.id], tx);
- const [folderWithFullPath] = await folderDAL.findSecretPathByFolderIds(projectId, [folder.id]);
+ if (!folderWithFullPath) {
+ throw new NotFoundError({
+ message: `Failed to retrieve path for folder with ID '${doc.id}'`
+ });
+ }
- if (!folderWithFullPath) {
- throw new NotFoundError({
- message: `Failed to retrieve path for folder with ID '${folder.id}'`
- });
- }
+ return { ...doc, path: folderWithFullPath.path };
+ });
await snapshotService.performSnapshot(folder.parentId as string);
- return { ...folder, path: folderWithFullPath.path };
+ return folder;
};
const updateManyFolders = async ({
@@ -461,7 +461,7 @@ export const secretFolderServiceFactory = ({
}
}
- const newFolder = await folderDAL.transaction(async (tx) => {
+ const { newFolder, newFolderPath, oldFolderPath } = await folderDAL.transaction(async (tx) => {
const [doc] = await folderDAL.update(
{ envId: env.id, id: folder.id, parentId: parentFolder.id, isReserved: false },
{ name, description },
@@ -498,30 +498,33 @@ export const secretFolderServiceFactory = ({
tx
);
if (!doc) throw new NotFoundError({ message: `Failed to update folder with ID '${id}'`, name: "UpdateFolder" });
- return doc;
- });
- const foldersWithFullPaths = await folderDAL.findSecretPathByFolderIds(projectId, [newFolder.id, folder.id]);
+ // Read the folder paths inside the transaction to avoid replication lag issues
+ // when using read replicas. The transaction ensures we read from the primary.
+ const foldersWithFullPaths = await folderDAL.findSecretPathByFolderIds(projectId, [doc.id, folder.id], tx);
- const newFolderWithFullPath = foldersWithFullPaths.find((f) => f?.id === newFolder.id);
- if (!newFolderWithFullPath) {
- throw new NotFoundError({
- message: `Failed to retrieve path for folder with ID '${newFolder.id}'`
- });
- }
+ const newFolderWithFullPath = foldersWithFullPaths.find((f) => f?.id === doc.id);
+ if (!newFolderWithFullPath) {
+ throw new NotFoundError({
+ message: `Failed to retrieve path for folder with ID '${doc.id}'`
+ });
+ }
- const folderWithFullPath = foldersWithFullPaths.find((f) => f?.id === folder.id);
- if (!folderWithFullPath) {
- throw new NotFoundError({
- message: `Failed to retrieve path for folder with ID '${folder.id}'`
- });
- }
+ const folderWithFullPath = foldersWithFullPaths.find((f) => f?.id === folder.id);
+ if (!folderWithFullPath) {
+ throw new NotFoundError({
+ message: `Failed to retrieve path for folder with ID '${folder.id}'`
+ });
+ }
+
+ return { newFolder: doc, newFolderPath: newFolderWithFullPath.path, oldFolderPath: folderWithFullPath.path };
+ });
await snapshotService.performSnapshot(newFolder.parentId as string);
await secretV2BridgeDAL.invalidateSecretCacheByProjectId(projectId);
return {
- folder: { ...newFolder, path: newFolderWithFullPath.path },
- old: { ...folder, path: folderWithFullPath.path }
+ folder: { ...newFolder, path: newFolderPath },
+ old: { ...folder, path: oldFolderPath }
};
};