---
id: ML-169.3
title: "Phase 2: PubSub subscription lifecycle audit"
status: Done
assignee: []
created_date: "2026-05-08 08:59"
updated_date: "2026-05-19 08:50"
labels:
  - audit
  - ready
dependencies: []
references:
  - lib/music_library/records.ex
  - lib/music_library/listening_stats.ex
  - lib/music_library_web/live_helpers/record_actions.ex
modified_files:
  - >-
    docs/audits/phase2-pubsub-lifecycle/doc-25 -
    Audit-Report-PubSub-Subscription-Lifecycle-Phase-2.md
parent_task_id: ML-169
priority: medium
ordinal: 5000
---

## Description

<!-- SECTION:DESCRIPTION:BEGIN -->

Audit all PubSub subscribe/unsubscribe pairs for correctness. Verify no stale subscriptions or double-subscription risks.

Pre-flagged concern: ListeningStats.subscribe() is called in mount by StatsLive.Index and ScrobbledTracksLive.Index but never unsubscribed. Phoenix.PubSub monitors PID for auto-cleanup, but double-subscription on LiveView reconnect is possible and needs verification.

<!-- SECTION:DESCRIPTION:END -->

## Acceptance Criteria

<!-- AC:BEGIN -->

- [x] #1 Records.subscribe/1 and Records.unsubscribe/1 call sites audited — confirmed only called via manage_subscription/2 in Show LiveViews
- [x] #2 manage_subscription/2 verified correct across all navigation paths (direct URL, browser back/forward, push_navigate, push_patch)
- [x] #3 ListeningStats.subscribe() double-subscription risk on reconnect evaluated — either confirmed safe by PID monitoring or flagged with fix
- [x] #4 Termination cleanup verified: Phoenix.PubSub auto-cleans on PID death confirmed; any manual unsubscribe gaps documented
- [x] #5 Records.notify_update/1 broadcast sites audited against intended behavior (coordinates with Phase 3 worker audit)
- [x] #6 Findings report written as a Backlog.md document with file:line references, severity ratings, and fix recommendations
<!-- AC:END -->

## Implementation Notes

<!-- SECTION:NOTES:BEGIN -->

Audit complete. Report written at docs/audits/phase2-pubsub-lifecycle/doc-25.

Key findings:

- 27 call sites across subscribe, unsubscribe, broadcast, and consumer handle_info verified
- All 8 subscribe sites have cleanup coverage (explicit unsubscribe or PID-death auto-clean)
- Pre-flagged ListeningStats.subscribe() double-subscription risk is a false positive: Phoenix.PubSub MapSet deduplicates per PID, and LiveView reconnect reuses the same PID within grace period
- No code changes required
<!-- SECTION:NOTES:END -->
