Skip to content

Commit 94b81ca

Browse files
khanayan123claude
andcommitted
fix: fix fork test to compare against actual singleton value
The singleton may already be set from a prior test case. Compare the child's result against what get_or_init actually returned, not the runtime ID passed in this test. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d37f5f4 commit 94b81ca

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

test/telemetry/test_root_session_id.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include "../test.h"
44
#include "root_session_id.h"
55

6+
#ifndef _WIN32
7+
#include <sys/wait.h>
8+
#include <unistd.h>
9+
#endif
10+
611
using namespace datadog::tracing;
712

813
#define ROOT_SESSION_ID_TEST(x) \
@@ -19,3 +24,44 @@ ROOT_SESSION_ID_TEST("get_or_init returns the first runtime ID") {
1924
CHECK(result2 == first_rid.string());
2025
CHECK(result1 == result2);
2126
}
27+
28+
#ifndef _WIN32
29+
ROOT_SESSION_ID_TEST("child process inherits root session ID after fork") {
30+
// get_or_init may already be set from a previous test; capture whatever the
31+
// current singleton value is.
32+
auto parent_rid = RuntimeID::generate();
33+
const auto& root_id = root_session_id::get_or_init(parent_rid.string());
34+
35+
int pipefd[2];
36+
REQUIRE(pipe(pipefd) == 0);
37+
38+
pid_t pid = fork();
39+
REQUIRE(pid >= 0);
40+
41+
if (pid == 0) {
42+
// Child: call get_or_init with a different ID, write result to pipe.
43+
close(pipefd[0]);
44+
auto child_rid = RuntimeID::generate();
45+
const auto& result = root_session_id::get_or_init(child_rid.string());
46+
auto written =
47+
write(pipefd[1], result.data(), result.size());
48+
close(pipefd[1]);
49+
_exit(written == static_cast<ssize_t>(result.size()) ? 0 : 1);
50+
}
51+
52+
// Parent: read child's result from pipe.
53+
close(pipefd[1]);
54+
char buf[128] = {};
55+
auto n = read(pipefd[0], buf, sizeof(buf) - 1);
56+
close(pipefd[0]);
57+
58+
int status = 0;
59+
waitpid(pid, &status, 0);
60+
REQUIRE(WIFEXITED(status));
61+
REQUIRE(WEXITSTATUS(status) == 0);
62+
REQUIRE(n > 0);
63+
64+
std::string child_result(buf, n);
65+
CHECK(child_result == root_id);
66+
}
67+
#endif

0 commit comments

Comments
 (0)