Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(media): harden media database writes (#14137)

Hassieb Pakzad7c7853a4a1a5
diff --git a/web/src/features/media/server/mediaService.ts b/web/src/features/media/server/mediaService.ts
index df8ff7a315..5a832b4587 100644
--- a/web/src/features/media/server/mediaService.ts
+++ b/web/src/features/media/server/mediaService.ts
@@ -218,12 +218,14 @@ async function upsertMediaRecord(params: {
contentType,
contentLength,
} = params;
- const maxRetries = 3;
- const delayMs = 100;
- for (let retryCount = 0; retryCount < maxRetries; retryCount += 1) {
- try {
- await prisma.$queryRaw`
+ // Media has two unique constraints: (project_id, id) for the public mediaId
+ // and (project_id, sha_256_hash) for content dedupe. Under concurrent uploads
+ // of the same file, either unique index can observe the speculative insert
+ // first. Omitting the conflict target makes DO NOTHING absorb conflicts from
+ // both constraints; the guarded Prisma update below then proves that the
+ // existing row has both the expected mediaId and full hash before reusing it.
+ await prisma.$executeRaw`
INSERT INTO "media" (
"id",
"project_id",
@@ -242,22 +244,27 @@ async function upsertMediaRecord(params: {
${contentType},
${contentLength}
)
- ON CONFLICT ("project_id", "sha_256_hash")
- DO UPDATE SET
- "bucket_name" = ${uploadBucket},
- "bucket_path" = ${bucketPath},
- "content_type" = ${contentType},
- "content_length" = ${contentLength}
+ ON CONFLICT DO NOTHING
`;
- return;
- } catch (error) {
- if (retryCount === maxRetries - 1) throw error;
- logger.debug(
- `Failed to create media record. Retrying (${retryCount + 1}/${maxRetries})...`,
- );
- await new Promise((resolve) => setTimeout(resolve, delayMs));
- }
+ const result = await prisma.media.updateMany({
+ where: {
+ projectId,
+ id: mediaId,
+ sha256Hash,
+ },
+ data: {
+ bucketName: uploadBucket,
+ bucketPath,
+ contentType,
+ contentLength: BigInt(contentLength),
+ },
+ });
+
+ if (result.count === 0) {
+ throw new InternalServerError(
+ `Media ID collision detected for media ID ${mediaId} in project ${projectId}. The existing media row has a different id or sha_256_hash.`,
+ );
}
}
@@ -272,18 +279,18 @@ async function linkMediaToTraceOrObservation(params: {
if (observationId) {
await prisma.$queryRaw`
- INSERT INTO "observation_media" ("id", "project_id", "trace_id", "observation_id", "media_id", "field")
- VALUES (${randomUUID()}, ${projectId}, ${traceId}, ${observationId}, ${mediaId}, ${field})
- ON CONFLICT DO NOTHING;
- `;
+ INSERT INTO "observation_media" ("id", "project_id", "trace_id", "observation_id", "media_id", "field")
+ VALUES (${randomUUID()}, ${projectId}, ${traceId}, ${observationId}, ${mediaId}, ${field})
+ ON CONFLICT DO NOTHING;
+ `;
return;
}
await prisma.$queryRaw`
- INSERT INTO "trace_media" ("id", "project_id", "trace_id", "media_id", "field")
- VALUES (${randomUUID()}, ${projectId}, ${traceId}, ${mediaId}, ${field})
- ON CONFLICT DO NOTHING;
- `;
+ INSERT INTO "trace_media" ("id", "project_id", "trace_id", "media_id", "field")
+ VALUES (${randomUUID()}, ${projectId}, ${traceId}, ${mediaId}, ${field})
+ ON CONFLICT DO NOTHING;
+ `;
}
function getBucketPath(params: {