From 4e9a2e0e63018b5334807c41132f5c11351d773b Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 26 Apr 2019 16:30:29 -0700 Subject: [PATCH] Fix threads not waking up on SIGINT when using UBF_TIMER_PTHREAD When using UBF_TIMER_PTHREAD, the timer_pthread_fn function will not signal the main thread with SIGVTALRM in cases where timer_pthread is armed before consume_communication_pipe is called. This is because consume_communication_pipe will unarm the timer. Fix this by checking the return value of consume_communication_pipe. If it returns TRUE and the timer_pthread is disarmed, then signal the main thread with SIGVTALRM. This fixes TestThread#test_thread_timer_and_interrupt. It also fixes the use of Ctrl+C/SIGINT in irb on OpenBSD, and I'm guessing other platforms that use UBF_TIMER_PTHREAD. Previously, you could hit Ctrl+C multiple times and it would do nothing until another key was pressed (even Backspace). --- thread_pthread.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/thread_pthread.c b/thread_pthread.c index 499da0b9ca..2e5be35c11 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -2187,25 +2187,33 @@ timer_pthread_fn(void *p) pthread_t main_thread_id = vm->main_thread->thread_id; struct pollfd pfd; int timeout = -1; + int ccp; pfd.fd = timer_pthread.low[0]; pfd.events = POLLIN; while (system_working > 0) { (void)poll(&pfd, 1, timeout); - (void)consume_communication_pipe(pfd.fd); + ccp = consume_communication_pipe(pfd.fd); - if (system_working > 0 && ATOMIC_CAS(timer_pthread.armed, 1, 1)) { - pthread_kill(main_thread_id, SIGVTALRM); - - if (rb_signal_buff_size() || !ubf_threads_empty()) { - timeout = TIME_QUANTUM_MSEC; - } - else { - ATOMIC_SET(timer_pthread.armed, 0); - timeout = -1; - } - } + if (system_working > 0) { + if (ATOMIC_CAS(timer_pthread.armed, 1, 1)) { + pthread_kill(main_thread_id, SIGVTALRM); + + if (rb_signal_buff_size() || !ubf_threads_empty()) { + timeout = TIME_QUANTUM_MSEC; + } + else { + ATOMIC_SET(timer_pthread.armed, 0); + timeout = -1; + } + } + else if (ccp) { + pthread_kill(main_thread_id, SIGVTALRM); + ATOMIC_SET(timer_pthread.armed, 0); + timeout = -1; + } + } } return 0; -- 2.21.0