Skip to content

Commit be721cd

Browse files
committed
Fix GCC optimization bug eliminating rb_gc_before_fork()
GCC 15.2.0 with -O3 and -fipa-cp-clone completely eliminates rb_gc_before_fork() because it sees rb_gc_impl_before_fork() is a no-op. This removes the memory synchronization point before fork(), causing stale cached heap slot data to persist in child processes, leading to jemalloc corruption and SIGSEGV crashes. Add __atomic_thread_fence(RBIMPL_ATOMIC_SEQ_CST) to force GCC to preserve the function and create a proper memory barrier. Uses Ruby's memory ordering constant from include/ruby/atomic.h. This generates a lock-prefixed instruction on x86-64, preventing function elimination and ensuring memory synchronization across fork boundaries. Affects: Shopify's +optimized builds with GCC 15, -O3, --disable-shared Impact: Fixes intermittent crashes in Puma/Unicorn worker processes
1 parent 728001c commit be721cd

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

gc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5618,6 +5618,7 @@ void
56185618
rb_gc_before_fork(void)
56195619
{
56205620
rb_gc_impl_before_fork(rb_gc_get_objspace());
5621+
__atomic_thread_fence(RBIMPL_ATOMIC_SEQ_CST);
56215622
}
56225623

56235624
void

0 commit comments

Comments
 (0)