Skip to content

Commit 5f0b697

Browse files
committed
fix(ci): bound and diagnose Apple runner hangs
1 parent 49fef6a commit 5f0b697

9 files changed

Lines changed: 275 additions & 101 deletions

File tree

‎ci/meson/native/meson.build‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,23 @@ elif build_mode == 'profile'
409409
]
410410
endif
411411

412+
# The Apple runner watchdog can interrupt dyld while sanitizer initialization
413+
# holds loader locks. Compile its complete call chain without instrumentation so
414+
# the SIGALRM handler remains limited to async-signal-safe system calls.
415+
apple_runner_watchdog_libs = []
416+
if is_macos
417+
apple_runner_watchdog_lib = static_library('apple_runner_watchdog',
418+
files('../../../tests/shared/apple_runner_watchdog.cpp'),
419+
cpp_args: runner_cpp_args + [
420+
path_norm_I_src,
421+
'-fno-sanitize=all',
422+
],
423+
implicit_include_directories: false,
424+
install: false
425+
)
426+
apple_runner_watchdog_libs += [apple_runner_watchdog_lib]
427+
endif
428+
412429
# ============================================================================
413430
# Legacy aliases for backward compatibility
414431
# ============================================================================

‎ci/meson/streaming_runner.py‎

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
import os
15+
import sys
1516
import threading
1617
import time
1718
from dataclasses import dataclass
@@ -40,6 +41,45 @@
4041
from ci.util.timestamp_print import ts_print as _ts_print
4142

4243

44+
_DEFAULT_TEST_PROCESS_TIMEOUT_SECONDS = 600
45+
_MACOS_TEST_PROCESS_TIMEOUT_SECONDS = 90
46+
47+
48+
def _test_process_timeout_seconds() -> int:
49+
"""Return the outer runner timeout for the current platform.
50+
51+
Apple runners have their own 20-second watchdog. Keep a wider Python
52+
deadline so normal watchdog diagnostics can flush, while still bounding a
53+
pre-main sanitizer/dyld stall that cannot arm the in-process watchdog.
54+
"""
55+
if sys.platform == "darwin":
56+
return _MACOS_TEST_PROCESS_TIMEOUT_SECONDS
57+
return _DEFAULT_TEST_PROCESS_TIMEOUT_SECONDS
58+
59+
60+
def _wait_for_test_process(proc: RunningProcess) -> TestResult:
61+
"""Wait for a runner and retain its captured output on every failure.
62+
63+
``RunningProcess.wait()`` raises on timeout after killing the child. The
64+
captured stream remains available on ``proc.stdout``; include it in the
65+
returned failure so phase markers and sanitizer diagnostics reach CI logs.
66+
"""
67+
try:
68+
returncode = proc.wait(echo=False)
69+
except KeyboardInterrupt as ki:
70+
handle_keyboard_interrupt(ki)
71+
raise
72+
except Exception as error:
73+
captured = str(proc.stdout)
74+
output = f"[MESON] Test execution error: {error}"
75+
if captured:
76+
output += f"\n[MESON] Captured runner output:\n{captured}"
77+
return TestResult(success=False, output=output)
78+
79+
captured = str(proc.stdout)
80+
return TestResult(success=returncode == 0, output=captured)
81+
82+
4383
def validate_test_artifact(
4484
test_path: Path, build_start_time: float
4585
) -> Optional[TestResult]:
@@ -298,7 +338,7 @@ def test_callback(test_path: Path) -> TestResult:
298338
proc = RunningProcess(
299339
cmd,
300340
cwd=ctx.source_dir,
301-
timeout=600,
341+
timeout=_test_process_timeout_seconds(),
302342
auto_run=True,
303343
check=False,
304344
env=env,
@@ -308,17 +348,16 @@ def test_callback(test_path: Path) -> TestResult:
308348
with active_procs_lock:
309349
active_procs.add(proc)
310350
try:
311-
returncode = proc.wait(echo=False)
351+
result = _wait_for_test_process(proc)
312352
finally:
313353
with active_procs_lock:
314354
active_procs.discard(proc)
315-
captured = str(proc.stdout)
316355

317-
if returncode != 0:
356+
if not result.success:
318357
with failed_outputs_lock:
319-
failed_test_outputs[test_path.stem] = captured
358+
failed_test_outputs[test_path.stem] = result.output
320359

321-
return TestResult(success=returncode == 0, output=captured)
360+
return result
322361

323362
except KeyboardInterrupt as ki:
324363
handle_keyboard_interrupt(ki)
@@ -327,10 +366,13 @@ def test_callback(test_path: Path) -> TestResult:
327366
output="[MESON] Test interrupted by user",
328367
)
329368
except Exception as e:
330-
return TestResult(
369+
result = TestResult(
331370
success=False,
332371
output=f"[MESON] Test execution error: {e}",
333372
)
373+
with failed_outputs_lock:
374+
failed_test_outputs[test_path.stem] = result.output
375+
return result
334376

335377
def _kill_active_procs() -> None:
336378
"""Kill all running test subprocesses (called on halt)."""

‎ci/tests/test_streaming_runner_artifact_validation.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
from ci.meson.streaming import (
2626
TestResult as StreamingTestResult,
2727
)
28-
from ci.meson.streaming_runner import validate_test_artifact # noqa: E402
28+
from ci.meson.streaming_runner import ( # noqa: E402
29+
_test_process_timeout_seconds,
30+
_wait_for_test_process,
31+
validate_test_artifact,
32+
)
2933

3034

3135
_T = TypeVar("_T")
@@ -132,6 +136,26 @@ def test_dll_touch_wait_is_bounded(self) -> None:
132136
done.set()
133137
self.assertTrue(_wait_for_dll_touch(done, timeout=0.0))
134138

139+
def test_runner_timeout_preserves_captured_output(self) -> None:
140+
"""Watchdog phase markers survive the outer Python timeout."""
141+
process = MagicMock()
142+
process.wait.side_effect = TimeoutError("runner stalled")
143+
process.stdout = "[FASTLED RUNNER] phase: dynamic-load\n"
144+
145+
result = _wait_for_test_process(process)
146+
147+
self.assertFalse(result.success)
148+
self.assertIn("runner stalled", result.output)
149+
self.assertIn("phase: dynamic-load", result.output)
150+
151+
def test_macos_runner_timeout_is_bounded(self) -> None:
152+
"""A pre-main Apple stall cannot consume a ten-minute worker slot."""
153+
with patch("ci.meson.streaming_runner.sys.platform", "darwin"):
154+
self.assertEqual(90, _test_process_timeout_seconds())
155+
156+
with patch("ci.meson.streaming_runner.sys.platform", "linux"):
157+
self.assertEqual(600, _test_process_timeout_seconds())
158+
135159
def test_dll_touch_timeout_is_reported_in_compile_output(self) -> None:
136160
"""Timeout diagnostics propagate to callers and persisted failure logs."""
137161
process = MagicMock()

‎examples/meson.build‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ example_runner_exe = executable('example_runner',
156156
files('../tests/shared/example_runner.cpp'),
157157
cpp_args: runner_cpp_args + [path_norm_I_src, path_norm_I_stub, path_norm_I_tests],
158158
link_args: runner_link_args,
159-
link_with: [crash_handler_lib], # Shared crash handler from root meson.build
159+
link_with: [crash_handler_lib] + apple_runner_watchdog_libs,
160160
implicit_include_directories: false,
161161
install: false
162162
)

‎src/platforms/apple/run_example.hpp‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424
#include <mach-o/dyld.h> // For _NSGetExecutablePath
2525
// IWYU pragma: end_keep
2626

27-
// Timeout watchdog for hung example detection
28-
#include "timeout_watchdog.h" // ok include path
2927
#include "fl/stl/noexcept.h"
28+
#include "platforms/apple/runner_watchdog.hpp"
3029

3130
int main(int argc, char** argv) FL_NO_EXCEPT {
31+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kStartup);
32+
apple_runner_watchdog::setup();
33+
3234
// Setup crash handler BEFORE loading any shared libraries
35+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kCrashHandlerSetup);
3336
runner_setup_crash_handler();
3437

35-
// Setup timeout watchdog to detect hung examples
36-
timeout_watchdog::setup(); // Default: 20 seconds, configurable via FASTLED_TEST_TIMEOUT
37-
38+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kPathResolution);
3839
std::string so_path;
3940
const std::string shared_lib_ext = ".dylib";
4041

@@ -75,17 +76,21 @@ int main(int argc, char** argv) FL_NO_EXCEPT {
7576

7677
// Load shared library with RTLD_NOW for immediate symbol resolution
7778
// This helps ASAN properly track symbols from the loaded library
79+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kDynamicLoad);
7880
void* handle = dlopen(so_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
7981
if (!handle) {
8082
std::cout << "Error: Failed to load " << so_path << " (" << dlerror() << ")" << std::endl;
83+
apple_runner_watchdog::cancel();
8184
return 1;
8285
}
8386

8487
// Get run_example function
88+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kSymbolLookup);
8589
RunExampleFunc run_example = (RunExampleFunc)dlsym(handle, "run_example");
8690
if (!run_example) {
8791
std::cout << "Error: Failed to find run_example() in " << so_path << " (" << dlerror() << ")" << std::endl;
8892
dlclose(handle);
93+
apple_runner_watchdog::cancel();
8994
return 1;
9095
}
9196

@@ -109,13 +114,15 @@ int main(int argc, char** argv) FL_NO_EXCEPT {
109114
}
110115

111116
// Call example function with arguments
117+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kInvocation);
112118
int example_result = run_example(example_argc, example_argv);
113119

114120
// Cleanup: Skip dlclose when running with AddressSanitizer
115121
// ASAN runs leak detection at program exit. If we dlclose() the shared library
116122
// before that, ASAN cannot symbolize addresses from the unloaded library,
117123
// resulting in "<unknown module>" in stack traces.
118124
// See: https://github.com/google/sanitizers/issues/899
125+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kTeardown);
119126
#if !defined(__SANITIZE_ADDRESS__) && !defined(__has_feature)
120127
dlclose(handle);
121128
#elif defined(__has_feature)
@@ -124,6 +131,8 @@ int main(int argc, char** argv) FL_NO_EXCEPT {
124131
#endif
125132
#endif
126133

134+
apple_runner_watchdog::set_phase(apple_runner_watchdog::Phase::kCompleted);
135+
apple_runner_watchdog::cancel();
127136
return example_result;
128137
}
129138

0 commit comments

Comments
 (0)