From d6f31838757d3a490b3d368994115875eed798c9 Mon Sep 17 00:00:00 2001 From: Barrett Ingram Date: Sat, 3 Jul 2021 07:55:57 -0500 Subject: Return with an error message if pry is started inside signal handler (#2206) Mutex#synchronize will throw an error if called from code passed to Signal.trap. This makes it impossible to start a working Pry session inside a signal handler. Currently, calling binding.pry inside signal handling code raises an exception because Pry uses mutexes to solve various thread-safety issues. This commit updates Pry.start to detect whether we'll have the ability to use Mutex#synchronize. If not, we'll print an error message and return instead of raising an error. https://github.com/pry/pry/issues/2191 --- spec/pry_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'spec') diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index 1ea0210d..f5bb0713 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -184,6 +184,31 @@ describe Pry do .to raise_error SignalException end + describe "inside signal handler" do + before do + if Pry::Helpers::Platform.jruby? + skip "jruby allows mutex usage in signal handlers" + end + + if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.0.0") + skip "pre-2.0 mri allows mutex usage in signal handlers" + end + + trap("USR1") { @str_output = mock_pry } + end + + after do + trap("USR1") do + # do nothing + end + end + + it "should return with error message" do + Process.kill("USR1", Process.pid) + expect(@str_output).to match(/Unable to obtain mutex lock/) + end + end + describe "multi-line input" do it "works" do expect(mock_pry('x = ', '1 + 4')).to match(/5/) -- cgit v1.2.1