Skip to content

Cancel GlobalLoader timers on destroy - #3285

Open
CydeWeys wants to merge 1 commit into
google:masterfrom
CydeWeys:fix-userdata-loading-timeout-flake
Open

CydeWeys wants to merge 1 commit into
google:masterfrom
CydeWeys:fix-userdata-loading-timeout-flake

Conversation

@CydeWeys

@CydeWeys CydeWeys commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

GlobalLoaderService schedules 10-second RxJS timeout subscriptions for active loaders, but did not implement OnDestroy to unsubscribe them when the Angular injector is torn down. In Vitest runs exceeding 10 seconds (such as CI), tests that instantiate UserDataService without resolving the initial /console-api/userdata HTTP request leave a dangling timer that fires after TestBed destruction and throws NG0205 when calling MatSnackBar.open in UserDataService.loadingTimeout().

  • Implement OnDestroy in GlobalLoaderService to unsubscribe and clear all active loader timers on teardown, and unsubscribe any existing loader subscription before replacing it in startGlobalLoader().
  • Implement OnDestroy in all short-lived GlobalLoader components (HistoryComponent, ContactComponent, ContactDetailsComponent, RdapEditComponent, EppPasswordEditComponent, SecurityEditComponent) to call stopGlobalLoader(this) on component teardown so mid-flight route/view changes do not leak 10s timers.
  • Fix constructor loader lifecycle in both UserDataService and RegistrarService: start the global loader before subscribing so synchronous completions do not leak a timer, use finalize() to stop the loader (and resolve registrarsLoaded) on completion/error/unsubscribe, pass an error observer ({ error: () => {} }) so RxJS does not trigger reportUnhandledError on rethrown HTTP errors, and implement OnDestroy to unsubscribe in-flight initial requests.
  • Add unit tests in userData.service.spec.ts, registrar.service.spec.ts, shared.spec.ts, history.component.spec.ts, contact.component.spec.ts, rdap.component.spec.ts, and security.component.spec.ts covering TestBed teardown, ngOnDestroy, synchronous completion, error handling, duplicate startGlobalLoader calls, and short-lived component teardown.

BUG= http://b/588453384


This change is Reviewable

@CydeWeys
CydeWeys requested a review from ptkach November 20, 2026 19:19
Comment on lines +62 to +68
ngOnDestroy() {
for (const subscription of this.loaders.values()) {
subscription.unsubscribe();
}
this.loaders.clear();
this.syncLoading();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Because GlobalLoaderService is providedIn: 'root', ngOnDestroy() only runs when the root injector / TestBed is torn down—not when short-lived components implementing GlobalLoader (such as HistoryComponent, ContactComponent, ContactDetailsComponent, RdapEditComponent, EppPasswordEditComponent, and SecurityEditComponent) are destroyed on route/view changes while a request is still in flight. Those components will still retain a 10s timer in this.loaders and invoke loadingTimeout() (MatSnackBar.open) after component destruction unless they call stopGlobalLoader(this) on teardown.

this.getUserData().subscribe(() => {
this.globalLoader.stopGlobalLoader(this);
});
this.globalLoader.startGlobalLoader(this);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

RegistrarService (console-webapp/src/app/registrar/registrar.service.ts lines 101–108) has this exact same constructor bug pattern and was left unfixed:

  1. this.loadRegistrars().subscribe(...) runs before this.globalLoader.startGlobalLoader(this), so synchronous of(...) mocks leave globalLoader.isLoading() stuck true and leak a 10s timer that fires 'Timeout loading registrars' (which is why registrar.service.spec.ts had to use a deferred Subject).
  2. No finalize() on error (isLoading() stays true for 10s and registrarsLoaded hangs forever).
  3. No OnDestroy to unsubscribe the in-flight /console-api/registrars request.

Comment on lines +46 to +48
this.userDataSubscription = this.getUserData()
.pipe(finalize(() => this.globalLoader.stopGlobalLoader(this)))
.subscribe();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

BackendService.getUserData() calls this.errorCatcher<UserData>(err) with no mockData, which rethrows via throwError(() => error). In RxJS 7+, calling .subscribe() with no error observer forwards errors to reportUnhandledError (setTimeout(() => { throw err; })), which surfaces as an uncaught exception if /console-api/userdata fails (including in Vitest fake-timer tests when advancing timers). Pass { error: () => {} } (or open an error snackbar) in .subscribe(...).

Comment on lines +126 to +151
it('should open snackbar on loadingTimeout if request does not resolve in time', () => {
vi.useFakeTimers();
const pendingUserData$ = new Subject<UserData>();

TestBed.configureTestingModule({
providers: [
BackendService,
GlobalLoaderService,
MatSnackBar,
provideHttpClient(),
provideHttpClientTesting(),
],
});
const backend = TestBed.inject(BackendService);
vi.spyOn(backend, 'getUserData').mockReturnValue(
pendingUserData$.asObservable()
);
const snackBarSpy = vi
.spyOn(TestBed.inject(MatSnackBar), 'open')
.mockReturnValue({} as never);

TestBed.inject(UserDataService);
vi.advanceTimersByTime(10000);

expect(snackBarSpy).toHaveBeenCalledWith('Timeout loading user data');
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The PR description notes that finalize() stops the loader on completion/error/unsubscribe, but there is no test covering the error path (e.g., throwError(...) + vi.advanceTimersByTime(10000)). Adding an error test case here will also catch the unhandled error from .subscribe() having no error observer.

GlobalLoaderService schedules 10-second RxJS timeout subscriptions for
active loaders, but did not implement OnDestroy to unsubscribe them when
the Angular injector is torn down. In Vitest runs exceeding 10 seconds
(such as CI), tests that instantiate UserDataService without resolving
the initial /console-api/userdata HTTP request leave a dangling timer
that fires after TestBed destruction and throws NG0205 when calling
MatSnackBar.open in UserDataService.loadingTimeout().

- Implement OnDestroy in GlobalLoaderService to unsubscribe and clear
  all active loader timers on teardown, and unsubscribe any existing
  loader subscription before replacing it in startGlobalLoader().
- Implement OnDestroy in all short-lived GlobalLoader components
  (HistoryComponent, ContactComponent, ContactDetailsComponent,
  RdapEditComponent, EppPasswordEditComponent, SecurityEditComponent)
  to call stopGlobalLoader(this) on component teardown so mid-flight
  route/view changes do not leak 10s timers.
- Fix constructor loader lifecycle in both UserDataService and
  RegistrarService: start the global loader before subscribing so
  synchronous completions do not leak a timer, use finalize() to stop
  the loader (and resolve registrarsLoaded) on completion/error/
  unsubscribe, pass an error observer ({ error: () => {} }) so RxJS
  does not trigger reportUnhandledError on rethrown HTTP errors, and
  implement OnDestroy to unsubscribe in-flight initial requests.
- Add unit tests in userData.service.spec.ts, registrar.service.spec.ts,
  shared.spec.ts, history.component.spec.ts, contact.component.spec.ts,
  rdap.component.spec.ts, and security.component.spec.ts covering
  TestBed teardown, ngOnDestroy, synchronous completion, error handling,
  duplicate startGlobalLoader calls, and short-lived component teardown.

BUG= http://b/588453384
@CydeWeys
CydeWeys force-pushed the fix-userdata-loading-timeout-flake branch from 9303c16 to a48c8e6 Compare November 22, 2026 18:24
@CydeWeys CydeWeys changed the title Cancel GlobalLoader timers on service destroy Cancel GlobalLoader timers on destroy Nov 22, 2026

This branch has not been deployed

No deployments
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.

2 participants