From 0d06d2cc662a1803980bd332856ac2478a2a3246 Mon Sep 17 00:00:00 2001 From: Siddartha Pothapragada Date: Wed, 16 Sep 2026 01:18:18 -0700 Subject: [PATCH] Skip the callback for an empty range in parallel_for_no_threadpool #22663 added NestedCallsPreserveOuterThreadNumber, which asserts that an empty nested range never invokes its callback, and parameterized it over both parallel_for implementations. It added the guard to the threadpool one in extension/threadpool/thread_parallel.cpp but not to this one, so the release build calls f(begin, end) unconditionally and the test fails with "Empty nested range invoked its callback". Debug builds pass only by accident: they iterate c10::irange(begin, end), which is empty, so the callback is skipped as a side effect. Checking begin == end up front makes both branches agree by construction rather than by coincidence, which is why the check is above the #ifdef rather than duplicated into the release arm. This is what has been holding viable/strict since Sep 14: unittest-release fails on linux, macOS and windows, which reddens the trunk workflow, and trunk is required by update-viablestrict. --- runtime/kernel/thread_parallel_interface.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/runtime/kernel/thread_parallel_interface.h b/runtime/kernel/thread_parallel_interface.h index 8cce610dcb4..d320576f097 100644 --- a/runtime/kernel/thread_parallel_interface.h +++ b/runtime/kernel/thread_parallel_interface.h @@ -31,6 +31,13 @@ inline bool parallel_for_no_threadpool( begin, end); ET_CHECK_OR_RETURN_FALSE(grain_size > 0, "grain_size = %" PRId64, grain_size); + // An empty range runs no work items, so the callback is not invoked. Checked + // here rather than only in the release branch below: the debug branch skips + // it as a side effect of iterating an empty range, and relying on that made + // the two branches disagree. + if (begin == end) { + return true; + } #ifndef NDEBUG // Go backwards through the range elementwise to catch code that // assumes parallel_for is in order like a regular for loop.