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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void (*ruby_posix_signal(int, void (*)(int)))(int);

RUBY_SYMBOL_EXPORT_BEGIN
/* signal.c (export) */
void rb_signal_atfork(void);
RUBY_SYMBOL_EXPORT_END

#endif /* INTERNAL_SIGNAL_H */
21 changes: 19 additions & 2 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ ruby_nativethread_signal(int signum, sighandler_t handler)
#endif
#endif

#if !defined(POSIX_SIGNAL) && !defined(SIG_GET)
static rb_nativethread_lock_t sig_check_lock;
#endif

static int
signal_ignored(int sig)
{
Expand All @@ -678,9 +682,11 @@ signal_ignored(int sig)
// SIG_GET: Returns the current value of the signal.
func = signal(sig, SIG_GET);
#else
// TODO: this is not a thread-safe way to do it. Needs lock.
sighandler_t old = signal(sig, SIG_DFL);
sighandler_t old;
rb_native_mutex_lock(&sig_check_lock);
old = signal(sig, SIG_DFL);
signal(sig, old);
rb_native_mutex_unlock(&sig_check_lock);
func = old;
#endif
if (func == SIG_IGN) return 1;
Expand Down Expand Up @@ -1509,6 +1515,9 @@ Init_signal(void)
rb_define_method(rb_eSignal, "signo", esignal_signo, 0);
rb_alias(rb_eSignal, rb_intern_const("signm"), rb_intern_const("message"));
rb_define_method(rb_eInterrupt, "initialize", interrupt_init, -1);
#if !defined(POSIX_SIGNAL) && !defined(SIG_GET)
rb_native_mutex_initialize(&sig_check_lock);
#endif

// It should be ready to call rb_signal_exec()
VM_ASSERT(GET_THREAD()->pending_interrupt_queue);
Expand Down Expand Up @@ -1561,3 +1570,11 @@ Init_signal(void)

rb_enable_interrupt();
}

void
rb_signal_atfork(void)
{
#if defined(HAVE_WORKING_FORK) && !defined(POSIX_SIGNAL) && !defined(SIG_GET)
rb_native_mutex_initialize(&sig_check_lock);
#endif
}
1 change: 1 addition & 0 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -4933,6 +4933,7 @@ rb_thread_atfork_internal(rb_thread_t *th, void (*atfork)(rb_thread_t *, const r

thread_sched_atfork(TH_SCHED(th));
ubf_list_atfork();
rb_signal_atfork();

// OK. Only this thread accesses:
ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
Expand Down
Loading