Skip to content

fix: avoid scene initialization after Strict Mode cleanup - #53

Open
akn966 wants to merge 1 commit into
diegopeixoto:mainfrom
akn966:fix/strict-mode-init-race
Open

akn966 wants to merge 1 commit into
diegopeixoto:mainfrom
akn966:fix/strict-mode-init-race

Conversation

@akn966

@akn966 akn966 commented Aug 21, 2026 •

Copy link
Copy Markdown

Summary

  • defer scene initialization by one microtask so React Strict Mode can clean up its discarded effect before addScene() starts
  • skip initialization when that effect has already been cleaned up
  • add a Strict Mode regression test that verifies the SDK initializes exactly once

Why

In a Next.js App Router application, navigating client-side to a route containing a scene could trigger this SDK error:

TypeError: Cannot read properties of null (reading 'canvas')
    at setInitialFlattenedPlaneUniforms

The first Strict Mode effect started addScene() synchronously. React then cleaned up that discarded effect and the wrapper destroyed the scene, while asynchronous SDK initialization callbacks were still running. Those callbacks later accessed the destroyed scene's null curtain.

Yielding one microtask before touching the SDK lets the Strict Mode cleanup set ignore, preventing the discarded effect from starting a scene at all.

Checks

  • npm test — 124 passed
  • npm run lint
  • npm run type-check
  • npm run build

Summary by CodeRabbit

  • Bug Fixes
    • Improved scene initialization reliability when the app is rendered under React Strict Mode.
    • Prevented scenes from being initialized more than once or unintentionally destroyed during startup.
    • Improved handling when scene properties change while initialization is still in progress.

@coderabbitai

coderabbitai Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9c089e95-5c09-48c1-9a6b-e1a15ac35f0d

📥 Commits

Reviewing files that changed from the base of the PR and between cee51fb and e4b3f93.

📒 Files selected for processing (2)
  • src/__tests__/useUnicornScene.test.ts
  • src/shared/hooks.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The scene initialization hook now waits for one microtask before SDK startup. It skips initialization when Strict Mode cleanup invalidates the effect. Tests cover single initialization, scene retention, and pending rerender behavior.

Changes

Scene initialization

Layer / File(s) Summary
Strict Mode initialization guard
src/shared/hooks.ts, src/__tests__/useUnicornScene.test.ts
initializeScene yields before SDK startup and exits when cleanup occurs during the delay. Tests verify that Strict Mode does not duplicate addScene, does not destroy the created scene, and flushes pending initialization before rerendering.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to e4b3f

The change defers scene initialization to avoid starting discarded Strict Mode effects and adds regression coverage; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: diegopeixoto

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the fix that prevents scene initialization after React Strict Mode cleanup.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@diegopeixoto

Copy link
Copy Markdown
Owner

@GeorgeHastings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants