Skip to content

fix: don't let telemetry timers block Angular from becoming stable - #302

Merged
Hector Hernandez (hectorhdzg) merged 2 commits into
microsoft:mainfrom
arturovt:fix/hydration-blocked-by-telemetry-timer
Aug 28, 2026
Merged

Hector Hernandez (hectorhdzg) merged 2 commits into
microsoft:mainfrom
arturovt:fix/hydration-blocked-by-telemetry-timer

Conversation

@arturovt

Copy link
Copy Markdown
Contributor

App Insights keeps a repeating timer alive in the background to batch and retry sending events. If that timer gets scheduled while running inside Angular's zone, Angular never sees the zone go quiet, so it never thinks the app is "stable" - this blocks hydration and can delay it forever while telemetry keeps flowing in.

This runs trackPageView (on route change) and processTelemetry outside Angular's zone, using Zone.root.run the same way NgZone.runOutsideAngular does. Since the zone context carries through the rest of the synchronous call chain, this also moves the telemetry SDK's own batch/retry timer out of Angular's zone, without needing any changes to that SDK itself.

This does not affect the separate flush-on-unload code path, which already sends synchronously via sendBeacon and never touches this timer at all, so event delivery on page unload is unaffected.

Fixes #117.

App Insights keeps a repeating timer alive in the background to batch
and retry sending events. If that timer gets scheduled while running
inside Angular's zone, Angular never sees the zone go quiet, so it
never thinks the app is "stable" - this blocks hydration and can
delay it forever while telemetry keeps flowing in.

This runs trackPageView (on route change) and processTelemetry
outside Angular's zone, using Zone.root.run the same way
NgZone.runOutsideAngular does. Since the zone context carries through
the rest of the synchronous call chain, this also moves the
telemetry SDK's own batch/retry timer out of Angular's zone, without
needing any changes to that SDK itself.

This does not affect the separate flush-on-unload code path, which
already sends synchronously via sendBeacon and never touches this
timer at all, so event delivery on page unload is unaffected.

Fixes microsoft#117.
@arturovt
Artur (arturovt) requested a review from a team as a code owner August 26, 2026 21:20
@arturovt

Copy link
Copy Markdown
Contributor Author

Hector Hernandez (@hectorhdzg) a friendly ping on this, an easy production tested change.

@hectorhdzg

Copy link
Copy Markdown
Member

Thanks for your contribution Artur (@arturovt), could you add a regression test that initializes App Insights with loadAppInsights() inside NgZone, then verifies ApplicationRef.isStable becomes true before the SDK’s 10-second timer expires?

Testing only processTelemetry() isn’t enough because the original reporter also had to move SDK initialization outside Angular. Also, why use Zone.root.run() instead of an injected NgZone.runOutsideAngular()? Jumping to root can bypass custom parent-zone behavior.

Adds a regression test for microsoft#117 that doesn't stub out the channel/Sender
like the other spec does - it spins up a real ApplicationInsights instance
(the same loadAppInsights() call every app makes) so it exercises the
actual timers the SDK schedules, not a mock.

This also answers a review question about scope: this plugin only wraps
two entry points it owns (trackPageView on route change, and
processTelemetry), so it can only keep timers that flow through those out
of Angular's zone. It has no way to touch timers AppInsightsCore itself
arms directly inside loadAppInsights() - like its diagnosticLogInterval,
which defaults to 10s and keeps re-arming itself for as long as the SDK
is alive. That call belongs to the app, not this plugin.

So the test has two phases: it calls loadAppInsights() itself outside
NgZone (the pattern now documented in the README), then separately
triggers a route change *inside* NgZone (matching a real click or
navigate() call) to prove this plugin's own fix covers what's left.
Reverting either the plugin fix or the recommended init pattern makes
this test hang past Angular's stability check instead of resolving.

Also documents the outside-NgZone init pattern in the README, since it's
required for hydration to actually stay unblocked - this plugin's fix
alone isn't sufficient on its own.
@arturovt

Copy link
Copy Markdown
Contributor Author

Thanks for the review Hector Hernandez (@hectorhdzg) !

Added a regression test for this — applicationinsights-angularplugin-js.hydration.spec.ts. It spins up a real ApplicationInsights instance and calls loadAppInsights() for real (not stubbing out the channel), then checks ApplicationRef.whenStable() actually resolves quickly.

And yeah, you were right to push on this — writing the test made the gap pretty obvious. This plugin only owns two entry points, trackPageView (route change) and processTelemetry, so it can only keep those out of NgZone. That does include the Sender's batch/retry timer since it's reached through processTelemetry, but it can't do anything about timers that AppInsightsCore schedules directly inside loadAppInsights() itself — e.g. diagnosticLogInterval, which defaults to 10s and just keeps re-arming itself the whole time the SDK is alive. That one's armed before any of our plugin code even runs, as part of the app's own loadAppInsights() call — not something this plugin calls or controls.

So the test does two things:

  1. Calls loadAppInsights() itself through ngZone.runOutsideAngular() — added this as the recommended pattern in the README too — which keeps the SDK's own init-time timers out of NgZone.
    Then separately fires a route change inside NgZone (mimicking an actual click/navigate()), to prove the plugin's own fix handles what's left.
  2. Checked both directions locally — pull either the plugin fix or the outside-NgZone init and the test hangs past the stability check instead of resolving, so it's actually catching the thing it's supposed to.

On Zone.root.run() vs injecting NgZone.runOutsideAngular() — two reasons we went with root:

  • Injecting NgZone would break anyone constructing AngularPlugin outside an injection context, which is a supported way to use it today (new AngularPlugin() / new AngularPlugin(injector), not resolved via DI).
  • We're not too worried about clobbering "custom parent zone" behavior, because there really isn't a legitimate case for an app to be customizing zone behavior above NgZone — that's internal to Angular, not something apps are meant to extend.

Let me know if you want the test shaped differently or the README wording tweaked, happy to adjust.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you for taking care of this

@hectorhdzg
Hector Hernandez (hectorhdzg) merged commit b9ebeb9 into microsoft:main Aug 28, 2026
6 checks passed
@arturovt
Artur (arturovt) deleted the fix/hydration-blocked-by-telemetry-timer branch August 28, 2026 20:45
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.

[BUG] Incompatible with Angular Hydration

2 participants