From 37ba65e34e54bd755741dbdd7125cb21fb84c2f8 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 31 Mar 2022 08:26:44 +0200 Subject: [PATCH 1/2] Cap the maximum value passed to setTimeout --- bin/wasm-node/CHANGELOG.md | 4 ++++ .../javascript/src/worker/bindings-smoldot-light.ts | 7 +++++++ bin/wasm-node/rust/src/bindings.rs | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index c2c409b6b7..976b8f7a1f 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixed + +- Fixed a `TimeoutOverflowWarning` caused by calling `setTimeout` with a value that is too large. + ## 0.6.10 - 2022-03-29 ### Fixed diff --git a/bin/wasm-node/javascript/src/worker/bindings-smoldot-light.ts b/bin/wasm-node/javascript/src/worker/bindings-smoldot-light.ts index fbe4a3ea19..95da6d07f8 100644 --- a/bin/wasm-node/javascript/src/worker/bindings-smoldot-light.ts +++ b/bin/wasm-node/javascript/src/worker/bindings-smoldot-light.ts @@ -110,6 +110,13 @@ export default function (config: Config): compat.WasmModuleImports { start_timer: (id: number, ms: number) => { const instance = config.instance!; + // In both NodeJS and browsers, if `setTimeout` is called with a value larger than + // 2147483647, the delay is for some reason instead set to 1. + // As mentioned in the documentation of `start_timer`, it is acceptable to end the + // timer before the given number of milliseconds has passed. + if (ms > 2147483647) + ms = 2147483647; + // In browsers, `setTimeout` works as expected when `ms` equals 0. However, NodeJS // requires a minimum of 1 millisecond (if `0` is passed, it is automatically replaced // with `1`) and wants you to use `setImmediate` instead. diff --git a/bin/wasm-node/rust/src/bindings.rs b/bin/wasm-node/rust/src/bindings.rs index bc8b225ad4..ba58c057f0 100644 --- a/bin/wasm-node/rust/src/bindings.rs +++ b/bin/wasm-node/rust/src/bindings.rs @@ -144,6 +144,10 @@ extern "C" { /// After at least `milliseconds` milliseconds have passed, must call [`timer_finished`] with /// the `id` passed as parameter. /// + /// It is not a logic error to call [`timer_finished`] *before* `milliseconds` milliseconds + /// have passed, and this will likely cause smoldot to restart a new timer for the remainder + /// of the duration. + /// /// When [`timer_finished`] is called, the value of [`monotonic_clock_ms`] must have increased /// by at least the given number of `milliseconds`. /// From e59d9bb63081272404d5c64c5f72b1712d6e1f8c Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 31 Mar 2022 08:27:48 +0200 Subject: [PATCH 2/2] PR number --- bin/wasm-node/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index 976b8f7a1f..9eeb970ff7 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -4,7 +4,7 @@ ### Fixed -- Fixed a `TimeoutOverflowWarning` caused by calling `setTimeout` with a value that is too large. +- Fixed a `TimeoutOverflowWarning` caused by calling `setTimeout` with a value that is too large. ([#2188](https://github.com/paritytech/smoldot/pull/2188)) ## 0.6.10 - 2022-03-29