From: "k0kubun (Takashi Kokubun) via ruby-core" Date: 2026-11-16T17:42:38+00:00 Subject: [ruby-core:127520] [Ruby Bug#22541] `Thread#raise` and `Fiber#raise` can set VM throw data as `Exception#cause`, causing a crash. Issue #22541 has been updated by k0kubun (Takashi Kokubun). Backport changed from 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED to 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE ruby_4_0 commit:3bb445a6302ec8834475b6cd92e2492dcec94c81 merged revision(s) commit:9388ee4e5fcf4ef14709693423325074eaa3271a. ---------------------------------------- Bug #22541: `Thread#raise` and `Fiber#raise` can set VM throw data as `Exception#cause`, causing a crash. https://bugs.ruby-lang.org/issues/22541#change-119988 * Author: FletcherDares (Fletcher Dares) * Status: Closed * ruby -v: 4.0.X * Backport: 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE ---------------------------------------- `Thread#raise` and `Fiber#raise` can store VM-internal throw data as the exception's `cause`. This happens when they are called while a non-local exit (`break`, `return` or `throw`) is unwinding through a C frame, for example from a signal trap. Any code that later reads the cause, such as `Exception#full_message`, crashes the interpreter. This is a regression in Ruby 4.0. The same problem was fixed for `rb_ec_setup_exception` in #19593, but `Thread#raise` and `Fiber#raise` now go through `rb_exception_setup` (https://github.com/ruby/ruby/pull/13967), which does not have the check. #21865 restored the checked path for the VM's own signal raise, but not for these two methods. ## Background This was discovered in a production application using `Async::Container`, whose signal handler is `Signal.trap(:INT) { Thread.current.raise(Interrupt) }`. When the signal arrived while a `break` was unwinding, the resulting `Interrupt` had throw data as its cause, and error reporting crashed when it read the cause. A minimal reproduction is: ```ruby Signal.trap(:INT) { Thread.current.raise(Interrupt) } Thread.new { loop { Process.kill(:INT, Process.pid); sleep 0.001 } } 50.times do loop { (1..1).any? } rescue Interrupt => e e.full_message # walks e.cause end puts "no crash" ``` Expected output: ```text no crash ``` Actual result on Ruby 4.0.9: ```text test.rb:7: [BUG] Segmentation fault at 0x0000000000000092 ``` The crash is in `show_cause`, called from `exc_full_message`. Ruby 3.4.8 prints `no crash`. Tested with: ```text ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [arm64-darwin25] ``` ## Proposed fix Discard throw data when `rb_exception_setup` takes the implicit cause from `errinfo`, the same way `rb_ec_setup_exception` does. ## Pull request PR: https://github.com/ruby/ruby/pull/19861 The PR includes: 1. The `THROW_DATA_P` check in `rb_exception_setup`. 2. Regression tests for `Thread#raise` and `Fiber#raise` that do not depend on signals or timing. Once accepted, I would like to request a backport to Ruby 4.0. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/