Skip to content

Commit 70ff634

Browse files
committed
fix(har): also drop pending sizes and compression barriers on flush
With the response extra info never arriving, the request can still finish normally (Network.loadingFinished is delivered). The HAR tracer then adds sizes and compression calculation barriers that await the extra-info-fed response headers size promise, which never resolves. Since tracing flush runs before the pages are closed, these barriers hang context.close() the same way the raw header barriers did. Treat all barriers that only upgrade already-recorded provisional data as droppable on flush, not just the raw header ones. The extra info is not force-resolved server side because it can still arrive after loadingFinished. Fixes: #42448
1 parent 2148b48 commit 70ff634

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

‎packages/playwright-core/src/server/har/harTracer.ts‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ type HarTracerOptions = {
6666
export class HarTracer {
6767
private _context: BrowserContext | APIRequestContext;
6868
private _barrierPromises = new Set<Promise<void>>();
69-
// Raw header upgrade barriers among the barrier promises. Provisional headers
70-
// are recorded as a fallback for them, so they can be dropped when the
71-
// recording stops instead of waiting for the extra info that may never arrive.
72-
private _headerUpgradeBarriers = new Set<Promise<void>>();
69+
// Barriers that only upgrade already-recorded provisional data (raw headers,
70+
// sizes, compression) and wait for the network "extra info" that may never
71+
// arrive, see https://github.com/microsoft/playwright/issues/42448. They can
72+
// be dropped when the recording stops instead of hanging the flush.
73+
private _upgradeBarriers = new Set<Promise<void>>();
7374
private _delegate: HarTracerDelegate;
7475
private _options: HarTracerOptions;
7576
private _pageEntries: har.Page[] = [];
@@ -213,12 +214,12 @@ export class HarTracer {
213214
return race;
214215
}
215216

216-
private _addHeaderUpgradeBarrier(target: Page | Worker | null, promise: Promise<void>) {
217+
private _addUpgradeBarrier(target: Page | Worker | null, promise: Promise<void>) {
217218
const race = this._addBarrier(target, promise);
218219
if (!race)
219220
return;
220-
this._headerUpgradeBarriers.add(race);
221-
race.then(() => this._headerUpgradeBarriers.delete(race));
221+
this._upgradeBarriers.add(race);
222+
race.then(() => this._upgradeBarriers.delete(race));
222223
}
223224

224225
private _onAPIRequest(event: APIRequestEvent) {
@@ -379,7 +380,7 @@ export class HarTracer {
379380
}
380381
};
381382
if (compressionCalculationBarrier)
382-
this._addBarrier(page || request.serviceWorker(), compressionCalculationBarrier.barrier);
383+
this._addUpgradeBarrier(page || request.serviceWorker(), compressionCalculationBarrier.barrier);
383384

384385
const promise = response.internalBody().then(buffer => {
385386
if (this._options.omitScripts && request.resourceType() === 'script') {
@@ -409,7 +410,7 @@ export class HarTracer {
409410
this._computeHarEntryTotalTime(harEntry);
410411

411412
if (!this._options.omitSizes) {
412-
this._addBarrier(page || request.serviceWorker(), response.internalSizes().then(sizes => {
413+
this._addUpgradeBarrier(page || request.serviceWorker(), response.internalSizes().then(sizes => {
413414
harEntry.response.bodySize = sizes.responseBodySize;
414415
harEntry.response.headersSize = sizes.responseHeadersSize;
415416
harEntry.response._transferSize = sizes.transferSize;
@@ -632,13 +633,13 @@ export class HarTracer {
632633
}
633634

634635
this._recordRequestOverrides(harEntry, request);
635-
this._addHeaderUpgradeBarrier(page || request.serviceWorker(), request.internalRawRequestHeaders().then(headers => {
636+
this._addUpgradeBarrier(page || request.serviceWorker(), request.internalRawRequestHeaders().then(headers => {
636637
this._recordRequestHeadersAndCookies(harEntry, headers);
637638
}));
638639
// Record available headers including redirect location in case the tracing is stopped before
639640
// response extra info is received (in Chromium).
640641
this._recordResponseHeaders(harEntry, response.headers());
641-
this._addHeaderUpgradeBarrier(page || request.serviceWorker(), response.internalRawResponseHeaders().then(headers => {
642+
this._addUpgradeBarrier(page || request.serviceWorker(), response.internalRawResponseHeaders().then(headers => {
642643
this._recordResponseHeaders(harEntry, headers);
643644
}));
644645
}
@@ -666,12 +667,13 @@ export class HarTracer {
666667
}
667668

668669
async flush() {
669-
// Drop the pending raw header upgrades: provisional headers are already recorded
670-
// for them as a fallback. If the response extra info has not arrived by now,
671-
// the request is most likely not going to finish, and waiting for the upgrade
672-
// would hang the flush, e.g. when saving the HAR at the context close.
670+
// Drop the pending data upgrade barriers: provisional data is already
671+
// recorded for them as a fallback. The extra info they wait for can arrive
672+
// late (e.g. after loadingFinished), so it is not resolved on the server
673+
// side - but once the recording stops, waiting for the upgrade would hang
674+
// the flush, e.g. when saving the HAR at the context close.
673675
// See https://github.com/microsoft/playwright/issues/42448.
674-
for (const barrier of this._headerUpgradeBarriers)
676+
for (const barrier of this._upgradeBarriers)
675677
this._barrierPromises.delete(barrier);
676678
await Promise.all(this._barrierPromises);
677679
}
@@ -680,7 +682,7 @@ export class HarTracer {
680682
this._started = false;
681683
eventsHelper.removeEventListeners(this._eventListeners);
682684
this._barrierPromises.clear();
683-
this._headerUpgradeBarriers.clear();
685+
this._upgradeBarriers.clear();
684686

685687
const context = this._context instanceof BrowserContext ? this._context : undefined;
686688
const log: har.Log = {

0 commit comments

Comments
 (0)