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

Log threads when the OS signal is received to simplify debugging, especially when a CI node hangs. #266

Merged
merged 10 commits into from
Aug 7, 2024
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

### UNRELEASED

### 7.7.0

* Log threads when the OS signal is received to simplify debugging, especially when a CI node hangs.

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/266

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v7.6.2...v7.7.0

### 7.6.2

* Fix an error for the `KnapsackPro::Formatters::TimeTracker` formatter in RSpec when using Knapsack Pro Regular Mode and the `.rspec` file is not present.
Expand Down
28 changes: 28 additions & 0 deletions lib/knapsack_pro/runners/queue/base_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,37 @@ def trap_signals
Signal.trap(signal) {
puts "#{signal} signal has been received. Terminating Knapsack Pro..."
@@terminate_process = true
log_threads
}
end
end

def log_threads
threads = Thread.list

puts
puts '=' * 80
puts "Start logging #{threads.count} detected threads."
puts 'Use the following backtrace(s) to find the line of code that got stuck if the CI node hung and terminated your tests.'

threads.each do |thread|
puts
if thread == Thread.main
puts "Main thread backtrace:"
else
puts "Non-main thread inspect: #{thread.inspect}"
puts "Non-main thread backtrace:"
end
puts thread.backtrace.join("\n")
puts
end

puts
puts 'End logging threads.'
puts '=' * 80

$stdout.flush
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/integration/runners/queue/rspec_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,10 @@ def when_first_matching_example_defined(type:)
describe 'A_describe' do
it 'A1 test example' do
expect(1).to eq 1

Thread.new do
sleep 10
end
end
end
SPEC
Expand Down Expand Up @@ -1406,6 +1410,14 @@ def when_first_matching_example_defined(type:)
OUTPUT
)


expect(actual.stdout).to include('Use the following backtrace(s) to find the line of code that got stuck if the CI node hung and terminated your tests.')
expect(actual.stdout).to include('Main thread backtrace:')
expect(actual.stdout).to include("spec_integration/b_spec.rb:7:in `kill'")
expect(actual.stdout).to include('Non-main thread backtrace:')
expect(actual.stdout).to include("spec_integration/a_spec.rb:6:in `sleep'")


expect(actual.exit_code).to eq 1
end
end
Expand Down