Skip to content

react: schedule transport close as microtask to avoid strict mode issues#57

Merged
ttypic merged 2 commits intomainfrom
fix-react-dev-double-unmount
Apr 16, 2026
Merged

react: schedule transport close as microtask to avoid strict mode issues#57
ttypic merged 2 commits intomainfrom
fix-react-dev-double-unmount

Conversation

@ttypic
Copy link
Copy Markdown
Contributor

@ttypic ttypic commented Apr 16, 2026

The problem in the use-chat demo app is actually on Vercel's side and I am going to raise an issue there. In production mode everything works fine. The issue is that in strict mode in development, React does a fake mount-unmount, which results in new transport creation and old transport disposal, although useChat continues using the old transport (that is already closed).

Changed TransportProvider to schedule the transport close as a microtask on unmount, ensuring React's Strict Mode remount cycle cancels the close during development.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 16, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 92.81% 2117 / 2281
🔵 Statements 91.09% 2262 / 2483
🔵 Functions 93.28% 389 / 417
🔵 Branches 77.8% 950 / 1221
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/react/contexts/transport-provider.tsx 92.85% 75% 100% 100% 53, 71
Generated in workflow #149 for commit 11b6413 by the Vitest Coverage Report Action

@ttypic ttypic force-pushed the fix-react-dev-double-unmount branch from 6e4aa98 to 412c19e Compare April 16, 2026 11:23
@github-actions github-actions bot temporarily deployed to staging/pull/57/typedoc April 16, 2026 11:24 Inactive
@ttypic ttypic force-pushed the fix-react-dev-double-unmount branch from 412c19e to ad9a2d3 Compare April 16, 2026 11:25
@github-actions github-actions bot temporarily deployed to staging/pull/57/typedoc April 16, 2026 11:26 Inactive
Changed `TransportProvider` to schedule the transport close as a microtask on unmount, ensuring React's Strict Mode remount cycle cancels the close during development. Updated corresponding test to verify behavior.
@ttypic ttypic force-pushed the fix-react-dev-double-unmount branch from ad9a2d3 to 2740a29 Compare April 16, 2026 11:29
@github-actions github-actions bot temporarily deployed to staging/pull/57/typedoc April 16, 2026 11:29 Inactive
Copy link
Copy Markdown
Contributor

@kaschula kaschula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I have tested it locally as well and its working.

I had to do some. research into how react in strict behaves and how your fix works but I now get it.

@ttypic ttypic enabled auto-merge (rebase) April 16, 2026 13:18
@ttypic ttypic merged commit f3112f7 into main Apr 16, 2026
12 checks passed
@ttypic ttypic deleted the fix-react-dev-double-unmount branch April 16, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants