Skip to content

Fix racing conditions in Postgres streamer#1002

Merged
VaguelySerious merged 3 commits into
vercel:mainfrom
rovo89:fix/postgres-stream-racing-conditions
Feb 23, 2026
Merged

Fix racing conditions in Postgres streamer#1002
VaguelySerious merged 3 commits into
vercel:mainfrom
rovo89:fix/postgres-stream-racing-conditions

Conversation

@rovo89
Copy link
Copy Markdown
Contributor

@rovo89 rovo89 commented Feb 11, 2026

I'm currently building a chat using DurableAgent and frequently (almost for every message) had missing chunks. Often the text-start was missing or some of the words were lost. The chunks are persisted totally fine.

Investigations have shown that the chunks were dismissed as out-of-order. Indeed, they arrived at the listen handler in a different order than they were processed in writeToStreamMulti(). Since making the proposed changes - especially awaiting the `postgres.notify()´ calls - I haven't seen a single missing chunk.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 11, 2026

🦋 Changeset detected

Latest commit: b4708fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@workflow/world-postgres Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Feb 11, 2026

@rovo89 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Signed-off-by: Robert Vollmer <rovo89@users.noreply.github.com>
While it might technically work, it hands control back to the Postgres
module very early and then relies on microtask scheduling to ensure the
correct order. But after parsing the message, the function already uses
a mutex for correct ordering of chunks within a stream, so each chunk
is queued twice. Parsing synchronously avoids that back-and-forth and
makes the code flow easier to follow.

Signed-off-by: Robert Vollmer <rovo89@users.noreply.github.com>
Signed-off-by: Robert Vollmer <rovo89@users.noreply.github.com>
@rovo89 rovo89 force-pushed the fix/postgres-stream-racing-conditions branch from 3ac3271 to b4708fb Compare February 21, 2026 17:05
@rovo89
Copy link
Copy Markdown
Contributor Author

rovo89 commented Feb 21, 2026

Rebased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants