Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/proud-dingos-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/world-postgres": patch
---

Fix racing conditions in Postgres streamer
10 changes: 4 additions & 6 deletions packages/world-postgres/src/streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export function createStreamer(postgres: Sql, drizzle: Drizzle): Streamer {

const STREAM_TOPIC = 'workflow_event_chunk';
postgres.listen(STREAM_TOPIC, async (msg) => {
const parsed = await Promise.resolve(msg)
.then(JSON.parse)
.then(StreamPublishMessage.parse);
const parsed = StreamPublishMessage.parse(JSON.parse(msg));

const key = `strm:${parsed.streamId}` as const;
if (!events.listenerCount(key)) {
Expand Down Expand Up @@ -108,7 +106,7 @@ export function createStreamer(postgres: Sql, drizzle: Drizzle): Streamer {
chunkData: toBuffer(chunk),
eof: false,
});
postgres.notify(
await postgres.notify(
STREAM_TOPIC,
JSON.stringify(
StreamPublishMessage.encode({
Expand Down Expand Up @@ -145,7 +143,7 @@ export function createStreamer(postgres: Sql, drizzle: Drizzle): Streamer {

// Notify for each chunk (could be batched in future if needed)
for (const chunkId of chunkIds) {
postgres.notify(
await postgres.notify(
STREAM_TOPIC,
JSON.stringify(
StreamPublishMessage.encode({
Expand All @@ -171,7 +169,7 @@ export function createStreamer(postgres: Sql, drizzle: Drizzle): Streamer {
chunkData: Buffer.from([]),
eof: true,
});
postgres.notify(
await postgres.notify(
'workflow_event_chunk',
JSON.stringify(
StreamPublishMessage.encode({
Expand Down