diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 4502f223c3..ad48103e11 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -487,6 +487,11 @@ if(STRATA_BUILD_TESTS) target_include_directories(low_vram_rules_test PRIVATE include) add_test(NAME low_vram_rules_test COMMAND low_vram_rules_test) + # the serve watchdog's limits (STRATA_WATCHDOG_S / _IO_S) parse without undefined behaviour (#6861: negative seconds in the log) + add_executable(watchdog_limits_test src/core/watchdog_limits_test.cpp) + target_include_directories(watchdog_limits_test PRIVATE include) + add_test(NAME watchdog_limits_test COMMAND watchdog_limits_test) + # #4870 mid-coverage adaptive defaults (every 2 / 192 swaps / decay 0.92) are opt-in (STRATA_ADAPT_MID=1): header-only rule, no GPU add_executable(adapt_coverage_rules_test src/core/adapt_coverage_rules_test.cpp) target_include_directories(adapt_coverage_rules_test PRIVATE include) diff --git a/engine/include/strata/core/watchdog_limits.hpp b/engine/include/strata/core/watchdog_limits.hpp new file mode 100644 index 0000000000..0b8b4a0262 --- /dev/null +++ b/engine/include/strata/core/watchdog_limits.hpp @@ -0,0 +1,31 @@ +// strata/core/watchdog_limits.hpp - the serve watchdog's two time limits, parsed without undefined behaviour (header-only, no GPU). +// +// STRATA_WATCHDOG_S (default 60, 0 = the watchdog is off) and STRATA_WATCHDOG_IO_S (default 10 x the first, 0 = no allowance while the file +// tier is read) were read with std::atoi. atoi of a value outside int is undefined: #6861's server log printed "no progress for +// -1195029860 s" and "no step finished for -1907795513 s ... waiting up to 2079576072 s". A negative limit made the first silent second +// "stuck"; so the limits are parsed with strtol, an unparsable or out-of-range value falls back to the default, and the messages print the +// seconds the engine really waited. +#pragma once + +#include +#include +#include + +namespace strata::core { + +/// Seconds of a watchdog variable: `env` null or empty -> `def`; a whole non-negative number up to `max_s` -> itself; anything else (text after +/// the digits, a negative number, a value past max_s or past long) -> `def`. `*bad` (optional) is set when a value was given and refused. +inline int watchdog_seconds(const char* env, int def, bool* bad = nullptr, int max_s = 7 * 24 * 3600) noexcept { + if (bad != nullptr) *bad = false; + if (env == nullptr || env[0] == '\0') return def; + char* end = nullptr; + errno = 0; + const long v = std::strtol(env, &end, 10); + if (errno != 0 || end == env || *end != '\0' || v < 0 || v > (long) max_s) { + if (bad != nullptr) *bad = true; + return def; + } + return (int) v; +} + +} // namespace strata::core diff --git a/engine/src/core/watchdog_limits_test.cpp b/engine/src/core/watchdog_limits_test.cpp new file mode 100644 index 0000000000..ff55166ccd --- /dev/null +++ b/engine/src/core/watchdog_limits_test.cpp @@ -0,0 +1,28 @@ +// src/core/watchdog_limits_test.cpp - the serve watchdog's limits parse (#6861: negative "elapsed" seconds from atoi of an out-of-range value). +#include "strata/core/watchdog_limits.hpp" + +#include + +static int fails = 0; +#define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++fails; } } while (0) + +int main() { + using strata::core::watchdog_seconds; + bool bad = true; + CHECK(watchdog_seconds(nullptr, 60, &bad) == 60 && !bad); + CHECK(watchdog_seconds("", 60, &bad) == 60 && !bad); + CHECK(watchdog_seconds("0", 60, &bad) == 0 && !bad); // 0 = off, as documented + CHECK(watchdog_seconds("180", 60, &bad) == 180 && !bad); + CHECK(watchdog_seconds("604800", 60, &bad) == 604800 && !bad); // a week: the largest accepted + // the values atoi turned into garbage or negatives + CHECK(watchdog_seconds("3099937436", 60, &bad) == 60 && bad); // past int: atoi gave -1195029860 on the reporter's engine + CHECK(watchdog_seconds("2387171783", 60, &bad) == 60 && bad); // -1907795513 + CHECK(watchdog_seconds("99999999999999999999", 60, &bad) == 60 && bad); + CHECK(watchdog_seconds("-5", 60, &bad) == 60 && bad); + CHECK(watchdog_seconds("604801", 60, &bad) == 60 && bad); + CHECK(watchdog_seconds("12s", 60, &bad) == 60 && bad); + CHECK(watchdog_seconds("abc", 600, &bad) == 600 && bad); + CHECK(watchdog_seconds(" 5", 60, &bad) == 5 && !bad); // strtol skips leading blanks, as atoi did + if (fails == 0) std::printf("watchdog_limits_test: ok\n"); + return fails == 0 ? 0 : 1; +} diff --git a/engine/src/program/generate.cpp b/engine/src/program/generate.cpp index ef365b79a1..ed78a84d93 100644 --- a/engine/src/program/generate.cpp +++ b/engine/src/program/generate.cpp @@ -37,6 +37,7 @@ #include "strata/core/gfx12_chunk.hpp" #include "strata/core/nv_chunk.hpp" #include "strata/core/nv_defaults.hpp" +#include "strata/core/watchdog_limits.hpp" #include "strata/core/adapt_coverage_rules.hpp" #include "strata/core/low_vram_rules.hpp" #include "strata/kernels/gfx_arch.hpp" @@ -16245,12 +16246,16 @@ int main(int argc, char** argv) { // GPU spinning forever. STRATA_WATCHDOG_S=0 turns it off. Issue #31: before it does, it reports what every // part was doing (stall_report), so one occurrence says where the wait is. { - const char* ws = std::getenv("STRATA_WATCHDOG_S"); - const int limit = ws ? std::atoi(ws) : 60; // one step (a prompt layer, a verify window) takes seconds + // #6861: parsed with strtol (watchdog_limits.hpp): atoi of a value outside int printed "no progress for -1195029860 s" + bool ws_bad = false, wio_bad = false; + const int limit = strata::core::watchdog_seconds(std::getenv("STRATA_WATCHDOG_S"), 60, &ws_bad); // one step (a prompt layer, a verify window) takes seconds // #1407: while the OS is still reading the file tier (major faults / bytes read advance), a silent step is // slow, not stuck; it gets up to STRATA_WATCHDOG_IO_S (default 10 x the limit) of such time. 0 = no allowance. - const char* wio = std::getenv("STRATA_WATCHDOG_IO_S"); - const int io_limit = wio ? std::atoi(wio) : limit * 10; + const int io_limit = strata::core::watchdog_seconds(std::getenv("STRATA_WATCHDOG_IO_S"), limit * 10, &wio_bad); + if (ws_bad || wio_bad) + std::fprintf(stderr, "strata serve: %s%s is not a whole number of seconds (0 to 604800): using %d s%s\n", ws_bad ? "STRATA_WATCHDOG_S" : "", + wio_bad ? (ws_bad ? " and STRATA_WATCHDOG_IO_S" : "STRATA_WATCHDOG_IO_S") : "", ws_bad ? 60 : io_limit, + ws_bad && wio_bad ? " (and 10 x that for the file tier)" : ""); if (limit > 0) std::thread([limit, io_limit] { strata::platform::name_this_thread("strata-watchdog"); @@ -16288,9 +16293,9 @@ int main(int argc, char** argv) { } // a blocking step's explicit allowance (session files): still within it, not yet stuck if (strata::core::progress_now_ms() < p.allow_until_ms.load()) continue; - std::fprintf(stderr, "strata serve: no progress for %d s during a request (%s) - stopping " + std::fprintf(stderr, "strata serve: no progress for %lld s during a request (%s) - stopping " "the engine so the server starts it again (issue #29)%s\n", - limit, stage_text().c_str(), + (long long) std::chrono::duration_cast(now - since).count(), stage_text().c_str(), launch_blocking() ? "; CUDA_LAUNCH_BLOCKING=1 is set, and no verify window can " "finish with it (#1341)" : ""); stall_report(stderr, p.ticks.load() - ticks_at);