Skip to content

TaskTrackingZoneSpec stops tracking the task for setInterval(callback, ms), when the callback is invoked for the first time #45350

@Platonn

Description

@Platonn

Which @angular/* package(s) are the source of the bug?

zone.js/plugins/task-tracking

Is this a regression?

Yes

Description

Actual behavior: TaskTrackingZone.getTasksFor('macroTask') doesn't return the task for setInterval(callback, ms), if the callback was already invoked before.

Expected behavior: the periodic task remains tracked until it's explicitly cancelled (e.g. with clearInterval()).

Why this bug is important

TaskTrackingZoneSpec is useful to for example for debugging why the Angular SSR app hangs (due to pending macro tasks):

import 'zone.js/plugins/task-tracking';
/* ... */

  constructor(protected ngZone: NgZone) {
    ngZone.runOutsideAngular(() => {
      setTimeout(() => {
        console.log({ macroTasks: this.getMacroTasks() });
      }, /* some delay */ 5000);
    });
  }
  
  getMacroTasks() {
    return (
      this.ngZone as any
    )._inner._parent._properties.TaskTrackingZone.getTasksFor('macroTask');
  }

If the setInterval() has a shorter periodic timer value (e.g. 1000ms) than our debugging script's delay time (e.g. 5000ms), then this setInterval task won't be logged in the list of pending Zone's macrotasks!

When the callback of setInverval(callback, ms) was invoked for the first time, then this task is removed from the array of tracked tasks in TaskTrackingZone. See source code of TaskTrackingZoneSpec:

To fix this bug, you would need to change this line:

- if (task.type === 'eventTask')
+ if (task.type === 'eventTask' || (task.data && task.data.isPeriodic))

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/tasktrackingzonespec-setinterval?file=src%2Fapp%2Fapp.component.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 12.0.5
Node: 16.13.0
Package Manager: yarn 1.22.10
OS: darwin arm64

Angular: 12.0.5
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1200.5
@angular-devkit/build-angular   12.0.5
@angular-devkit/core            12.0.5
@angular-devkit/schematics      12.0.5
@schematics/angular             12.0.5
rxjs                            6.6.7
typescript                      4.2.4

zone.js                         0.11.5

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: zonesIssues related to zone.js

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions