From 9a0014619e77da1d4fb7bb7fb1f72bd42f57f7c5 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Sat, 25 Jul 2026 08:42:20 -0700 Subject: [PATCH] Cover caller-stream TLS semantics (#21291) Summary: Add focused coverage for the caller-stream contract relied on by external CUDA backends. Verify that an explicitly selected null/default stream remains distinct from no guard, and that selections are isolated per execution thread. Keep the fbcode and xplat mirrors identical. Differential Revision: D113382078 --- .../slim/cuda/test/test_cuda_stream_guard.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp b/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp index df618a7b8e9..d06a22e4594 100644 --- a/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp +++ b/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp @@ -12,6 +12,7 @@ #include #include +#include #include using namespace executorch::backends::cuda; @@ -296,6 +297,30 @@ TEST(CallerStreamGuardTest, GuardSelectsThenRestores) { EXPECT_FALSE(getCallerStream().has_value()); } +TEST(CallerStreamGuardTest, ExplicitNullStreamIsStillSelected) { + CallerStreamGuard guard(nullptr); + ASSERT_TRUE(getCallerStream().has_value()); + EXPECT_EQ(*getCallerStream(), nullptr); +} + +TEST(CallerStreamGuardTest, SelectionIsThreadLocal) { + const cudaStream_t selected = fake_stream(0); + CallerStreamGuard guard(selected); + + bool child_started_without_stream = false; + bool child_selected_own_stream = false; + std::thread child([&]() { + child_started_without_stream = !getCallerStream().has_value(); + CallerStreamGuard child_guard(fake_stream(1)); + child_selected_own_stream = getCallerStream() == fake_stream(1); + }); + child.join(); + + EXPECT_TRUE(child_started_without_stream); + EXPECT_TRUE(child_selected_own_stream); + EXPECT_EQ(getCallerStream(), selected); +} + TEST(CallerStreamGuardTest, NestedGuardsRestoreOuter) { const cudaStream_t outer = fake_stream(1); const cudaStream_t inner = fake_stream(2);