8000 Free rb_native_thread memory at fork by peterzhu2118 · Pull Request #13591 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

Free rb_native_thread memory at fork #13591

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

Merged
merged 1 commit into from
Jun 12, 2025

Conversation

peterzhu2118
Copy link
Member

We never freed any resources of rb_native_thread at fork because it would cause it to hang. This is because it called rb_native_cond_destroy for condition variables. We can't call rb_native_cond_destroy here because according to the specs of pthread_cond_destroy:

Attempting to destroy a condition variable upon which other threads
are currently blocked results in undefined behavior.

Specifically, glibc's pthread_cond_destroy waits on all the other listeners. Since after forking all the threads are dead, the condition variable's listeners will never wake up, so it will hang forever.

This commit changes it to only free the memory and none of the condition variables.

We never freed any resources of rb_native_thread at fork because it would
cause it to hang. This is because it called rb_native_cond_destroy for
condition variables.  We can't call rb_native_cond_destroy here because
according to the specs of pthread_cond_destroy:

    Attempting to destroy a condition variable upon which other threads
    are currently blocked results in undefined behavior.

Specifically, glibc's pthread_cond_destroy waits on all the other listeners.
Since after forking all the threads are dead, the condition variable's
listeners will never wake up, so it will hang forever.

This commit changes it to only free the memory and none of the condition
variables.
@peterzhu2118 peterzhu2118 force-pushed the native-thread-destroy-atfork branch from 0ba2a48 to eb0f8b6 Compare June 11, 2025 21:16
Copy link

Tests Failed

✖️no tests failed ✔️61992 tests passed(3 flakes)

@peterzhu2118 peterzhu2118 merged commit 6e36841 into ruby:master Jun 12, 2025
85 of 87 checks passed
@peterzhu2118 peterzhu2118 deleted the native-thread-destroy-atfork branch June 12, 2025 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0