Skip to content

Commit 947318a

Browse files
committed
Fix race condition in message debouncer
Guard against empty debouncedMessages array which caused "Cannot read properties of undefined (reading 'message_id')" error when concurrent requests cleared the debouncer before retrieval. Also add .wrangler and .turbo to biome ignore list.
1 parent e659c58 commit 947318a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

apps/interaction-worker/src/services/loop/loop-message-inbound-handler.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const waitFor = (ms: number): Promise<void> =>
2525
export const handleMessageInbound = async (
2626
options: MessageInboundHandlerOptions,
2727
): Promise<void> => {
28-
const { payload, doNamespace, db, ctx, env } = options;
28+
const { payload, doNamespace, db, env } = options;
2929

3030
if (payload.alert_type !== "message_inbound") {
3131
throw new Error(
@@ -65,6 +65,18 @@ export const handleMessageInbound = async (
6565
messageIds: debouncedMessages.map((m) => m.message_id),
6666
});
6767

68+
// Guard against empty debounced messages (race condition or cleared state)
69+
if (debouncedMessages.length === 0) {
70+
logger.warn(
71+
"Debounced messages empty - possible race condition or cleared state",
72+
{
73+
conversationId,
74+
currentMessageId: payload.message_id,
75+
},
76+
);
77+
return;
78+
}
79+
6880
// Check if we're the last message (should process)
6981
const lastMessage = debouncedMessages[debouncedMessages.length - 1];
7082

biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"useIgnoreFile": false
77
},
88
"files": {
9-
"ignoreUnknown": false
9+
"ignoreUnknown": false,
10+
"includes": ["**", "!.wrangler", "!.turbo"]
1011
},
1112
"formatter": {
1213
"enabled": true,

0 commit comments

Comments
 (0)