Skip to content

fix(react): remove typing status listener on unmount to prevent memor… - #1454

Open
swarnendusahu10-dotcom wants to merge 1 commit into
RocketChat:developfrom
swarnendusahu10-dotcom:fix/typing-status-listener-memory-leak
Open

swarnendusahu10-dotcom wants to merge 1 commit into
RocketChat:developfrom
swarnendusahu10-dotcom:fix/typing-status-listener-memory-leak

Conversation

@swarnendusahu10-dotcom

Copy link
Copy Markdown
Contributor

Summary

Fixes an orphaned event listener and memory leak in the TypingUsers component (packages/react/src/views/TypingUsers/TypingUsers.js).

Problem

In TypingUsers.js, useEffect subscribed to real-time typing events using an anonymous inline arrow function:

RCInstance.addTypingStatusListener((t) => {
  setTypingUsers((t || []).filter((u) => u !== currentUserName));
});

However, the cleanup function attempted to unregister using the React state setter:

return () => RCInstance.removeTypingStatusListener(setTypingUsers);

Inside EmbeddedChatApi.ts, removeTypingStatusListener filters callbacks by exact reference equality (c !== callback). Because the anonymous arrow function is referentially distinct from setTypingUsers, the registered callback was never removed.

Every time TypingUsers remounted (such as opening and closing the AttachmentPreview modal or switching channels/threads), an orphaned listener accumulated in RCInstance.onTypingStatusCallbacks. Subsequent typing events triggered state updates on unmounted component instances, leading to memory leaks and React console warnings.

Solution

  1. Reference Matching: Stored the listener in a dedicated function reference (handleTypingStatus) so that the exact same function reference passed to addTypingStatusListener is passed to removeTypingStatusListener during cleanup.
  2. Defensive Safe Navigation: Added optional chaining guards (RCInstance?.addTypingStatusListener and RCInstance?.removeTypingStatusListener) to prevent null reference errors if RCInstance is uninitialized or mocked in tests.
  3. Array Sanitization: Guarded user filtering with u && u !== currentUserName to eliminate falsy or empty string elements from typing indicator bubbles.
  4. Dependency Cleanup: Removed setTypingUsers from the useEffect dependency array, keeping only [RCInstance, currentUserName], eliminating redundant re-subscriptions.
  5. Lint Cleanup: Removed the unused useTheme import and unused theme variable to ensure zero ESLint warnings.

Testing

  • Verified component mount and unmount lifecycles; verified that onTypingStatusCallbacks cleanly removes the callback upon unmount.
  • Ran eslint src/views/TypingUsers/TypingUsers.js: 0 errors, 0 warnings.
  • Ran yarn build: Rollup build completed successfully with 0 errors.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant