|
| 1 | +module ActionDispatch |
| 2 | + # This middleware can be used to diagnose deadlocks in the autoload interlock. |
| 3 | + # |
| 4 | + # To use it, insert it near the top of the middleware stack, using |
| 5 | + # <tt>config/application.rb</tt>: |
| 6 | + # |
| 7 | + # config.middleware.insert_before Rack::Sendfile, ActionDispatch::DebugLocks |
| 8 | + # |
| 9 | + # After restarting the application and re-triggering the deadlock condition, |
| 10 | + # <tt>/rails/locks</tt> will show a summary of all threads currently known to |
| 11 | + # the interlock, which lock level they are holding or awaiting, and their |
| 12 | + # current backtrace. |
| 13 | + # |
| 14 | + # Generally a deadlock will be caused by the interlock conflicting with some |
| 15 | + # other external lock or blocking I/O call. These cannot be automatically |
| 16 | + # identified, but should be visible in the displayed backtraces. |
| 17 | + # |
| 18 | + # NOTE: The formatting and content of this middleware's output is intended for |
| 19 | + # human consumption, and should be expected to change between releases. |
| 20 | + # |
| 21 | + # This middleware exposes operational details of the server, with no access |
| 22 | + # control. It should only be enabled when in use, and removed thereafter. |
| 23 | + class DebugLocks |
| 24 | + def initialize(app, path = '/rails/locks') |
| 25 | + @app = app |
| 26 | + @path = path |
| 27 | + end |
| 28 | + |
| 29 | + def call(env) |
| 30 | + req = ActionDispatch::Request.new env |
| 31 | + |
| 32 | + if req.get? |
| 33 | + path = req.path_info.chomp('/'.freeze) |
| 34 | + if path == @path |
| 35 | + return render_details(req) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + @app.call(env) |
| 40 | + end |
| 41 | + |
| 42 | + private |
| 43 | + def render_details(req) |
| 44 | + threads = ActiveSupport::Dependencies.interlock.raw_state do |threads| |
| 45 | + # The Interlock itself comes to a complete halt as long as this block |
| 46 | + # is executing. That gives us a more consistent picture of everything, |
| 47 | + # but creates a pretty strong Observer Effect. |
| 48 | + # |
| 49 | + # Most directly, that means we need to do as little as possible in |
| 50 | + # this block. More widely, it means this middleware should remain a |
| 51 | + # strictly diagnostic tool (to be used when something has gone wrong), |
| 52 | + # and not for any sort of general monitoring. |
| 53 | + |
| 54 | + threads.each.with_index do |(thread, info), idx| |
| 55 | + info[:index] = idx |
| 56 | + info[:backtrace] = thread.backtrace |
| 57 | + end |
| 58 | + |
| 59 | + threads |
| 60 | + end |
| 61 | + |
| 62 | + str = threads.map do |thread, info| |
| 63 | + if info[:exclusive] |
| 64 | + lock_state = 'Exclusive' |
| 65 | + elsif info[:sharing] > 0 |
| 66 | + lock_state = 'Sharing' |
| 67 | + lock_state << " x#{info[:sharing]}" if info[:sharing] > 1 |
| 68 | + else |
| 69 | + lock_state = 'No lock' |
| 70 | + end |
| 71 | + |
| 72 | + if info[:waiting] |
| 73 | + lock_state << ' (yielded share)' |
| 74 | + end |
| 75 | + |
| 76 | + msg = "Thread #{info[:index]} [0x#{thread.__id__.to_s(16)} #{thread.status || 'dead'}] #{lock_state}\n" |
| 77 | + |
| 78 | + if info[:sleeper] |
| 79 | + msg << " Waiting in #{info[:sleeper]}" |
| 80 | + msg << " to #{info[:purpose].to_s.inspect}" unless info[:purpose].nil? |
| 81 | + msg << "\n" |
| 82 | + |
| 83 | + if info[:compatible] |
| 84 | + compat = info[:compatible].map { |c| c == false ? "share" : c.to_s.inspect } |
| 85 | + msg << " may be pre-empted for: #{compat.join(', ')}\n" |
| 86 | + end |
| 87 | + |
| 88 | + blockers = threads.values.select { |binfo| blocked_by?(info, binfo, threads.values) } |
| 89 | + msg << " blocked by: #{blockers.map {|i| i[:index] }.join(', ')}\n" if blockers.any? |
| 90 | + end |
| 91 | + |
| 92 | + blockees = threads.values.select { |binfo| blocked_by?(binfo, info, threads.values) } |
| 93 | + msg << " blocking: #{blockees.map {|i| i[:index] }.join(', ')}\n" if blockees.any? |
| 94 | + |
| 95 | + msg << "\n#{info[:backtrace].join("\n")}\n" if info[:backtrace] |
| 96 | + end.join("\n\n---\n\n\n") |
| 97 | + |
| 98 | + [200, { "Content-Type" => "text/plain", "Content-Length" => str.size }, [str]] |
| 99 | + end |
| 100 | + |
| 101 | + def blocked_by?(victim, blocker, all_threads) |
| 102 | + return false if victim.equal?(blocker) |
| 103 | + |
| 104 | + case victim[:sleeper] |
| 105 | + when :start_sharing |
| 106 | + blocker[:exclusive] || |
| 107 | + (!victim[:waiting] && blocker[:compatible] && !blocker[:compatible].include?(false)) |
| 108 | + when :start_exclusive |
| 109 | + blocker[:sharing] > 0 || |
| 110 | + blocker[:exclusive] || |
| 111 | + (blocker[:compatible] && !blocker[:compatible].include?(victim[:purpose])) |
| 112 | + when :yield_shares |
| 113 | + blocker[:exclusive] |
| 114 | + when :stop_exclusive |
| 115 | + blocker[:exclusive] || |
| 116 | + victim[:compatible] && |
| 117 | + victim[:compatible].include?(blocker[:purpose]) && |
| 118 | + all_threads.all? { |other| !other[:compatible] || blocker.equal?(other) || other[:compatible].include?(blocker[:purpose]) } |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments