[PR #304] [MERGED] fix: infinite re-render loop in Game Mode (React error #185) #4553

Closed
opened 2026-10-22 04:08:39 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/304
Author: @kevin-ho
Created: 4/29/2026
Status: ✅ Merged
Merged: 4/29/2026
Merged by: @SpicyMarinara

Base: main ← Head: fix/infinite-rerender-patchNpcAvatars


📝 Commits (4)

  • 1afcba2 fix: prevent infinite re-render loop in Game Mode (React error #185)
  • 1a6c2a4 chore: trigger CI re-run for template check
  • df0a4ad Merge branch 'main' into fix/infinite-rerender-patchNpcAvatars
  • 41bae44 refactor: composite speaker dedupe key and fix useRef type annotation

📊 Changes

2 files changed (+33 additions, -11 deletions)

View changed files

📝 packages/client/src/components/game/GameNarration.tsx (+21 -10)
📝 packages/client/src/stores/game-mode.store.ts (+12 -1)

📄 Description

Linked issue

Closes #305

Why this change

Opening any Game Mode chat session immediately crashes the UI with React error #185 (Maximum update depth exceeded). The bug prevents all Game Mode gameplay — users cannot use GM chats at all.

Root cause: patchNpcAvatars in the Zustand store uses .map() on the npcs array every time it's called, even when nothing was actually modified. .map() always returns a new array reference, so Zustand sees "state changed" and notifies all subscribers. A useEffect in GameSurface calls patchNpcAvatars during React's commit phase, which triggers a synchronous re-render via useSyncExternalStore, re-fires the effect, calls patchNpcAvatars again — and the loop continues until React hits its 50-render depth limit.

GM chats are specifically affected because they load additional state (NPCs, character library data via TanStack Query) that feeds into the useEffect dependencies, guaranteeing the loop on first mount.

What changed

  • packages/client/src/stores/game-mode.store.ts — patchNpcAvatars now tracks whether any NPC was actually modified. If nothing changed, it returns the same state reference (return s) instead of a new object. Zustand performs reference equality checking — when the reference is identical, it skips subscriber notification entirely, breaking the infinite loop.
  • packages/client/src/components/game/GameNarration.tsx — Added a useRef guard to the onActiveSpeakerChange useEffect. The effect now tracks the last reported speaker name and bails out if it hasn't changed. This prevents the same class of bug (redundant setActiveSpeaker calls) from a different code path where dependency references churn.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  1. Built the client with pnpm --filter @marinara-engine/client build — clean build, no TypeScript errors
  2. Rebuilt and restarted the production server on port 7860
  3. Opened the app in Chrome, hard-refreshed (Ctrl+Shift+R) to bust service worker cache
  4. Navigated to the GM chat (the specific chat that triggered the original crash with backgrounds:generated:korriban-valley)
  5. Game Mode loaded successfully — no React error #185, no infinite loop, UI fully interactive
  6. Also verified CONVO and RP chats still work normally (no regressions)

Docs and release impact

  • No docs changes needed

UI evidence (if applicable)

Before: Opening any GM chat immediately shows the red React error overlay (error #185) and the tab becomes unresponsive.

After: GM chats load normally with full Game Mode UI rendering. No console errors.

Summary by CodeRabbit

  • Refactor
    • Reduced repeated narration updates so dialogue speaker changes are reported only when the visible speaker actually changes, leading to steadier narration behavior.
    • Prevented unnecessary NPC avatar refreshes by keeping existing avatar data when unchanged, reducing visual flicker and improving game responsiveness and performance.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/304 **Author:** [@kevin-ho](https://github.com/kevin-ho) **Created:** 4/29/2026 **Status:** ✅ Merged **Merged:** 4/29/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `main` ← **Head:** `fix/infinite-rerender-patchNpcAvatars` --- ### 📝 Commits (4) - [`1afcba2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1afcba2c249d80c28f583886f70278702161572d) fix: prevent infinite re-render loop in Game Mode (React error #185) - [`1a6c2a4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1a6c2a4e5591de2b8917115d38ced16c3ab1d17e) chore: trigger CI re-run for template check - [`df0a4ad`](https://github.com/Pasta-Devs/Marinara-Engine/commit/df0a4ad53c155f3a6aad69c5427b27b2eb5f3da6) Merge branch 'main' into fix/infinite-rerender-patchNpcAvatars - [`41bae44`](https://github.com/Pasta-Devs/Marinara-Engine/commit/41bae44aac82e6b6579d3e830be8ccc3fbd2071d) refactor: composite speaker dedupe key and fix useRef type annotation ### 📊 Changes **2 files changed** (+33 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/game/GameNarration.tsx` (+21 -10) 📝 `packages/client/src/stores/game-mode.store.ts` (+12 -1) </details> ### 📄 Description ## Linked issue Closes #305 ## Why this change Opening any Game Mode chat session immediately crashes the UI with React error #185 (`Maximum update depth exceeded`). The bug prevents all Game Mode gameplay — users cannot use GM chats at all. **Root cause:** `patchNpcAvatars` in the Zustand store uses `.map()` on the `npcs` array every time it's called, even when nothing was actually modified. `.map()` always returns a **new array reference**, so Zustand sees "state changed" and notifies all subscribers. A `useEffect` in `GameSurface` calls `patchNpcAvatars` during React's commit phase, which triggers a synchronous re-render via `useSyncExternalStore`, re-fires the effect, calls `patchNpcAvatars` again — and the loop continues until React hits its 50-render depth limit. GM chats are specifically affected because they load additional state (NPCs, character library data via TanStack Query) that feeds into the `useEffect` dependencies, guaranteeing the loop on first mount. ## What changed - **`packages/client/src/stores/game-mode.store.ts`** — `patchNpcAvatars` now tracks whether any NPC was actually modified. If nothing changed, it returns the **same state reference** (`return s`) instead of a new object. Zustand performs reference equality checking — when the reference is identical, it skips subscriber notification entirely, breaking the infinite loop. - **`packages/client/src/components/game/GameNarration.tsx`** — Added a `useRef` guard to the `onActiveSpeakerChange` `useEffect`. The effect now tracks the last reported speaker name and bails out if it hasn't changed. This prevents the same class of bug (redundant `setActiveSpeaker` calls) from a different code path where dependency references churn. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes 1. Built the client with `pnpm --filter @marinara-engine/client build` — clean build, no TypeScript errors 2. Rebuilt and restarted the production server on port 7860 3. Opened the app in Chrome, hard-refreshed (Ctrl+Shift+R) to bust service worker cache 4. Navigated to the GM chat (the specific chat that triggered the original crash with `backgrounds:generated:korriban-valley`) 5. Game Mode loaded successfully — no React error #185, no infinite loop, UI fully interactive 6. Also verified CONVO and RP chats still work normally (no regressions) ## Docs and release impact - [x] No docs changes needed ## UI evidence (if applicable) Before: Opening any GM chat immediately shows the red React error overlay (error #185) and the tab becomes unresponsive. After: GM chats load normally with full Game Mode UI rendering. No console errors. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Reduced repeated narration updates so dialogue speaker changes are reported only when the visible speaker actually changes, leading to steadier narration behavior. * Prevented unnecessary NPC avatar refreshes by keeping existing avatar data when unchanged, reducing visual flicker and improving game responsiveness and performance. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:08:39 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4553
No description provided.