Skip to content

Commit

Permalink
Add thread killing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 committed Sep 14, 2023
1 parent cf926aa commit bacd2e8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
22 changes: 22 additions & 0 deletions foo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
t = Thread.new do
loop do
begin
sleep 1
rescue ::Exception
$stderr.puts "not today!"
ensure
$stderr.puts 'ensure still runs'
raise 'meow'
sleep 20
$stderr.puts "sleeping"
end
end
end

sleep 1

puts "kill"
t.kill
puts "joining"
t.join
puts "joined"
5 changes: 3 additions & 2 deletions lib/metasploit/framework/spec/threads/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def self.configure!

thread_list.each do |thread|
thread_uuid = thread[Metasploit::Framework::Spec::Threads::Suite::UUID_THREAD_LOCAL_VARIABLE]
thread_name = thread[:tm_name]

# unmanaged thread, such as the main VM thread
unless thread_uuid
Expand All @@ -104,10 +105,10 @@ def self.configure!

caller = caller_by_thread_uuid[thread_uuid]

error_lines << "Thread #{thread_uuid}'s status is #{thread.status.inspect} " \
error_lines << "Thread #{thread_uuid}'s (name=#{thread_name} status is #{thread.status.inspect} " \
"and was started here:\n"

error_lines.concat(caller)
error_lines << "The thread backtrace was:\n#{thread.backtrace ? thread.backtrace.join("\n") : 'nil (no backtrace)'}\n"
end
else
error_lines << "Run `rake spec` to log where Thread.new is called."
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/thread_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def initialize(framework)
# XXX: Preserve Ruby < 2.5 thread exception reporting behavior
# https://ruby-doc.org/core-2.5.0/Thread.html#method-c-report_on_exception
if Thread.method_defined?(:report_on_exception=)
Thread.report_on_exception = false
Thread.report_on_exception = true
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/msf/ui/web/web_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ def tab_complete(cmd)
end

def shutdown
$stderr.puts 'shutting down webconsole'
require 'pry-byebug'; binding.pry
self.pipe.close
$stderr.puts 'closed pipe'
self.thread.kill
$stderr.puts "thread killed #{self.thread}"
end

def busy
Expand Down
5 changes: 5 additions & 0 deletions lib/rex/ui/text/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,18 @@ def run(&block)
end
# Prevent accidental console quits
rescue ::Interrupt
$stderr.puts "Was interrupted, doing a retry!"
output.print("Interrupt: use the 'exit' command to quit\n")
retry
end
end
ensure
$stderr.puts "Ensure cleanup for shell"
$stderr.puts "Flushing history manager"
HistoryManager.instance.flush
$stderr.puts "History managed flushed"
self.hist_last_saved = Readline::HISTORY.length
$stderr.puts "Finished restoring history"
end

#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
thread_manager.each do |thread|
puts "-----------------------------------------------------"
thread_deets = "thread=#{thread} - name=#{thread[:tm_name]} --- backtrace=#{thread.backtrace ? thread.backtrace.join("\n") : 'no backtrace'} ---"

$stderr.puts "killing thread #{thread_deets}"
thread.kill
$stderr.puts "Waiting to join"
Expand Down

0 comments on commit bacd2e8

Please sign in to comment.