Skip to content

useStreamingUIMessages: infinite re-render loop when a stream has zero deltas #273

@AlexGodard

Description

@AlexGodard

Version: @convex-dev/agent@0.6.1

Reproduction

A streaming message that completes with zero deltas in its accumulated deltas array (e.g. a tool-only response, or a transient state where the stream is registered but no chunks have arrived yet) sends React into an infinite setState loop, throwing the classic Maximum update depth exceeded error.

Stack trace points to useStreamingUIMessages.js:65streams.map$argument_0setMessageState.

Root cause

src/react/useStreamingUIMessages.ts lines 68-78:

for (const stream of streams) {
  const lastDelta = stream.deltas.at(-1);
  const cursor = messageState[stream.streamMessage.streamId]?.cursor;
  if (!cursor) {                               // <-- bug 1
    noNewDeltas = false;
    break;
  }
  if (lastDelta && lastDelta.start >= cursor) { // <-- bug 2
    noNewDeltas = false;
    break;
  }
}
  1. !cursor treats cursor === 0 as "not set yet". But getParts(emptyDeltas, 0) returns cursor: 0, so any stream with no deltas writes messageState[streamId].cursor = 0. Next render the guard re-fires → setMessageState again → infinite loop.

  2. lastDelta.start >= cursor is also too loose: cursor is delta.end after getParts, so when lastDelta.end === lastDelta.start (zero-length delta, which deltas.js explicitly tolerates) the same loop triggers.

Suggested fix

-      if (!cursor) {
+      if (cursor === undefined) {
         noNewDeltas = false;
         break;
       }
-      if (lastDelta && lastDelta.start >= cursor) {
+      if (lastDelta && lastDelta.end > cursor) {
         noNewDeltas = false;
         break;
       }

cursor === undefined only treats truly-unseen streams as needing work, and lastDelta.end > cursor matches the semantics of getParts (cursor = end of last consumed delta).

Patched locally via pnpm patch and confirmed loop stops; happy to send a PR if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions