Skip to content

fix(auth-guard): remove memory-leaking subscription in canActivate#329

Open
tarun-227 wants to merge 2 commits into
PSMRI:developfrom
tarun-227:fix/auth-guard-memory-leak
Open

fix(auth-guard): remove memory-leaking subscription in canActivate#329
tarun-227 wants to merge 2 commits into
PSMRI:developfrom
tarun-227:fix/auth-guard-memory-leak

Conversation

@tarun-227
Copy link
Copy Markdown

Problem

Fixes #138 (reported in PSMRI/AMRIT).

AuthGuard.canActivate() subscribes to http_service.currentLangugae$ on every route activation with no cleanup mechanism:

canActivate(route: any, state: any) {
  this.http_service.currentLangugae$.subscribe(
    response => (this.current_language_set = response)
  );
  // ...
}

Because AuthGuard is a singleton and currentLangugae$ is a BehaviorSubject that never completes, each navigation to any of the 7 guarded routes (/service, /servicePoint, /registrar, /nurse-doctor, /lab, /pharmacist, /datasync) adds a subscription that is never released. Subscriber count grows indefinitely with every navigation.

Additionally, the subscribed value current_language_set is only assigned — it is never read anywhere in the class — so the subscription has no functional effect at all.

Fix

Remove the subscription, the current_language_set property, and the now-unused HttpServiceService import and constructor injection. The guard's actual logic (auth.validateSessionKey()) is unchanged.

canActivate(route: any, state: any) {
  return this.auth.validateSessionKey().pipe(
    tap((res: any) => {
      if (!(res && res.statusCode === 200 && res.data)) {
        this.router.navigate(['/login']);
      }
    })
  );
}

Impact

  • Eliminates unbounded memory growth on protected-route navigation
  • No behaviour change — the removed code had no observable effect

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 1, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 53cc517e-5e25-4163-9c80-f4967a508c3c

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 1, 2026

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