aws-c-common icon indicating copy to clipboard operation
aws-c-common copied to clipboard

clean up pending threads on fork

Open TingDaoK opened this issue 10 months ago • 1 comments

Issue #, and/or reason for changes (REQUIRED):

  • When the process gets forked, the threads are NOT get copied.
  • But we have a lazy thread join method may copy the state of the threads that needs to be joined from the parent process.
  • So, when the child process tries to clean up the global state of the threads to be cleaned, it picks the threads info copied from parent process, and it's meaningless to the child process, which leads to a segfault.
  • https://man7.org/linux/man-pages/man2/fork.2.html

Description of changes:

  • Try to register a handler to clean up the global state from the parent process before fork, so that child will not inherit the global state.
  • https://man7.org/linux/man-pages/man3/pthread_atfork.3.html

TODO

  • This is likely not gonna solve all issues for fork but at least, it will solve the segfault we see
  • What will happen to windows?
  • we need to dive much deep into the fork, and how to deal with it with multiple background threads, just throw out ideas:
    • don't allow it?
    • call exec()?
    • join all threads?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

TingDaoK avatar Jan 22 '25 22:01 TingDaoK

Codecov Report

:x: Patch coverage is 11.11111% with 8 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 84.11%. Comparing base (568f46b) to head (88ad60a). :warning: Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
source/thread_shared.c 0.00% 7 Missing :warning:
source/common.c 50.00% 1 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1181      +/-   ##
==========================================
- Coverage   84.23%   84.11%   -0.12%     
==========================================
  Files          57       57              
  Lines        5980     5988       +8     
==========================================
  Hits         5037     5037              
- Misses        943      951       +8     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Jan 24 '25 21:01 codecov-commenter