Skip to content

Commit 4792db9

Browse files
fix(core): Explicitly manage TracingSnapshot lifecycle and dispose of it once it's been used. (#58929)
Provide a callback to the TracingService implementation when a Snapshot can be disposed. The underlying tracing implementation may use refcounting and needs to release resources to enable the trace to complete. While change detection uses the snapshot for exactly one callback, after render runs multiple hooks in the sequence so we need a more predictable way to indicate that the snapshot can be finalized.s PR Close #58929
1 parent 31f73cd commit 4792db9

File tree

4 files changed

+10
-0
lines changed

4 files changed

+10
-0
lines changed

packages/core/src/application/application_ref.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ export class ApplicationRef {
604604
// if one exists. Snapshots may be reference counted by the implementation so
605605
// we want to ensure that if we request a snapshot that we use it.
606606
snapshot.run(TracingAction.CHANGE_DETECTION, this._tick);
607+
snapshot.dispose();
607608
return;
608609
}
609610

packages/core/src/application/tracing.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export enum TracingAction {
1717
/** A single tracing snapshot. */
1818
export interface TracingSnapshot {
1919
run<T>(action: TracingAction, fn: () => T): T;
20+
21+
/** Disposes of the tracing snapshot. Must be run exactly once per TracingSnapshot. */
22+
dispose(): void;
2023
}
2124

2225
/**
@@ -39,6 +42,10 @@ export interface TracingService<T extends TracingSnapshot> {
3942
* used when additional work is performed that was scheduled in this context.
4043
*
4144
* @param linkedSnapshot Optional snapshot to use link to the current context.
45+
* The caller is no longer responsible for calling dispose on the linkedSnapshot.
46+
*
47+
* @return The tracing snapshot. The caller is responsible for diposing of the
48+
* snapshot.
4249
*/
4350
snapshot(linkedSnapshot: T | null): T;
4451
}

packages/core/src/render3/after_render/manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export class AfterRenderSequence implements AfterRenderRef {
187187
// associates the initial run of the hook with the context that created it.
188188
// Follow-up runs are independent of that initial context and have different
189189
// triggers.
190+
this.snapshot?.dispose();
190191
this.snapshot = null;
191192
}
192193

packages/core/test/acceptance/tracing_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('TracingService', () => {
2727
actions.push(action);
2828
return fn();
2929
},
30+
dispose() {},
3031
};
3132
mockTracingService = {
3233
snapshot: jasmine.createSpy('snapshot').and.returnValue(fakeSnapshot),

0 commit comments

Comments
 (0)