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

BetterErrors.use_pry! works on threaded servers #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions lib/better_errors/repl/pry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,36 @@ def print(*args)
end

def initialize(binding, exception)
@fiber = Fiber.new do
@pry.repl binding
end
@input_queue = Queue.new
@output_queue = Queue.new
@input = BetterErrors::REPL::Pry::Input.new
@output = BetterErrors::REPL::Pry::Output.new
@pry = ::Pry.new input: @input, output: @output
@pry.hooks.clear_all if defined?(@pry.hooks.clear_all)
store_last_exception exception
@fiber.resume
@thread = Thread.new do
Thread.current.abort_on_exception = true
@fiber = Fiber.new do
@pry.repl binding
end
@pry = ::Pry.new input: @input, output: @output
@pry.hooks.clear_all if defined?(@pry.hooks.clear_all)
store_last_exception exception
@fiber.resume

loop do
command = @input_queue.shift
break if command == :stop
local ::Pry.config, color: false, pager: false do
@fiber.resume "#{command}\n"
end
# NOTE: indent_level here is not what we seem to expect it to be ¯\_(ツ)_/¯
indent_level = @pry.instance_variable_get(:@indent).indent_level
@output_queue << [@output.read_buffer, *prompt(indent_level)]
end
end
end

def stop
@input_queue << :stop
@thread.join
end

def store_last_exception(exception)
Expand All @@ -54,23 +75,20 @@ def store_last_exception(exception)
end

def send_input(str)
local ::Pry.config, color: false, pager: false do
@fiber.resume "#{str}\n"
[@output.read_buffer, *prompt]
end
@input_queue << str
@output_queue.shift
end

def prompt
if indent = @pry.instance_variable_get(:@indent) and !indent.indent_level.empty?
["..", indent.indent_level]
else
private

def prompt(indent_level)
if indent_level.empty?
[">>", ""]
else
["..", indent_level]
end
rescue
[">>", ""]
end

private
def local(obj, attrs)
old_attrs = {}
attrs.each do |k, v|
Expand Down