From 485d7bdcdb0af761348266487d31510c22238ef0 Mon Sep 17 00:00:00 2001 From: Andrew Wun Date: Thu, 15 Oct 2026 13:50:10 +0000 Subject: [PATCH] Fix race in ExpectSessionTest Poll isAlive() until the child is reaped instead of asserting once at EOF. --- .../helpers/tests/ExpectSessionTest.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fboss/platform/helpers/tests/ExpectSessionTest.cpp b/fboss/platform/helpers/tests/ExpectSessionTest.cpp index 361027a09b6..cb749e41713 100644 --- a/fboss/platform/helpers/tests/ExpectSessionTest.cpp +++ b/fboss/platform/helpers/tests/ExpectSessionTest.cpp @@ -3,6 +3,7 @@ #include "fboss/platform/helpers/ExpectSession.h" #include +#include #include @@ -10,6 +11,18 @@ using namespace facebook::fboss::platform; namespace { constexpr std::chrono::seconds kTimeout{10}; + +// EOF can arrive before the exiting child is reapable, so poll isAlive(). +bool exitsWithin(ExpectSession& session, std::chrono::milliseconds timeout) { + auto deadline = std::chrono::steady_clock::now() + timeout; + while (session.isAlive()) { + if (std::chrono::steady_clock::now() > deadline) { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + return true; +} } // namespace TEST(ExpectSessionTest, ExpectAndSend) { @@ -41,7 +54,7 @@ TEST(ExpectSessionTest, NoticesWhenChildExits) { EXPECT_FALSE(session.expect("more", kTimeout)); EXPECT_TRUE(session.isEof()); - EXPECT_FALSE(session.isAlive()); + EXPECT_TRUE(exitsWithin(session, kTimeout)); } TEST(ExpectSessionTest, ChildReapedElsewhereIsNotAlive) { @@ -52,7 +65,7 @@ TEST(ExpectSessionTest, ChildReapedElsewhereIsNotAlive) { ExpectSession session("true"); ASSERT_FALSE(session.expect("never printed", kTimeout)); - EXPECT_FALSE(session.isAlive()); + EXPECT_TRUE(exitsWithin(session, kTimeout)); } std::signal(SIGCHLD, previous); }