Skip to content

fix(transloco): memory leak due global service reference not being cleaned up#925

Open
dscheerens wants to merge 1 commit into
jsverse:masterfrom
dscheerens:fix-global-transloco-service-memory-leak
Open

fix(transloco): memory leak due global service reference not being cleaned up#925
dscheerens wants to merge 1 commit into
jsverse:masterfrom
dscheerens:fix-global-transloco-service-memory-leak

Conversation

@dscheerens

@dscheerens dscheerens commented Jun 1, 2026

Copy link
Copy Markdown

This pull request is meant to prevent a memory leak when the TranslocoService is destroyed, but the global service reference is kept alive.

We're running into this issue with a micro frontend architecture, where the TranslocoService is not garbage collected after the Angular application that uses it, is swapped out for another MFE application.

To solve the issue, this PR sets the service to undefined during the destroy lifecycle phase. As a safety feature this will only be done if the service being destroyed matches the current value held by service in case multiple micro frontends are active, where each could be having their own instance of the TranslocoService.


Summary by cubic

Fixes a memory leak by clearing the global service reference when a TranslocoService instance is destroyed. Also adds guards to handle calls when no active service is available, which is common in micro frontend swaps.

  • Bug Fixes
    • Set global service to undefined during destroy when it matches the instance, enabling garbage collection.
    • Updated translate and translateObject to safely handle missing service: log a dev warning and return '' or [].

Written for commit 789bfe2. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced the translation service to gracefully handle uninitialized service states by returning safe empty values instead of failing
    • Improved service cleanup to properly clear stale references and prevent memory retention

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The module-level TranslocoService reference is now optional with defensive checks. Exported translate and translateObject helpers guard against a missing service by logging dev-mode warnings and returning safe empty values. The service destroy hook clears its global reference to prevent stale memory retention.

Changes

Transloco Service Initialization Guards

Layer / File(s) Summary
Service existence guards in exported helpers
libs/transloco/src/lib/transloco.service.ts
The translate and translateObject exported helpers check if the global service exists and safely return empty values ('' and []) with optional dev-mode warnings before attempting to call into the service.
Service reference cleanup on destroy
libs/transloco/src/lib/transloco.service.ts
The TranslocoService destroy lifecycle hook clears the module-level global service reference to undefined when the destroyed instance is the current active one, preventing stale references and memory leaks.

Fun fact: In i18n (internationalization), the term "i18n" is a numeronym where the number "18" represents the 18 letters between the first "i" and the last "n"—a clever abbreviation! Similarly, "l10n" stands for localization. The word "transloco" draws from Spanish transloco, meaning "crazy" or "out of one's mind"—perhaps a playful nod to the sometimes dizzying complexity of handling translations across languages. 🌍

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies the main fix: preventing a memory leak caused by a global service reference not being cleaned up during destruction.
Description check ✅ Passed The description covers the issue, solution, and implementation details, though some PR checklist items are unchecked and appear incomplete.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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.

@pkg-pr-new

pkg-pr-new Bot commented Jun 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

@jsverse/transloco

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco@925

@jsverse/transloco-locale

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-locale@925

@jsverse/transloco-messageformat

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-messageformat@925

@jsverse/transloco-optimize

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-optimize@925

@jsverse/transloco-persist-lang

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-lang@925

@jsverse/transloco-persist-translations

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-persist-translations@925

@jsverse/transloco-preload-langs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-preload-langs@925

@jsverse/transloco-schematics

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-schematics@925

@jsverse/transloco-scoped-libs

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-scoped-libs@925

@jsverse/transloco-utils

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-utils@925

@jsverse/transloco-validator

npm i https://pkg.pr.new/jsverse/transloco/@jsverse/transloco-validator@925

