1100 Commits
Author SHA1 Message Date
John Hawthorn c092fd8552 Flush the current Ractor's stdout/stderr before spawning
prefork() flushed rb_stdout and rb_stderr, which are the main Ractor's IOs.
From another Ractor that takes the write_lock of an IO the Ractor does not
own. When a main Ractor thread holds it (any thread logging to $stdout will
do), the spawning Ractor's only thread sleeps on the mutex and
rb_check_deadlock, which only looks at the Ractor's own threads, raises
"No live threads left. Deadlock?" (fatal) from Kernel#spawn/system/popen.

Use rb_ractor_stdout()/rb_ractor_stderr() so each Ractor flushes its own.

[Bug #22434]

(cherry picked from commit 28f2eae4cf)
2026-10-21 11:46:04 -07:00
Douglas Eichelberger 27204205e6 Reset the fork lock in the child without unlocking it
A thread that is waiting for the fork lock when another thread forks
exists only in the parent, but on macOS the child's copy of the lock
still counts it. After the child unlocks its copy,
pthread_rwlock_destroy() and pthread_rwlock_init() fail with EBUSY, so
the child aborts with
"[BUG] pthread_rwlock_destroy: Resource busy (EBUSY)". Lookup threads
wait for the lock whenever a fork holds it, so a process that resolves
names in background threads while it forks loses a child now and then.

Reset the child's copy by assigning PTHREAD_RWLOCK_INITIALIZER, as
rb_internal_thread_event_hooks_rw_lock_atfork() does for its lock, and
do not unlock it first. With a waiter counted, macOS's unlock enters the
kernel to wake it, and the state that leaves behind for the lock's
address survives the assignment, so a later waiter in the child can
sleep forever although no thread holds the lock. FreeBSD already skipped
the unlock in the child (dc8c382266), and on glibc the child's unlock
does not release the write lock anyway.

[Bug #22445]
2026-10-15 12:37:17 -07:00
Douglas Eichelberger 59db3a3a81 Join getaddrinfo and getnameinfo threads before fork
A getaddrinfo thread calls getaddrinfo(3) inside rb_thread_prevent_fork,
but on glibc its thread exit takes the resolver configuration lock again
after the thread has released the fork lock. A fork that lands then
leaves the child with that lock held, and every later name lookup in the
child blocks forever. getnameinfo(3) attaches the same resolver state,
even for a name in /etc/hosts, and its thread ran outside
rb_thread_prevent_fork altogether.

Make the lookup threads joinable and have fork join any that may still
be exiting. As a lookup thread finishes, still inside
rb_thread_prevent_fork, it swaps itself into a single slot with an
atomic exchange and joins the thread it displaced, which has usually
exited by then, and rb_thread_acquire_fork_lock joins whatever is left
in the slot. Once lookups settle, at most one finished thread is left
unjoined. The slot holds pthread_t as a pointer, with NULL for empty,
which assumes pthread_t is scalar and never 0.

getnameinfo threads now run within rb_thread_prevent_fork and register
as they exit, as getaddrinfo threads do, and where getnameinfo runs
without a helper thread it is called within rb_thread_prevent_fork too.
So fork also waits for a reverse lookup in progress, as it already did
for a forward one. raddrinfo_pthread_create now always creates joinable
threads, which drops its pthread_detach fallback for platforms without
pthread_attr_setdetachstate.

The slot is kept across fork, so every fork path that keeps running
Ruby code has to take the fork lock. On the BSDs, Process.daemon forks
with daemon(3) and did not take it, so the daemon would inherit the
registered thread, and its next fork would abort in pthread_join with
ESRCH. Take and release the fork lock around daemon(3) as rb_fork_ruby
does. daemon(3) can fail in the child as well as in the parent, so
compare pids to tell which process returned.

[Bug #22406]

(cherry picked from commit 44f7c6b00d)
2026-10-14 17:15:16 -07:00
Nobuyoshi Nakada e06fe010c8 [Backport #22243] Ensure forked process to terminate
The forked process should terminate reliably even if an exception
occurs.
2026-08-19 10:17:01 -07:00
Stan Lo 4cd6661e18 Reorganize page documentations (#15154)
Re-organize page docs
2025-11-27 20:12:24 +00:00
Nobuyoshi Nakada 45fcb9d590 Adjust indent [ci skip] 2025-11-24 17:30:47 +09:00
Peter Zhu 390220579b Replace vfork with fork for ASAN
Older versions of ASAN do not support vfork. See https://github.com/google/sanitizers/issues/925
2025-11-23 10:54:44 -08:00
Peter Zhu 2447380894 Run rb_gc_before_fork after before_exec
before_exec stops the timer thread, which requires locking the Ractor
scheduler lock. This may deadlock if rb_gc_before_fork locks the VM.
2025-11-20 14:30:42 -08:00
Ricardo Trindade 48eb907f6b [DOC] Fix formatting of seconds parameter in documentation
There's an additional colon sign in the documentation for Process.clock_gettime
2025-09-24 19:06:24 +09:00
Peter Zhu b0ce1fd549 Combine rb_imemo_tmpbuf_auto_free_pointer and rb_imemo_tmpbuf_new 2025-09-15 09:25:20 -04:00
Jean Boussier 5bcc639b34 Disallow forking from non-main ractor
[Bug #17516]

`fork(2)` only leave the calling thread alive in the child.
Because of this forking from the non-main ractor can easily
leave the VM in a corrupted state.

It may be possible in the future to carefully allow forking from non-main
Ractor, but shot term it's preferable to add this restriction.
2025-06-24 15:10:40 +01:00
Nobuyoshi Nakada edaa27ce45 Suppress warnings by gcc-13 with -Og 2025-06-05 22:33:02 +09:00
Nobuyoshi Nakada aad9fa2853 Use RB_VM_LOCKING 2025-05-25 15:22:43 +09:00
John Hawthorn d7ad53f249 Acquire VM lock around fork
Otherwise it might be held by another Ractor, causing a deadlock in the
new process.
2025-05-08 10:53:28 -07:00
Nobuyoshi Nakada 4a67ef09cc [Feature #21116] Extract RJIT as a third-party gem 2025-02-13 18:01:03 +09:00
Nobuyoshi Nakada e433e6515e [DOC] Exclude 'Class' and 'Module' from RDoc's autolinking 2025-01-02 12:36:06 +09:00
Nobuyoshi Nakada b4ec22fe6c [DOC] Exclude 'Method' from RDoc's autolinking 2025-01-02 12:23:49 +09:00
Nobuyoshi Nakada d441d35107 [DOC] Exclude 'Process' from RDoc's autolinking 2025-01-02 11:58:15 +09:00
Hiroshi SHIBATA 9967eccc54 Removed Process::Status#& and Process::Status#>> 2024-12-25 20:10:18 +09:00
Yusuke Endoh c07fb79150 Avoid file descriptor leak
`extra_fd` was leaked if `fd_set_cloexec` fails -- I can't think of any
chance of that happening here, but just in case.

Coverity Scan found this issue.
2024-12-18 11:10:05 +09:00
Alan Wu 0c9c6efc20 Add missing fiber nil check to avoid slow rb_check_funcall()
Check for nil like other callers of rb_fiber_scheduler_current().
2024-12-09 16:20:01 -05:00
Matt Valentine-HouseandPeter Zhu 551be8219e Place all non-default GC API behind USE_SHARED_GC
So that it doesn't get included in the generated binaries for builds
that don't support loading shared GC modules

Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
2024-11-25 13:05:23 +00:00
Samuel Williams 86e8a36e77 Revert "Direct access to fptr->fd should prefer to use the internal/io.h. (#11793)"
This reverts commit 6ea0dcc978 that was accidentally merged.
2024-10-04 08:25:43 +00:00
Samuel Williams 6ea0dcc978 Direct access to fptr->fd should prefer to use the internal/io.h. (#11793) 2024-10-04 07:44:23 +00:00
Nobuyoshi Nakada dc8c382266 Fix bug at fork on FreeBSD
The main thread in a forked process appears not to own the read-write
lock.
2024-09-19 18:08:12 +09:00
Nobuyoshi Nakada c193ca5218 Adjust indent [ci skip] 2024-09-19 14:33:32 +09:00
Nobuyoshi Nakada d80a81c152 Constify struct getpwnam_r_args::login 2024-09-13 09:28:51 +09:00
Jeremy Evans ad761ad2d0 Release GVL for get{pwnam,pwuid,grgid,grnam}_r calls in process.c
Do not release GVL around get{pwuid,pwnam,grgid,grnam} calls,
as doing so is not thread-safe.  Another C extension could have
a concurrent call, and derefencing the returned pointer from
these calls could result in a segfault.

Have rb_home_dir_of call rb_getpwdirnam_for_login if available,
so it can use getpwnam_r and release GVL in a thread-safe manner.

This is related to GVL releasing work in [Bug #20587].
2024-09-12 07:24:02 -07:00
Jean Boussier 63cbe3f6ac Proof of Concept: Allow to prevent fork from happening in known fork unsafe API
[Feature #20590]

For better of for worse, fork(2) remain the primary provider of
parallelism in Ruby programs. Even though it's frowned uppon in
many circles, and a lot of literature will simply state that only
async-signal safe APIs are safe to use after `fork()`, in practice
most APIs work well as long as you are careful about not forking
while another thread is holding a pthread mutex.

One of the APIs that is known cause fork safety issues is `getaddrinfo`.
If you fork while another thread is inside `getaddrinfo`, a mutex
may be left locked in the child, with no way to unlock it.

I think we could reduce the impact of these problem by preventing
in for the most notorious and common cases, by locking around
`fork(2)` and known unsafe APIs with a read-write lock.
2024-09-05 11:43:46 +02:00
Nobuyoshi Nakada 08e142b209 Refactor getlogin and getpw* functions
- Extract functions to check not-found conditions
- Set the length to the result of `rb_getlogin`
- Reentrant versions return an error numeber but not `errno`
- Check maybe-undefined macros with `defined`
2024-09-05 16:55:54 +09:00
Nobuyoshi Nakada 566f2eb501 Merge rb_fork_ruby2 into rb_fork_ruby 2024-08-19 15:36:49 +09:00
Nobuyoshi Nakada cccffeff2f Fix missing variable
It seems like no one has tried to compile on a platform without
`setsid` for almost a quarter of a century.
2024-04-07 21:16:26 +09:00
Nobuyoshi Nakada b5b54c19f6 Unify ERRMSG1 and ERRMSG2 to ERRMSG_FMT variadic macro 2024-04-07 16:43:52 +09:00
Joshua Young e07178d526 [DOC] Fix scope resolution operator typo in Process#wait docs 2024-04-01 01:06:10 +00:00
Samuel Williams a7ff264477 Don't clear pending interrupts in the parent process. (#10365) 2024-03-27 10:10:07 +13:00
Nobuyoshi Nakada 5baee82c76 [DOC] Mention about executable file and fallback to sh 2024-03-01 13:11:29 +09:00
Nobuyoshi Nakada 8bff7e996c [DOC] Move "Execution Shell on Windows" under "Execution Shell" 2024-01-29 17:16:49 +09:00
Nobuyoshi Nakada 2defa9f4ae [DOC] Elaborate exceptional behaviors on Windows 2024-01-24 12:44:40 +09:00
Nobuyoshi Nakada 769bb924b2 [DOC] Tips for space containing exe_path 2024-01-24 12:44:40 +09:00
Nobuyoshi Nakada 623058649e [DOC] Add Argument args subsection 2024-01-24 12:44:40 +09:00
Nobuyoshi Nakada be6e06ef74 [DOC] Separate standard shells per platforms 2024-01-24 12:44:40 +09:00
Nobuyoshi Nakada 828f3ecfcd [DOC] Move exe_path example to Process module document
Exchanged with `Kernel.spawn`, like as `Kernel.exec` and
`Kernel.system`.  This description should be common for these methods.
2024-01-24 12:44:39 +09:00
Burdette Lamar df5f2fab93 [DOC] Correction for Process.spawn doc 2024-01-24 12:43:52 +09:00
Nobuyoshi Nakada e2e15ddd67 [DOC] echo command may not be a shell built-in 2024-01-21 18:41:41 +09:00
Xavier Noria aad246feba s/SafeStringValue/StringValue/
The macro SafeStringValue() became just StringValue() in c5c05460ac,
and it is deprecated nowadays.

This patch replaces remaining macro usage. Some occurrences are left in
ext/stringio and ext/win32ole, they should be fixed upstream.

The macro itself is not deleted, because it may be used in extensions.
2024-01-12 12:24:48 -08:00
Takuya Aramaki 3e9a612d92 [DOC] Fix wrong description about Process.waitpid2 (#9401) 2024-01-01 21:31:49 +09:00
Takashi Kokubun a164a34110 Resurrect a deprecated code for fixing tests for now
Revert "Remove deprecated code to fix RUBY_DEBUG build failures"

This reverts commit 9614bea2cd.

And changed 3.4 to 3.5. To be fixed properly later.
2023-12-25 01:36:28 -08:00
Takashi Kokubun 9614bea2cd Remove deprecated code to fix RUBY_DEBUG build failures 2023-12-25 01:21:33 -08:00
JP Camara 8782e02138 KQueue support for M:N threads
* Allows macOS users to use M:N threads (and technically FreeBSD, though it has not been verified on FreeBSD)

* Include sys/event.h header check for macros, and include sys/event.h when present

* Rename epoll_fd to more generic kq_fd (Kernel event Queue) for use by both epoll and kqueue

* MAP_STACK is not available on macOS so conditionall apply it to mmap flags

* Set fd to close on exec

* Log debug messages specific to kqueue and epoll on creation

* close_invalidate raises an error for the kqueue fd on child process fork. It's unclear rn if that's a bug, or if it's kqueue specific behavior

Use kq with rb_thread_wait_for_single_fd

* Only platforms with `USE_POLL` (linux) had changes applied to take advantage of kernel event queues. It needed to be applied to the `select` so that kqueue could be properly applied

* Clean up kqueue specific code and make sure only flags that were actually set are removed (or an error is raised)

* Also handle kevent specific errnos, since most don't apply from epoll to kqueue

* Use the more platform standard close-on-exec approach of `fcntl` and `FD_CLOEXEC`. The io-event gem uses `ioctl`, but fcntl seems to be the recommended choice. It is also what Go, Bun, and Libuv use

* We're making changes in this file anyways - may as well fix a couple spelling mistakes while here

Make sure FD_CLOEXEC carries over in dup

* Otherwise the kqueue descriptor should have FD_CLOEXEC, but doesn't and fails in assert_close_on_exec
2023-12-20 16:23:38 +09:00
Victor Shepelev 570d7b2c3e [DOC] Adjust some new features wording/examples. (#9183)
* Reword Range#overlap? docs last paragraph.

* Docs: add explanation about Queue#freeze

* Docs: Add :rescue event docs for TracePoint

* Docs: Enhance Module#set_temporary_name documentation

* Docs: Slightly expand Process::Status deprecations

* Fix MatchData#named_captures rendering glitch

* Improve Dir.fchdir examples

* Adjust Refinement#target docs
2023-12-14 23:01:48 +02:00