Skip to content

Commit

Permalink
Handle segfault in Ruby 2.6.6 on thread-locals (puma#2567)
Browse files Browse the repository at this point in the history
When you're trying to access a thread-local variable on Ruby < 2.7, and you call `thread_variable_get` on a `Process::Waiter` (a subclass of `Thread`), it will segfault. This adds a check for which Ruby version you're on to make it safe for Ruby 2.6.

Fixes puma#2566.
  • Loading branch information
Kevin Newton authored and JuanitoFatas committed Sep 9, 2022
1 parent f8379bf commit e7dbc56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -7,6 +7,7 @@

* Bugfixes
* Your bugfix goes here <Most recent on the top, like GitHub> (#Github Number)
* Ensure no segfaults when accessing thread-local variables on Ruby < 2.7.0 (#2567)
* Don't close systemd activated socket on pumactl restart (#2563, #2504)

## 5.2.2 / 2021-02-22
Expand Down
16 changes: 11 additions & 5 deletions lib/puma/cluster.rb
Expand Up @@ -332,16 +332,22 @@ def run
# This is aligned with the output from Runner, see Runner#output_header
log "* Workers: #{@options[:workers]}"

# Threads explicitly marked as fork safe will be ignored.
# Used in Rails, but may be used by anyone.
before = Thread.list.reject { |t| t.thread_variable_get(:fork_safe) }

if preload?
# Threads explicitly marked as fork safe will be ignored. Used in Rails,
# but may be used by anyone. Note that we need to explicit
# Process::Waiter check here because there's a bug in Ruby 2.6 and below
# where calling thread_variable_get on a Process::Waiter will segfault.
# We can drop that clause once those versions of Ruby are no longer
# supported.
fork_safe = ->(t) { !t.is_a?(Process::Waiter) && t.thread_variable_get(:fork_safe) }

before = Thread.list.reject(&fork_safe)

log "* Restarts: (\u2714) hot (\u2716) phased"
log "* Preloading application"
load_and_bind

after = Thread.list.reject { |t| t.thread_variable_get(:fork_safe) }
after = Thread.list.reject(&fork_safe)

if after.size > before.size
threads = (after - before)
Expand Down

0 comments on commit e7dbc56

Please sign in to comment.