commit: 789bfe2

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@libs/transloco/src/lib/transloco.service.ts`:
- Around line 82-87: The guard that returns a hardcoded '' or [] when no active
TranslocoService exists should instead preserve the shape of the caller's input:
in the service presence check (the block referencing the local variable service
and ngDevMode) update translate() to inspect the incoming keys argument and
return a matching empty-shaped value (if keys is an array return keys.map(() =>
''), if it's a plain string return '' as now, if it's an object return an object
with the same keys mapped to ''), and update translateObject() to return an
object with the same single-key or multi-key shape populated with '' values;
apply the same shape-preserving logic to the other guard at the second location
mentioned (lines 97-102) so callers always get the same shape they passed in
even when service is absent.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2240ac77-166d-4c6e-917b-75be30d888db

📥 Commits

Reviewing files that changed from the base of the PR and between 82ae7bb and 789bfe2.

📒 Files selected for processing (1)
  • libs/transloco/src/lib/transloco.service.ts

Comment on lines +82 to +87
if (!service) {
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
console.warn('No active TranslocoService found');
}
return '' as T;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard-path fallback values should preserve output shape.

When no active service exists, translate() always returns '' and translateObject() always returns []. This can break callers expecting array/object shapes (e.g., array keys in translate, single-key object in translateObject) and reintroduce runtime issues on the “safe” path.

Proposed fix
 export function translate<T = string>(
   key: TranslateParams,
   params: HashMap = {},
   lang?: string,
 ): T {
   if (!service) {
     if (typeof ngDevMode !== 'undefined' && ngDevMode) {
       console.warn('No active TranslocoService found');
     }
-    return '' as T;
+    return (Array.isArray(key) ? [] : '') as T;
   }

   return service.translate<T>(key, params, lang);
 }

 export function translateObject<T>(
   key: TranslateParams,
   params: HashMap = {},
   lang?: string,
 ): T | T[] {
   if (!service) {
     if (typeof ngDevMode !== 'undefined' && ngDevMode) {
       console.warn('No active TranslocoService found');
     }
-    return [];
+    return (Array.isArray(key) ? [] : ({} as T)) as T | T[];
   }

   return service.translateObject<T>(key, params, lang);
 }

Fun i18n fact: the word “hello” in Swahili is “jambo.”

Also applies to: 97-102

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libs/transloco/src/lib/transloco.service.ts` around lines 82 - 87, The guard
that returns a hardcoded '' or [] when no active TranslocoService exists should
instead preserve the shape of the caller's input: in the service presence check
(the block referencing the local variable service and ngDevMode) update
translate() to inspect the incoming keys argument and return a matching
empty-shaped value (if keys is an array return keys.map(() => ''), if it's a
plain string return '' as now, if it's an object return an object with the same
keys mapped to ''), and update translateObject() to return an object with the
same single-key or multi-key shape populated with '' values; apply the same
shape-preserving logic to the other guard at the second location mentioned
(lines 97-102) so callers always get the same shape they passed in even when
service is absent.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="libs/transloco/src/lib/transloco.service.ts">

<violation number="1" location="libs/transloco/src/lib/transloco.service.ts:86">
P2: The fallback value `'' as T` does not match the expected return shape when `key` is an array. Callers using `translate(['key1', 'key2'])` expect an array back, but this guard returns an empty string. Consider returning `[] as T` when `key` is an array to preserve the output shape contract.</violation>

<violation number="2" location="libs/transloco/src/lib/transloco.service.ts:101">
P2: The fallback value `[]` does not match the expected return shape when `key` is a single string. Callers using `translateObject('path.to.object')` expect an object `T` back, but this guard always returns an empty array. Consider returning `{} as T` for single-key calls and `[]` only for array keys.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

if (typeof ngDevMode !== 'undefined' && ngDevMode) {
console.warn('No active TranslocoService found');
}
return [];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The fallback value [] does not match the expected return shape when key is a single string. Callers using translateObject('path.to.object') expect an object T back, but this guard always returns an empty array. Consider returning {} as T for single-key calls and [] only for array keys.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At libs/transloco/src/lib/transloco.service.ts, line 101:

<comment>The fallback value `[]` does not match the expected return shape when `key` is a single string. Callers using `translateObject('path.to.object')` expect an object `T` back, but this guard always returns an empty array. Consider returning `{} as T` for single-key calls and `[]` only for array keys.</comment>

<file context>
@@ -87,6 +94,13 @@ export function translateObject<T>(
+    if (typeof ngDevMode !== 'undefined' && ngDevMode) {
+      console.warn('No active TranslocoService found');
+    }
+    return [];
+  }
+
</file context>

if (typeof ngDevMode !== 'undefined' && ngDevMode) {
console.warn('No active TranslocoService found');
}
return '' as T;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The fallback value '' as T does not match the expected return shape when key is an array. Callers using translate(['key1', 'key2']) expect an array back, but this guard returns an empty string. Consider returning [] as T when key is an array to preserve the output shape contract.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At libs/transloco/src/lib/transloco.service.ts, line 86:

<comment>The fallback value `'' as T` does not match the expected return shape when `key` is an array. Callers using `translate(['key1', 'key2'])` expect an array back, but this guard returns an empty string. Consider returning `[] as T` when `key` is an array to preserve the output shape contract.</comment>

<file context>
@@ -72,13 +72,20 @@ import {
+    if (typeof ngDevMode !== 'undefined' && ngDevMode) {
+      console.warn('No active TranslocoService found');
+    }
+    return '' as T;
+  }
+
</file context>
Suggested change
return '' as T;
return (Array.isArray(key) ? [] : '') as T;

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