Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle segfault in Ruby 2.6.6 on thread-locals #2567

Merged
merged 1 commit into from Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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