1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Handle segfault in Ruby 2.6.6 on thread-locals (#2567)

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 #2566.
This commit is contained in:
Kevin Newton 2021-04-19 11:30:02 -04:00 committed by GitHub
parent 28a4cb2c1a
commit 366496b89d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -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

View file

@ -332,16 +332,22 @@ module Puma
# 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)