From f135b5f7a8924a2d9097bc669c522c60ad9fd05a Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 31 Jul 2023 09:42:08 -0700 Subject: [PATCH] fix: make sure `monotonicTime()` value is reasonable (#24518) https://github.com/microsoft/playwright/issues/24432 --- packages/playwright-core/src/utils/time.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/playwright-core/src/utils/time.ts b/packages/playwright-core/src/utils/time.ts index 9f0aba31a9..d00ee62e82 100644 --- a/packages/playwright-core/src/utils/time.ts +++ b/packages/playwright-core/src/utils/time.ts @@ -14,7 +14,19 @@ * limitations under the License. */ +// The `process.hrtime()` returns a time from some arbitrary +// date in the past; on certain systems, this is the time from the system boot. +// The `monotonicTime()` converts this to milliseconds. +// +// For a Linux server with uptime of 36 days, the `monotonicTime()` value +// will be 36 * 86400 * 1000 = 3_110_400_000, which is larger than +// the maximum value that `setTimeout` accepts as an argument: 2_147_483_647. +// +// To make the `monotonicTime()` a reasonable value, we anchor +// it to the time of the first import of this utility. +const initialTime = process.hrtime(); + export function monotonicTime(): number { - const [seconds, nanoseconds] = process.hrtime(); + const [seconds, nanoseconds] = process.hrtime(initialTime); return seconds * 1000 + (nanoseconds / 1000 | 0) / 1000; }