-
Notifications
You must be signed in to change notification settings - Fork 10
Crash fix in case of forks: remove usage of pthread_once #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
r1viollet
wants to merge
4
commits into
main
Choose a base branch
from
r1viollet/pthread_once_crash_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. This product includes software | ||
| // developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present | ||
| // Datadog, Inc. | ||
|
|
||
| // Simple test to verify pthread key initialization works across fork() | ||
| // This tests the atomic-based pthread_once replacement | ||
|
|
||
| #include <pthread.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <sys/types.h> | ||
| #include <sys/wait.h> | ||
| #include <unistd.h> | ||
|
|
||
| // Forward declare the C++ functions we need | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| void *AllocationTracker_get_tl_state(void); | ||
| void *AllocationTracker_init_tl_state(void); | ||
| void AllocationTracker_ensure_key_initialized(void); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| // Thread function to test TLS in new thread | ||
| static void *thread_test_func(void *arg) { | ||
| int *result = (int *)arg; | ||
|
|
||
| void *thread_state = AllocationTracker_get_tl_state(); | ||
|
|
||
| if (thread_state != NULL) { | ||
| fprintf(stderr, "FAIL: New thread expected NULL TLS, got %p\n", | ||
| thread_state); | ||
| *result = 1; | ||
| return NULL; | ||
| } | ||
|
|
||
| *result = 0; | ||
| return NULL; | ||
| } | ||
|
|
||
| int main(void) { | ||
| // Initialize pthread key before using it | ||
| AllocationTracker_ensure_key_initialized(); | ||
|
|
||
| // Initialize pthread key and create TLS data in parent | ||
| void *parent_state = AllocationTracker_init_tl_state(); | ||
| if (parent_state == NULL) { | ||
| fprintf(stderr, "FAIL: Parent init_tl_state() returned NULL\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| void *retrieved = AllocationTracker_get_tl_state(); | ||
| if (retrieved != parent_state) { | ||
| fprintf(stderr, "FAIL: Parent get_tl_state() mismatch\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| // Test fork - this is where the old pthread_once bug would crash | ||
| fflush(stdout); | ||
| fflush(stderr); | ||
|
|
||
| pid_t pid = fork(); | ||
| if (pid == -1) { | ||
| perror("FAIL: fork"); | ||
| return 1; | ||
| } | ||
|
|
||
| if (pid == 0) { | ||
| // Child process - test pthread key works after fork | ||
| void *child_state = AllocationTracker_init_tl_state(); | ||
| if (child_state == NULL) { | ||
| fprintf(stderr, "FAIL: Child init_tl_state() returned NULL\n"); | ||
| _exit(1); | ||
| } | ||
|
|
||
| void *child_retrieved = AllocationTracker_get_tl_state(); | ||
| if (child_retrieved != child_state) { | ||
| fprintf(stderr, "FAIL: Child pthread key broken after fork\n"); | ||
| _exit(2); | ||
| } | ||
|
|
||
| // Test that a new thread starts with NULL TLS | ||
| pthread_t thread; | ||
| int thread_result = -1; | ||
| if (pthread_create(&thread, NULL, thread_test_func, &thread_result) != 0) { | ||
| fprintf(stderr, "FAIL: pthread_create failed\n"); | ||
| _exit(3); | ||
| } | ||
|
|
||
| pthread_join(thread, NULL); | ||
| if (thread_result != 0) { | ||
| fprintf(stderr, "FAIL: Thread test failed - new thread TLS not NULL\n"); | ||
| _exit(4); | ||
| } | ||
|
|
||
| _exit(0); | ||
| } | ||
|
|
||
| // Parent waits for child | ||
| int status; | ||
| waitpid(pid, &status, 0); | ||
|
|
||
| if (!WIFEXITED(status)) { | ||
| if (WIFSIGNALED(status)) { | ||
| fprintf(stderr, | ||
| "FAIL: Child crashed with signal %d (pthread_once ABI bug!)\n", | ||
| WTERMSIG(status)); | ||
| } else { | ||
| fprintf(stderr, "FAIL: Child did not exit normally\n"); | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| int exit_code = WEXITSTATUS(status); | ||
| if (exit_code != 0) { | ||
| const char *reason = "unknown"; | ||
| if (exit_code == 1) | ||
| reason = "init_tl_state failed"; | ||
| else if (exit_code == 2) | ||
| reason = "pthread key broken after fork"; | ||
| else if (exit_code == 3) | ||
| reason = "pthread_create failed"; | ||
| else if (exit_code == 4) | ||
| reason = "new thread TLS not NULL"; | ||
| fprintf(stderr, "FAIL: Child exited with code %d (%s)\n", exit_code, | ||
| reason); | ||
| return 1; | ||
| } | ||
|
|
||
| // Verify parent still works after fork | ||
| void *parent_after = AllocationTracker_get_tl_state(); | ||
| if (parent_after != parent_state) { | ||
| fprintf(stderr, "FAIL: Parent TLS corrupted after fork\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| // All tests passed - silent success for CI | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. This product includes software | ||
| // developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present | ||
| // Datadog, Inc. | ||
|
|
||
| // C++ wrapper to expose AllocationTracker functions to C test | ||
|
|
||
| #include "lib/allocation_tracker.hpp" | ||
|
|
||
| extern "C" { | ||
|
|
||
| void *AllocationTracker_get_tl_state(void) { | ||
| return ddprof::AllocationTracker::get_tl_state(); | ||
| } | ||
|
|
||
| void AllocationTracker_ensure_key_initialized(void) { | ||
| ddprof::AllocationTracker::ensure_key_initialized(); | ||
| } | ||
|
|
||
| void *AllocationTracker_init_tl_state(void) { | ||
| return ddprof::AllocationTracker::init_tl_state(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might trigger with a relaxed order, but at least I will know about it
I might change mem order