Skip to content

Commit aa8a3b2

Browse files
committed
MJIT: Do not hang after forking with threads
First, rb_mjit_fork should call rb_thread_atfork to stop threads after fork in the child process. Unfortunately, we cannot use rb_fork_ruby to prevent this kind of mistakes because MJIT needs special handling of waiting_pid and mjit_pause/resume. Second, mjit_waitpid_finished should be checked regardless of trap_interrupt. It doesn't seem like the flag is not set when SIGCHLD is handled for an MJIT child process.
1 parent c22a668 commit aa8a3b2

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

process.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,6 +4222,7 @@ rb_mjit_fork(void)
42224222

42234223
after_fork_ruby();
42244224
disable_child_handler_fork_parent(&old);
4225+
if (pid == 0) rb_thread_atfork();
42254226

42264227
return pid;
42274228
}

thread.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,16 +2321,16 @@ rb_threadptr_execute_interrupts(rb_thread_t *th, int blocking_timing)
23212321
ret |= rb_signal_exec(th, sig);
23222322
}
23232323
th->status = prev_status;
2324+
}
23242325

23252326
#if USE_MJIT
2326-
// Handle waitpid_signal for MJIT issued by ruby_sigchld_handler. This needs to be done
2327-
// outside ruby_sigchld_handler to avoid recursively relying on the SIGCHLD handler.
2328-
if (mjit_waitpid_finished) {
2329-
mjit_waitpid_finished = false;
2330-
mjit_notify_waitpid(WIFEXITED(mjit_waitpid_status) ? WEXITSTATUS(mjit_waitpid_status) : -1);
2331-
}
2332-
#endif
2327+
// Handle waitpid_signal for MJIT issued by ruby_sigchld_handler. This needs to be done
2328+
// outside ruby_sigchld_handler to avoid recursively relying on the SIGCHLD handler.
2329+
if (mjit_waitpid_finished && th == th->vm->ractor.main_thread) {
2330+
mjit_waitpid_finished = false;
2331+
mjit_notify_waitpid(WIFEXITED(mjit_waitpid_status) ? WEXITSTATUS(mjit_waitpid_status) : -1);
23332332
}
2333+
#endif
23342334

23352335
/* exception from another thread */
23362336
if (pending_interrupt && threadptr_pending_interrupt_active_p(th)) {

0 commit comments

Comments
 (0)