Problem
When disconnecting, the code calls socketManager.disconnect() and sets the socket state to null, but it does not explicitly remove event listeners attached to the socket instance.
Why it matters
Remaining listeners can hold references and memory, cause unexpected callbacks if socket instances persist or reconnect, and result in resource leaks.
Affected files
- src/components/providers/SocketProvider.tsx (cleanup in useEffect and disconnect)
- src/services/socketManager.ts (disconnect implementation)
Suggested fix
- Ensure registered listeners (connect, disconnect, connect_error, reconnect, etc.) are removed when disconnecting (socket.off(...)).
- Prefer that SocketManager manages both registration and teardown of handlers it creates. Implement a handler registry to remove them on disconnect.
- Add unit tests validating no listeners remain after disconnect.
Notes
This is especially important for SPA environments that may mount/unmount providers repeatedly.
Problem
When disconnecting, the code calls socketManager.disconnect() and sets the socket state to null, but it does not explicitly remove event listeners attached to the socket instance.
Why it matters
Remaining listeners can hold references and memory, cause unexpected callbacks if socket instances persist or reconnect, and result in resource leaks.
Affected files
Suggested fix
Notes
This is especially important for SPA environments that may mount/unmount providers repeatedly.