From 427d6c83e130ea4c3fd02d458c15a21688b94e17 Mon Sep 17 00:00:00 2001 From: Dengke Date: Wed, 22 Jan 2025 14:34:11 -0800 Subject: [PATCH 1/5] try to fix the fork crash --- source/common.c | 3 +++ source/thread_shared.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/source/common.c b/source/common.c index a391f4fcb..afc154413 100644 --- a/source/common.c +++ b/source/common.c @@ -389,6 +389,9 @@ void aws_common_library_init(struct aws_allocator *allocator) { } else { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: libnuma.so failed to load"); } + if (pthread_atfork(common_pthread_atfork_on_fork, NULL, NULL) != 0) { + AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: failed to register pthread at fork"); + } #endif } } diff --git a/source/thread_shared.c b/source/thread_shared.c index bd479a16e..3dc3aa19b 100644 --- a/source/thread_shared.c +++ b/source/thread_shared.c @@ -165,3 +165,17 @@ void aws_thread_pending_join_add(struct aws_linked_list_node *node) { void aws_thread_initialize_thread_management(void) { aws_linked_list_init(&s_pending_join_managed_threads); } + +void aws_pthread_atfork_on_fork_child(void) { + /** + * The handler after fork in the child process. + * + * Empty the s_pending_join_managed_threads, the list is created by the parent process and should not be used in + * child process. + */ + struct aws_linked_list empty; + aws_linked_list_init(&empty); + aws_mutex_lock(&s_managed_thread_lock); + aws_linked_list_swap_contents(&empty, &s_pending_join_managed_threads); + aws_mutex_unlock(&s_managed_thread_lock); +} From 398b4e939d445349524ee8cdf3351c660bb6d7a2 Mon Sep 17 00:00:00 2001 From: Dengke Date: Wed, 22 Jan 2025 14:38:31 -0800 Subject: [PATCH 2/5] woops --- include/aws/common/private/thread_shared.h | 5 +++++ source/common.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/aws/common/private/thread_shared.h b/include/aws/common/private/thread_shared.h index ca263e56c..e54047c46 100644 --- a/include/aws/common/private/thread_shared.h +++ b/include/aws/common/private/thread_shared.h @@ -36,4 +36,9 @@ AWS_COMMON_API void aws_thread_initialize_thread_management(void); */ AWS_COMMON_API size_t aws_thread_get_managed_thread_count(void); +/** + * The handler after fork in the child process. + */ +void aws_pthread_atfork_on_fork_child(void); + #endif /* AWS_COMMON_PRIVATE_THREAD_SHARED_H */ diff --git a/source/common.c b/source/common.c index afc154413..7cbfd589d 100644 --- a/source/common.c +++ b/source/common.c @@ -389,7 +389,7 @@ void aws_common_library_init(struct aws_allocator *allocator) { } else { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: libnuma.so failed to load"); } - if (pthread_atfork(common_pthread_atfork_on_fork, NULL, NULL) != 0) { + if (pthread_atfork(NULL, NULL, aws_pthread_atfork_on_fork_child) != 0) { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: failed to register pthread at fork"); } #endif From 387f53f6bcdc3bcbdda71505e9b083f772e7d787 Mon Sep 17 00:00:00 2001 From: Dengke Date: Wed, 22 Jan 2025 14:53:19 -0800 Subject: [PATCH 3/5] another option --- include/aws/common/private/thread_shared.h | 4 ++-- source/common.c | 2 +- source/thread_shared.c | 10 ++++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/aws/common/private/thread_shared.h b/include/aws/common/private/thread_shared.h index e54047c46..19e648319 100644 --- a/include/aws/common/private/thread_shared.h +++ b/include/aws/common/private/thread_shared.h @@ -37,8 +37,8 @@ AWS_COMMON_API void aws_thread_initialize_thread_management(void); AWS_COMMON_API size_t aws_thread_get_managed_thread_count(void); /** - * The handler after fork in the child process. + * The handler before fork in the parent process. */ -void aws_pthread_atfork_on_fork_child(void); +void aws_pthread_atfork_on_fork_prepare(void); #endif /* AWS_COMMON_PRIVATE_THREAD_SHARED_H */ diff --git a/source/common.c b/source/common.c index 7cbfd589d..539860ca2 100644 --- a/source/common.c +++ b/source/common.c @@ -389,7 +389,7 @@ void aws_common_library_init(struct aws_allocator *allocator) { } else { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: libnuma.so failed to load"); } - if (pthread_atfork(NULL, NULL, aws_pthread_atfork_on_fork_child) != 0) { + if (pthread_atfork(aws_pthread_atfork_on_fork_prepare, NULL, NULL) != 0) { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: failed to register pthread at fork"); } #endif diff --git a/source/thread_shared.c b/source/thread_shared.c index 3dc3aa19b..e3c414aaf 100644 --- a/source/thread_shared.c +++ b/source/thread_shared.c @@ -166,16 +166,18 @@ void aws_thread_initialize_thread_management(void) { aws_linked_list_init(&s_pending_join_managed_threads); } -void aws_pthread_atfork_on_fork_child(void) { +void aws_pthread_atfork_on_fork_prepare(void) { /** - * The handler after fork in the child process. + * The handler before fork in the parent process. * - * Empty the s_pending_join_managed_threads, the list is created by the parent process and should not be used in - * child process. + * Clean up the s_pending_join_managed_threads now, the list is created by the parent process and should not be used + * in child process. + * Fork will copy the list, but not the real threads. */ struct aws_linked_list empty; aws_linked_list_init(&empty); aws_mutex_lock(&s_managed_thread_lock); aws_linked_list_swap_contents(&empty, &s_pending_join_managed_threads); aws_mutex_unlock(&s_managed_thread_lock); + aws_thread_join_and_free_wrapper_list(&empty); } From 1c3f598053c0d0ec5a36a976b6b9a5dfeb677df6 Mon Sep 17 00:00:00 2001 From: Dengke Date: Thu, 23 Jan 2025 14:07:42 -0800 Subject: [PATCH 4/5] just not windows --- source/common.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/common.c b/source/common.c index 539860ca2..eed29174b 100644 --- a/source/common.c +++ b/source/common.c @@ -389,6 +389,9 @@ void aws_common_library_init(struct aws_allocator *allocator) { } else { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: libnuma.so failed to load"); } +#endif + +#ifndef AWS_OS_WINDOWS if (pthread_atfork(aws_pthread_atfork_on_fork_prepare, NULL, NULL) != 0) { AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "static: failed to register pthread at fork"); } From 88ad60a0ff4266f1e8380a843d2493f700ffa34e Mon Sep 17 00:00:00 2001 From: Dengke Date: Tue, 11 Mar 2025 16:02:05 -0700 Subject: [PATCH 5/5] don't explicitly release lock, just close the fd --- source/posix/cross_process_lock.c | 1 - 1 file changed, 1 deletion(-) diff --git a/source/posix/cross_process_lock.c b/source/posix/cross_process_lock.c index 1ef5d2b5f..f94834184 100644 --- a/source/posix/cross_process_lock.c +++ b/source/posix/cross_process_lock.c @@ -133,7 +133,6 @@ struct aws_cross_process_lock *aws_cross_process_lock_try_acquire( void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) { if (instance_lock) { - flock(instance_lock->locked_fd, LOCK_UN); close(instance_lock->locked_fd); AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock file released for fd %d", instance_lock->locked_fd); aws_mem_release(instance_lock->allocator, instance_lock);