---
issue: 061
title: "Frontend socket context helpers accumulate listeners without dedup — memory/event leaks"
severity: high
domain: Realtime
labels: [bug, frontend, memory-leak]
status: open
created: 2026-05-30
source: Full Codebase Audit 2026-05-30
---

# Frontend socket context helpers accumulate listeners without dedup — memory/event leaks

**Severity:** high
**Domain:** Realtime
**Labels:** bug, frontend, memory-leak

## Description

Socket subscription helpers in `src/socket/contexts/socket-context.tsx:244` do not return unsubscribe functions and do not call `socket.off` when consumers unmount. Each React effect re-registration adds a new listener without removing the old one, causing duplicate event callbacks and memory leaks.

## Options

1. Have each `on*` helper return an unsubscribe function and require consumers to call it in effect cleanup.
2. Make helpers stable (`useCallback`) and internally `off()` the previous handler before `on()`.
3. Centralize event handling in the provider and expose state, not raw subscriptions.

## Recommendation

Return an unsubscribe from each helper and call `socket.off` in cleanup; also memoize the helpers. This touches many consumers — coordinate as a single refactor pass.

## Affected Files

- `frontend/src/socket/contexts/socket-context.tsx:244`

## References

- [Full Codebase Audit 2026-05-30](../09%20-%20Audits/Full%20Codebase%20Audit%20-%202026-05-30.md) — DEC-19
