Project

General

Profile

Actions

Bug #22541

closed

`Thread#raise` and `Fiber#raise` can set VM throw data as `Exception#cause`, causing a crash.

Bug #22541: `Thread#raise` and `Fiber#raise` can set VM throw data as `Exception#cause`, causing a crash.

Added by FletcherDares (Fletcher Dares) 6 days ago. Updated 3 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:127454]

Description

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:

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:

no crash

Actual result on Ruby 4.0.9:

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:

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.

Updated by rwstauner (Randy Stauner) 6 days ago Actions #1

  • Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED

Updated by Anonymous 6 days ago Actions #2

  • Status changed from Open to Closed

Applied in changeset git|9388ee4e5fcf4ef14709693423325074eaa3271a.


Do not use throw data as the cause in Thread#raise and Fiber#raise

[Bug #22541]

rb_exception_setup takes the implicit cause from errinfo without
checking for throw data. While a non-local exit (break, return or
throw) unwinds through a C frame, errinfo holds a vm_throw_data imemo.
A Thread#raise or Fiber#raise called at that point, for example from a
signal trap, stored that imemo as the exception's cause. Reading the
cause then crashed, for example in Exception#full_message.

rb_ec_setup_exception has had this check since [Bug #19593].
64f508ade8 moved Thread#raise and Fiber#raise to rb_exception_setup,
which does not have it.

Updated by k0kubun (Takashi Kokubun) 3 days ago Actions #3 [ruby-core:127520]

  • Backport changed from 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED to 3.3: DONTNEED, 3.4: DONTNEED, 4.0: DONE
Actions

Also available in: PDF Atom