Skip to content

Commit a299e02

Browse files
mmalerbaalxhub
authored andcommitted
fix(core): preserve tracing snapshot until tick finishes (#59796)
This change waits until the end of `tick` to clear the tracing snapshot. This ensures that if we notify the scheduler during `tick` the correct tracing snapshot is used. PR Close #59796
1 parent 5191a41 commit a299e02

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

packages/core/src/application/application_ref.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ import {ViewRef as InternalViewRef} from '../render3/view_ref';
4343
import {TESTABILITY} from '../testability/testability';
4444
import {NgZone} from '../zone/ng_zone';
4545

46+
import {profiler} from '../render3/profiler';
47+
import {ProfilerEvent} from '../render3/profiler_types';
48+
import {EffectScheduler} from '../render3/reactivity/root_effect_scheduler';
4649
import {ApplicationInitStatus} from './application_init';
4750
import {TracingAction, TracingService, TracingSnapshot} from './tracing';
48-
import {EffectScheduler} from '../render3/reactivity/root_effect_scheduler';
49-
import {ProfilerEvent} from '../render3/profiler_types';
50-
import {profiler} from '../render3/profiler';
5151

5252
/**
5353
* A DI token that provides a set of callbacks to
@@ -580,21 +580,20 @@ export class ApplicationRef {
580580
}
581581

582582
/** @internal */
583-
_tick = (): void => {
583+
_tick(): void {
584584
profiler(ProfilerEvent.ChangeDetectionStart);
585585

586586
if (this.tracingSnapshot !== null) {
587-
const snapshot = this.tracingSnapshot;
588-
this.tracingSnapshot = null;
589-
590-
// Ensure we always run `_tick()` in the context of the most recent snapshot,
587+
// Ensure we always run `tickImpl()` in the context of the most recent snapshot,
591588
// if one exists. Snapshots may be reference counted by the implementation so
592589
// we want to ensure that if we request a snapshot that we use it.
593-
snapshot.run(TracingAction.CHANGE_DETECTION, this._tick);
594-
snapshot.dispose();
595-
return;
590+
this.tracingSnapshot.run(TracingAction.CHANGE_DETECTION, this.tickImpl);
591+
} else {
592+
this.tickImpl();
596593
}
594+
}
597595

596+
private tickImpl = (): void => {
598597
(typeof ngDevMode === 'undefined' || ngDevMode) && warnIfDestroyed(this._destroyed);
599598
if (this._runningTick) {
600599
throw new RuntimeError(
@@ -617,6 +616,8 @@ export class ApplicationRef {
617616
this.internalErrorHandler(e);
618617
} finally {
619618
this._runningTick = false;
619+
this.tracingSnapshot?.dispose();
620+
this.tracingSnapshot = null;
620621
setActiveConsumer(prevConsumer);
621622
this.afterTick.next();
622623

0 commit comments

Comments
 (0)