fix: resolve WebSocket stale closure bug in useRemoteConnection hook#81
fix: resolve WebSocket stale closure bug in useRemoteConnection hook#81Rozerxshashank wants to merge 1 commit intoAOSSIE-Org:mainfrom
Conversation
📝 WalkthroughWalkthroughFixed a WebSocket lifecycle bug in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
Fixes #80
Problem:
The useRemoteConnection hook stored the WebSocket instance using
useState. TheuseEffectcleanup function captured the initial state value (null) via closure, sows?.close()on unmount never actually closed the real socket. This caused:WebSocket is closed before the connection is establishedbrowser warnings during navigation (React Strict Mode double-mounting)Fix useRemoteConnection.ts :
useState<WebSocket>withuseRef<WebSocket>so cleanup always accesses the current socketisMountedflag to prevent reconnection attempts after unmountsetTimeout(connect, 0)to avoid creating sockets during React Strict Mode's instant unmount cycle.close()to prevent cascading error eventssendandsendCombocallbacks with empty dependency arrays since ref identity never changesChecklist