Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix race condition in signal handler query #13712

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 3 commits into from
Jun 28, 2025

Conversation

sferik
Copy link
Contributor

@sferik sferik commented Jun 26, 2025

Introduced a static mutex when checking non-POSIX signal handlers, making the operation thread-safe and preventing races around the signal retrieval and restoration. Fixes TODO added by @luke-gruber in 1d4822a#diff-47e173231fbc1ddb6c555f7116c157cb32ab63d04e85047b55ff378e71fb965eR679.

@luke-gruber
Copy link
Contributor

Thanks for taking a look at this! It looks like Windows stuff is failing because there's no static initializer for Window's version of rb_nativethread_lock_t (see thread_native.h for internals). You can make it file local and init in during Init_signal.

This mutex also needs to be reinitialized after a call to fork or else the child might hit a deadlock. In rb_thread_atfork_internal() you can add a rb_signal_atfork() and do it there.

@sferik
Copy link
Contributor Author

sferik commented Jun 26, 2025

@luke-gruber Thanks for the feedback! I will work on making these changes and update the PR.

@sferik sferik force-pushed the fix-race-condition-in-signal-handler-query branch 3 times, most recently from 1a0bebb to 3870590 Compare June 27, 2025 01:37
@sferik
Copy link
Contributor Author

sferik commented Jun 27, 2025

@luke-gruber I've made the changes you've recommend:

  • Initialized this mutex during signal setup so it’s ready before any handler queries happen.
  • Provided a fork hook to reinitialize the lock after forking and invoked it from the thread fork path to prevent deadlocks in the child.
  • Tests now pass on all platforms, including Windows.

Please let me know if you have any additional feedback.

signal.c Outdated
@@ -1561,3 +1566,13 @@ Init_signal(void)

rb_enable_interrupt();
}

#if defined(HAVE_WORKING_FORK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition doesn’t look matching the above.
And the function is always needed, so the condition can be in the function body.

@sferik sferik force-pushed the fix-race-condition-in-signal-handler-query branch from 6d2387c to 0fa364c Compare June 28, 2025 01:46
@sferik
Copy link
Contributor Author

sferik commented Jun 28, 2025

I've made the allocation and initialization of sig_check_lock conditional on POSIX_SIGNAL and SIG_GET being defined. I've also changed the code to always define rb_signal_atfork, moving the conditions inside the function body.

Please let me know if I've gotten anything wrong and if you'd like me to make any other changes (e.g. squashing these changes into a single commit). Thanks so much for reviewing this code!

Copy link
Member

@nobu nobu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, do you know any platform that is still alive, neither POSIX nor Windows?

@sferik
Copy link
Contributor Author

sferik commented Jun 28, 2025

I guess some bespoke real-time OSes, stuff within the government (e.g. systems that control rocket ships), and very old mainframes that are still technically "alive", for some definition of that word, but just about everything else is POSIX or Windows.

@nobu
Copy link
Member

nobu commented Jun 28, 2025

Ah, sorry, I meant any of the platforms supported by ruby.

@nobu nobu merged commit eab4a0b into ruby:master Jun 28, 2025
84 checks passed
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.

3 participants