Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .server-changes/realtimestreams-dedupe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
area: webapp
type: fix
---

Dedupe the `realtimeStreams` array push on `PUT /realtime/v1/streams/:runId/:target/:streamId` so repeat stream-init calls for the same `(run, streamId)` skip the row UPDATE, mirroring the existing append handler.
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,49 @@ const { action } = createActionApiRoute(

if (request.method === "PUT") {
// This is the "create" endpoint
const updatedRun = await prisma.taskRun.update({
const target = await prisma.taskRun.findFirst({
where: {
friendlyId: targetId,
runtimeEnvironmentId: authentication.environment.id,
},
data: {
realtimeStreams: {
push: params.streamId,
},
},
select: {
id: true,
realtimeStreams: true,
realtimeStreamsVersion: true,
completedAt: true,
},
});

if (updatedRun.completedAt) {
if (!target) {
return new Response("Run not found", { status: 404 });
}

if (target.completedAt) {
return new Response("Cannot initialize a realtime stream on a completed run", {
status: 400,
});
}

if (!target.realtimeStreams.includes(params.streamId)) {
await prisma.taskRun.update({
where: { id: target.id },
data: {
realtimeStreams: { push: params.streamId },
},
});
}
Comment on lines +65 to +95
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

Dedup remains race-prone under concurrent PUTs.

Line 88 checks membership and Lines 89-94 push in a separate statement, so two concurrent requests can both pass the check and both append streamId. Please make this check+append atomic at the DB level (single conditional write / lock-scoped transaction) to fully prevent duplicate writes.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/webapp/app/routes/realtime.v1.streams`.$runId.$target.$streamId.ts
around lines 65 - 95, Replace the separate findFirst + update with a single
conditional DB write: perform a prisma.taskRun.updateMany (or
prisma.taskRun.update with a NOT has check) where the record id equals target.id
AND NOT { realtimeStreams: { has: params.streamId } }, and set data: {
realtimeStreams: { push: params.streamId } }; then verify the returned count
(from updateMany) or catch a zero-result to detect "already present" instead of
relying on the prior membership check. This makes the check+append atomic (use
prisma.taskRun.updateMany with where: { id: target.id, NOT: { realtimeStreams: {
has: params.streamId } } } and data: { realtimeStreams: { push: params.streamId
} } and handle count === 0 accordingly).


const realtimeStream = getRealtimeStreamInstance(
authentication.environment,
updatedRun.realtimeStreamsVersion,
target.realtimeStreamsVersion,
basinContext
);

const { responseHeaders } = await realtimeStream.initializeStream(targetId, params.streamId);

return json(
{
version: updatedRun.realtimeStreamsVersion,
version: target.realtimeStreamsVersion,
},
{ status: 202, headers: responseHeaders }
);
Expand Down
Loading