Skip to content

[Bug]: WebSocket stale closure causes failed cleanup and zombie connections on reconnect #80

@Rozerxshashank

Description

@Rozerxshashank

Bug Description

In useRemoteConnection.ts , the WebSocket is stored using useState. The useEffect cleanup function captures the initial state value (null), so when the component unmounts, ws?.close() does nothing so it never references the actual socket.

This means the socket stays open, and any pending reconnect timers continue firing even after the component is gone, creating zombie connections.

Proposed Solution:

Replace useState with useRef for the WebSocket instance, A ref is always up to date, so cleanup can reliably close the real socket. Add an isMounted flag to block reconnection after unmount, and remove ws from the trackpad.tsx sendCombo dependency arrays since refs don't need them.

Steps to Reproduce

  1. Open the Trackpad page (WebSocket connects)
  2. Stop the dev server to trigger a disconnect
  3. While the 3-second reconnect timer is pending, navigate away from the page
  4. Restart the server, orphaned "Client connected" logs appear from zombie sockets

Expected Behavior

The WebSocket should be properly closed on unmount, and no reconnection attempts should happen after the component is gone.

Desktop Operating System

Windows 11

Mobile Device (Client)

Samsung Galaxy A56

Rein Version

No response

Console Logs / Error Messages

Screenshots / Recordings

No response

Checklist

  • I have searched for existing issues to avoid duplicates

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions