Skip to content

Prevent duplicate socket reconnects caused by accumulated token refresh listeners #196

Description

@MiniduOshan

I can Fix This..Please assign me

Describe the bug

MessageContext registers token refresh listeners whenever a new socket instance is created, but the listeners are never removed during cleanup or component unmount.

After multiple login/logout cycles or repeated remounts, multiple listeners remain active. As a result, a single token refresh event can trigger multiple socket reconnects and duplicate event handling.

Affected file:

  • frontend/src/app/MessageContext.tsx

To Reproduce

  1. Start the frontend and backend servers
  2. Log in to the application
  3. Navigate between pages or trigger socket reconnects
  4. Log out and log in again multiple times
  5. Trigger a token refresh event (or wait for automatic token refresh)
  6. Observe multiple reconnect attempts or duplicated socket events

Expected behavior

Only one token refresh listener should exist for the active socket lifecycle.

A token refresh event should:

  • trigger a single reconnect attempt
  • Avoid duplicate socket event handling
  • properly clean up listeners during unmount/logout

Actual behaviour

Listeners accumulate over time because cleanup does not unregister them.

This can lead to:

  • duplicate reconnect attempts
  • stale socket instances
  • repeated message/event handling
  • increased memory usage

Possible Fix

  • Remove token refresh listeners during cleanup/unmount
  • Ensure listener registration happens only once per socket lifecycle
  • Add unsubscribe handling for addTokenRefreshListener

Additional context

Relevant section:

  • frontend/src/app/MessageContext.tsx

Potential impact becomes more visible after:

  • repeated authentication changes
  • hot reloads during development
  • prolonged app sessions
  • reconnect-heavy network conditions

Suggested labels

  • bug
  • frontend
  • websocket
  • performance

Metadata

Metadata

Assignees

No one assigned

    Labels

    gssoc:approvedApproved for GirlScript Summer of Code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions