Skip to content

Commit b580f7b

Browse files
committed
fix(zone.js): do not run stack formatting when the lazy stack frames policy is used
We don't have to read the `stack` property when the lazy stack frames policy is used. The V8 triggers the stack trace serialization only when the `stack` property is read for the first time within the `Accessors::ErrorStackGetter`. We want to avoid formatting the stack trace when the lazy policy is used, and we don't need the stack until the `zoneAwareStack` property is read. PR Close angular#43240
1 parent 8d42747 commit b580f7b

2 files changed

Lines changed: 65 additions & 13 deletions

File tree

‎packages/zone.js/lib/common/error-rewrite.ts‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,27 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
119119
function ZoneAwareError(this: unknown|typeof NativeError): Error {
120120
// We always have to return native error otherwise the browser console will not work.
121121
let error: Error = NativeError.apply(this, arguments);
122-
// Save original stack trace
123-
const originalStack = (error as any)['originalStack'] = error.stack;
124122

125-
// Process the stack trace and rewrite the frames.
126-
if ((ZoneAwareError as any)[stackRewrite] && originalStack) {
123+
if ((ZoneAwareError as any)[stackRewrite]) {
127124
let zoneFrame = api.currentZoneFrame();
128125
if (zoneJsInternalStackFramesPolicy === 'lazy') {
129-
// don't handle stack trace now
126+
// Don't handle stack trace now, and also, don't read the `stack` property. The V8 triggers
127+
// the stack trace serialization only when the `stack` property is read for the first time
128+
// within the `Accessors::ErrorStackGetter`. We want to avoid formatting the stack trace
129+
// when the lazy policy is used, and we don't need the stack until the `zoneAwareStack`
130+
// property is read.
130131
(error as any)[api.symbol('zoneFrameNames')] = buildZoneFrameNames(zoneFrame);
131132
} else if (zoneJsInternalStackFramesPolicy === 'default') {
132-
try {
133-
error.stack = error.zoneAwareStack = buildZoneAwareStackFrames(originalStack, zoneFrame);
134-
} catch (e) {
135-
// ignore as some browsers don't allow overriding of stack
133+
// Note: we access the `error.stack` property immediately and save it as original stack
134+
// trace only when the default policy is used.
135+
const originalStack = error.originalStack = error.stack;
136+
if (originalStack) {
137+
try {
138+
error.stack = error.zoneAwareStack =
139+
buildZoneAwareStackFrames(originalStack, zoneFrame);
140+
} catch (e) {
141+
// ignore as some browsers don't allow overriding of stack
142+
}
136143
}
137144
}
138145
}
@@ -162,22 +169,26 @@ Zone.__load_patch('Error', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
162169

163170
const zoneAwareStackSymbol = api.symbol('zoneAwareStack');
164171

165-
// try to define zoneAwareStack property when zoneJsInternal frames policy is delay
172+
// try to define zoneAwareStack property when zoneJsInternal frames policy is lazy
166173
if (zoneJsInternalStackFramesPolicy === 'lazy') {
167174
Object.defineProperty(ZoneAwareError.prototype, 'zoneAwareStack', {
168175
configurable: true,
169176
enumerable: true,
170177
get: function() {
171178
if (!this[zoneAwareStackSymbol]) {
172179
this[zoneAwareStackSymbol] = buildZoneAwareStackFrames(
173-
this.originalStack, this[api.symbol('zoneFrameNames')], false);
180+
// Note: the `zoneAwareStack` may be overwritten since it has a setter and it'll
181+
// contain the latest value, otherwise we just fallback to the `stack` property.
182+
// Reading `this.stack` will trigger stack trace serialization.
183+
this.originalStack ?? this.stack, this[api.symbol('zoneFrameNames')], false);
174184
}
175185
return this[zoneAwareStackSymbol];
176186
},
177187
set: function(newStack: string) {
188+
// Note: do not set the `stack` property as some browsers don't support overriding it.
178189
this.originalStack = newStack;
179-
this[zoneAwareStackSymbol] = buildZoneAwareStackFrames(
180-
this.originalStack, this[api.symbol('zoneFrameNames')], false);
190+
this[zoneAwareStackSymbol] =
191+
buildZoneAwareStackFrames(newStack, this[api.symbol('zoneFrameNames')], false);
181192
}
182193
});
183194
}

‎packages/zone.js/test/common/Error.spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,47 @@ describe('ZoneAwareError', () => {
287287
}
288288
});
289289

290+
it('should not access the `stack` property immediately when the policy is `lazy`', () => {
291+
if (policy !== 'lazy' || !(Error as any)['stackRewrite']) {
292+
return;
293+
}
294+
295+
class LazyStackError extends Error {
296+
/** @internal **/
297+
_nativeError: Error;
298+
299+
stackHasBeenRead = false;
300+
301+
constructor(message: string) {
302+
super();
303+
const nativeError = new Error(message) as any as Error;
304+
this._nativeError = nativeError;
305+
}
306+
307+
override get stack() {
308+
this.stackHasBeenRead = true;
309+
return this._nativeError.stack;
310+
}
311+
}
312+
313+
const rootZone = Zone.root;
314+
rootZone.run(testFn);
315+
316+
function testFn() {
317+
let lazyStackError: LazyStackError;
318+
try {
319+
throw new LazyStackError('lazy stack');
320+
} catch (error: unknown) {
321+
lazyStackError = (error as LazyStackError);
322+
}
323+
324+
expect(lazyStackError.stackHasBeenRead).toEqual(false);
325+
const {zoneAwareStack} = lazyStackError;
326+
expect(lazyStackError.stackHasBeenRead).toEqual(true);
327+
expect(zoneAwareStack).toMatch(/Error: lazy stack/);
328+
}
329+
});
330+
290331
const zoneAwareFrames = [
291332
'Zone.run', 'Zone.runGuarded', 'Zone.scheduleEventTask', 'Zone.scheduleMicroTask',
292333
'Zone.scheduleMacroTask', 'Zone.runTask', 'ZoneDelegate.scheduleTask',

0 commit comments

Comments
 (0)