ChatbotX icon indicating copy to clipboard operation
ChatbotX copied to clipboard

Flow editor: `useInboxList()` returns a new `[]` on every render while the inbox query is pending or failed, so `NodeEditorMenu`'s effect loops into "Maximum update depth exceeded"

Open rawdaymx opened this issue 2 months ago • 1 comments

Summary

Since #1265 (fdddbf157, inbox stores → TanStack Query), useInboxList() returns data ?? []. While the inbox query has no data — still pending, or failed — every render gets a new empty array. NodeEditorMenu (the "add step" menu in the flow editor's node panel) keeps that array in the dependency list of a useEffect that calls setNodeMenus(...) every time it runs. New array → effect runs → state update → re-render → new array → … The panel never settles, and React reports:

Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
src/features/flows/react-flow/nodes/editor.tsx (181:9) @ NodeEditorMenu.useEffect

(The line number is from the build we observed it on, which has a few extra lines in that file. On d44f6b48c the same setNodeMenus( call is at editor.tsx:177.)

The same effect is fine once the query has data: data is then the cached array, so its identity stays the same between renders. That is the control. The only thing that matters is whether data is undefined, and that happens in two situations: the request is still pending, or it failed and the query stays in error with no data. In the second case the loop has no end.

Environment

Kind of defect Static (code). Measured by reading and running the repository, not a deployed instance
upstream/main d44f6b48c (fetched 2026-09-23), where every file:line below comes from
Introduced by fdddbf157, #1265. Before it, NodeEditorMenu read useInboxStore((s) => s.inboxes), a stored array whose identity was stable
Browser observation Chromium via Playwright, next dev, on a build that includes upstream 7e35593f5 (after #1265, before #1266). The inbox list response was deliberately held back while the node panel was opened
Unit reproduction apps/builder/src/features/inboxes/provider/__tests__/inbox-hook.test.tsx on d44f6b48c, vitest + jsdom

How to see it

  1. Open a flow in the editor, with the inbox list request (listAllInboxesAuthenticatedAPI) still in flight (slow network, cold cache), or failing (for example, the endpoint returns 5xx).
  2. Open the detail panel of a node whose type has a menu (for example, a send-message node).

Expected: the panel renders its "add step" menu once. Inbox-dependent entries fill in when the list arrives. What we observed in the browser: the panel does not stay open, and the dev overlay shows the Maximum update depth exceeded error above, pointing at NodeEditorMenu.useEffect.

Evidence

Three regression tests (in the linked PR) run against d44f6b48c without the fix:

 FAIL  src/features/inboxes/provider/__tests__/inbox-hook.test.tsx > useInboxList > returns the same empty array across renders while the request is pending
AssertionError: expected false to be true // Object.is equality
 FAIL  src/features/inboxes/provider/__tests__/inbox-hook.test.tsx > useInboxList > a consumer effect keyed on the list settles while the request is pending
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times
 FAIL  src/features/inboxes/provider/__tests__/inbox-hook.test.tsx > useInboxList > a consumer effect keyed on the list settles after the request fails
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times
 Test Files  1 failed (1)
      Tests  3 failed | 17 passed (20)

The same tests with the fix:

 Test Files  1 passed (1)
      Tests  20 passed (20)

The two "settles" tests mount a probe with the same shape as NodeEditorMenu: useInboxList() feeds an effect that unconditionally sets state. The probe counts its own renders and reports when it passes 20. We do not wait for React to throw. Under jsdom, a passive-effect loop does not throw. It keeps allocating a new array on every pass until the worker runs out of heap. We saw that happen before we added the ceiling.

Where it comes from

apps/builder/src/features/inboxes/provider/inbox-hook.ts:68-72 (at d44f6b48c):

export const useInboxList = (options?: { enabled?: boolean }) => {
  const workspaceId = useWorkspaceId()
  const { data } = useInboxes(workspaceId, options)
  return data ?? []
}

apps/builder/src/features/flows/react-flow/nodes/editor.tsx:166-196, in NodeEditorMenu:

const inboxes = useInboxList()
// ...
useEffect(() => {
  const nodeConfig = nodeType ? allNodesConfig[nodeType]?.(t) : null
  if (nodeConfig) {
    setNodeMenus(nodeConfig.menus(t, { inboxes, /* ... */ }))
  } else {
    setNodeMenus([])
  }
}, [nodeType, t, inboxes, whatsappTemplates, whatsappFlows, messengerTemplates, beforeStep])

Both branches of the effect set a new array, so every run schedules another render. With inboxes changing identity on every render, it never stops.

The seven derived hooks in the same file (useConfiguredInboxTypeOptions, useInboxOptionsByChannel, useInboxOptionsForChannels, useWhatsappInboxOptions, useMessengerInboxOptions, useSmtpInboxOptions, useSmtpInboxFromAddressMap) wrap useInboxList() in a useMemo keyed on it. For now that only means they recompute on every render while the query has no data, which is wasted work but not a loop. It also means their results change identity on every render too. So any future consumer that keys an effect on one of them hits the same loop.

Why we think this is a defect rather than the intended design

  • Before #1265 the list came from a zustand selector over a stored array, so its identity was stable across renders. NodeEditorMenu's effect was written against that contract, and #1265 changed it without changing the effect.
  • #1266 already treated identity as part of these hooks' contract: it wrapped useInvalidateInboxes in useCallback and added "keeps the invalidator stable across renders" to this same test file. The list returned by useInboxList did not get the same treatment.
  • We searched apps/builder/src on d44f6b48c for a test or comment that makes the fresh-array fallback intentional (Maximum update depth, infinite loop, render loop, stable reference, referentially) and found none. We used setQueryData as the control search: it matches in 5+ files, so the search itself works.

Suggested fix

One shared empty array for the fallback, so the identity only changes when the data does:

const EMPTY_INBOXES: NonNullable<ReturnType<typeof useInboxes>["data"]> = []

export const useInboxList = (options?: { enabled?: boolean }) => {
  const workspaceId = useWorkspaceId()
  const { data } = useInboxes(workspaceId, options)
  return data ?? EMPTY_INBOXES
}

This is the only behaviour change: the returned array keeps the same identity while there is no data. Contents are unchanged. It also stabilises the seven derived hooks above for free. A PR with this change and the regression tests follows (linked below).

What we did not verify

  • The browser run was on 7e35593f5, not on d44f6b48c. On d44f6b48c the defect is established by the unit tests above and by reading the code. We checked git diff 7e35593f5 d44f6b48c on both files. Only fa6ea3119 (#1266) touches them, and only in imports, useInvalidateInboxes, and NodeEditor's custom-field read. useInboxList and NodeEditorMenu are byte-for-byte the same.
  • We did not measure how often a real user hits the pending window. The failed-request case needs the inbox endpoint to fail. We reproduced that in the unit test, not against a real backend.
  • Related pattern, already gone on main: on 7e35593f5 the same fresh-[] pattern also looped in contact-detail.tsx (useCustomFields → customFieldMap → effect with setContactFields) and in ContactCustomFieldManage (useCustomFieldSelectOptions → effect with setOptions). #1266 restructured both, so neither loops on d44f6b48c. useCustomFieldSelectOptions still falls back to fresh arrays (custom-field-hook.ts:284-285), and NodeEditor does the same with useCustomFields(...).data ?? [] (editor.tsx:244). We found no current consumer that feeds either one into a state-setting effect, so we left them out of this issue and the PR. They are the same latent shape.

Related issues — none of these describes it

  • #1158 / PR #1211: about the flow editor not telling "inboxes failed" apart from "no inboxes". #1211 targets the pre-#1265 zustand store (useInboxStore), which no longer exists, and does not touch the identity of the fallback array.
  • Searched open and closed issues and PRs for Maximum update depth, useInboxList, NodeEditorMenu, infinite loop, render loop: no match. We used the same query with flowId as the control, and it does return results.

rawdaymx avatar Sep 23 '26 15:09 rawdaymx

Fix with the three regression tests: #1298.

rawdaymx avatar Sep 23 '26 15:09 rawdaymx