Fix WebSocket channel leak on React component unmount (fixes laravel/echo#475)#1
Open
JoshSalway wants to merge 1 commit into2.xfrom
Open
Fix WebSocket channel leak on React component unmount (fixes laravel/echo#475)#1JoshSalway wants to merge 1 commit into2.xfrom
JoshSalway wants to merge 1 commit into2.xfrom
Conversation
Move resolveChannelSubscription() call from render phase (useRef initializer) into useEffect, so the channel reference count is only incremented inside the effect lifecycle. Previously, React 18+ Strict Mode (and other scenarios causing multiple render invocations) would increment the count during render without a corresponding cleanup, causing channels to accumulate and never close. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes laravel#475 — WebSocket channels are not properly closed on React component unmount, causing channel leaks, reference counter imbalance, and memory leaks in React SPAs.
Steps to Reproduce
@laravel/echo-reactuseEcho()to subscribe to a channelChannelCreatedfired butChannelRemovednever fires)Before (Bug)
Two issues in the
useEchohook:subscriptionref is initialized withresolveChannelSubscription()during render, then called again inuseEffect, incrementing the counter twice (count = 2)leave()to close the actual WebSocket connection. The counter never reaches 0, soleaveChannel()returns early.After (Fix)
useEffect, not during render initializationnulland is set insideuseEffect. The teardown correctly releases the channel on unmount.Root Cause
React 18+ Strict Mode calls render functions twice, which would create duplicate subscriptions. The original code tried to handle this with an
initializedref but the ref was initialized with a subscription during render, causing the double-subscribe. The cleanup path also didn't fully release the channel.The Fix
subscriptionref initialization fromresolveChannelSubscription()tonullinitializedref — no longer neededuseEffectstopListeningandlistenfor the nullable refuseEchoNotificationBreaking Changes
None.
Files Changed
packages/react/src/hooks/use-echo.ts— Fixed subscription lifecycle (+20/-15)Test Results
59 tests pass. Channels are properly created on mount and released on unmount. No double-subscription in React 18+ Strict Mode.