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

Fix can't modify frozen String error in DebugLocks #30433

Merged
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
14 changes: 7 additions & 7 deletions actionpack/lib/action_dispatch/middleware/debug_locks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def call(env)

private
def render_details(req)
threads = ActiveSupport::Dependencies.interlock.raw_state do |threads|
threads = ActiveSupport::Dependencies.interlock.raw_state do |raw_threads|
# The Interlock itself comes to a complete halt as long as this block
# is executing. That gives us a more consistent picture of everything,
# but creates a pretty strong Observer Effect.
Expand All @@ -53,29 +53,29 @@ def render_details(req)
# strictly diagnostic tool (to be used when something has gone wrong),
# and not for any sort of general monitoring.

threads.each.with_index do |(thread, info), idx|
raw_threads.each.with_index do |(thread, info), idx|
info[:index] = idx
info[:backtrace] = thread.backtrace
end

threads
raw_threads
end

str = threads.map do |thread, info|
if info[:exclusive]
lock_state = "Exclusive"
lock_state = "Exclusive".dup
elsif info[:sharing] > 0
lock_state = "Sharing"
lock_state = "Sharing".dup
lock_state << " x#{info[:sharing]}" if info[:sharing] > 1
else
lock_state = "No lock"
lock_state = "No lock".dup
end

if info[:waiting]
lock_state << " (yielded share)"
end

msg = "Thread #{info[:index]} [0x#{thread.__id__.to_s(16)} #{thread.status || 'dead'}] #{lock_state}\n"
msg = "Thread #{info[:index]} [0x#{thread.__id__.to_s(16)} #{thread.status || 'dead'}] #{lock_state}\n".dup

if info[:sleeper]
msg << " Waiting in #{info[:sleeper]}"
Expand Down
38 changes: 38 additions & 0 deletions actionpack/test/dispatch/debug_locks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "abstract_unit"

class DebugLocksTest < ActionDispatch::IntegrationTest
setup do
build_app
end

def test_render_threads_status
thread_ready = Concurrent::CountDownLatch.new
test_terminated = Concurrent::CountDownLatch.new

thread = Thread.new do
ActiveSupport::Dependencies.interlock.running do
thread_ready.count_down
test_terminated.wait
end
end

thread_ready.wait

get "/rails/locks"

test_terminated.count_down

assert_match(/Thread.*?Sharing/, @response.body)
ensure
thread.join
end

private
def build_app
@app = self.class.build_app do |middleware|
middleware.use ActionDispatch::DebugLocks
end
end
end