Skip to content

Handling Interrupt exception and calling .readline again seems to break Interrupt handling in LineEditor.reset #351

Closed

Description

@win93

Description

I expect the following code to print 'received interrupt' twice if I press ^C twice

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'reline'

begin
  while (line = Reline.readline('> '))
    puts line
  end
rescue Interrupt
  puts 'received interrupt'
  retry
end

Instead, it works the first time, but the second time, I receive the following trace:

/Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/ansi.rb:282:in `scroll_down': stack level too deep (SystemStackError)
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:313:in `scroll_down'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:160:in `block in reset'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:169:in `block in reset'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:169:in `block in reset'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:169:in `block in reset'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:169:in `block in reset'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:169:in `block in reset'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline/line_editor.rb:169:in `block in reset'
     ... 13088 levels...
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline.rb:244:in `inner_readline'
    from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.7/lib/reline.rb:191:in `readline'
    from /usr/local/Cellar/ruby/3.0.2/lib/ruby/3.0.0/forwardable.rb:238:in `readline'
    from exe/reline-test.rb:8:in `<main>'

I have gem 'reline', '~> 0.2.7' in my Gemfile, and you can see from the trace that 0.2.7 is the version of reline I am using.

The stdlib docs mention ways to handle ^C. I tried trap("INT", "SIG_IGN"), which doesn't seem to be what I want. This seems to temporarily hide the prompt, and doesn't clear the inputted line. (Type 'hello' then ^C, then ' world'. You will see '> hello world')

I also tried adding trap("INT", "DEFAULT") right before retry, and this fixes the issue but feels like I am working around an issue in reline. If I am approaching this the wrong way, let me know. Thanks for your time.

Terminal Emulator

I am using iTerm2 3.4.10 on macOS 10.15.7

Activity

  1. win93 commented on Sep 19, 2021

    @win93
    ContributorAuthor

    I get the same behavior with 0.2.8.pre9 as well. The stack trace is slightly different:

    /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:839:in `block in clear_dialog': stack level too deep (SystemStackError)
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:838:in `each'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:838:in `clear_dialog'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:154:in `block in reset'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:170:in `block in reset'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:170:in `block in reset'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:170:in `block in reset'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:170:in `block in reset'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb:170:in `block in reset'
         ... 13088 levels...
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline.rb:322:in `inner_readline'
        from /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline.rb:265:in `readline'
        from /usr/local/Cellar/ruby/3.0.2/lib/ruby/3.0.0/forwardable.rb:238:in `readline'
        from exe/reline-test.rb:8:in `<main>'
    
  2. win93 commented on Sep 19, 2021

    @win93
    ContributorAuthor

    I am not very familiar with how signals are managed in this library, but the fix for this may be as simple as:

    --- /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor-orig.rb	2021-09-19 17:20:17.000000000 -0500
    +++ /Users/alexg/.gem/ruby/3.0.0/gems/reline-0.2.8.pre.9/lib/reline/line_editor.rb	2021-09-19 17:20:30.000000000 -0500
    @@ -159,6 +159,7 @@
           end
           Reline::IOGate.move_cursor_column(0)
           scroll_down(1)
    +      Signal.trap('INT', @old_trap)
           case @old_trap
           when 'DEFAULT', 'SYSTEM_DEFAULT'
             raise Interrupt

    This has the effect of installing the old_trap back in place after the installed trap fires. In other words, the handling would be a one-time event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions