Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/playwright-core/src/server/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,26 @@ export class ProgressController {

if (deadline) {
const timeoutError = new TimeoutError(`Timeout ${timeout}ms exceeded.`);
timer = setTimeout(() => {
// TODO: migrate this to "progress.disableTimeout()".
if (this.metadata.pauseStartTime && !this.metadata.pauseEndTime)
return;
const remaining = deadline - monotonicTime();
if (remaining <= 0) {
// Deadline already passed, fire immediately without scheduling a timer.
if (this._state === 'running') {
this._state = { error: timeoutError };
this._forceAbortPromise.reject(timeoutError);
this._controller.abort(timeoutError);
}
}, deadline - monotonicTime());
} else {
timer = setTimeout(() => {
// TODO: migrate this to "progress.disableTimeout()".
if (this.metadata.pauseStartTime && !this.metadata.pauseEndTime)
return;
if (this._state === 'running') {
this._state = { error: timeoutError };
this._forceAbortPromise.reject(timeoutError);
this._controller.abort(timeoutError);
}
}, remaining);
}
}

try {
Expand Down
11 changes: 9 additions & 2 deletions packages/playwright-core/src/utils/isomorphic/timeoutRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ export async function raceAgainstDeadline<T>(cb: () => Promise<T>, deadline: num
return { result, timedOut: false };
}),
new Promise<{ timedOut: true }>(resolve => {
if (!deadline)
return;
const kMaxDeadline = 2147483647; // 2^31-1
const timeout = (deadline || kMaxDeadline) - monotonicTime();
timer = setTimeout(() => resolve({ timedOut: true }), timeout);
const timeout = deadline - monotonicTime();
if (timeout <= 0) {
// Deadline already passed, resolve immediately without scheduling a timer.
resolve({ timedOut: true });
return;
}
timer = setTimeout(() => resolve({ timedOut: true }), Math.min(timeout, kMaxDeadline));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we agreed to not set timer at all. Here we are at risk of firing in 0ms.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, clamping to 0 still fires the timer. Updated to check deadline - monotonicTime() <= 0 and resolve immediately with { timedOut: true } — no setTimeout at all when the deadline has already passed.

}),
]).finally(() => {
clearTimeout(timer);
Expand Down