Skip to content

Writing a link_to method in helper and a URL with request parameter under certain "if else" statement crashes Rails sever with Abort trap: 6 #33460

@y4m4p

Description

@y4m4p

This is a very very specific bug and should not be much of a problem when using Rails normally, but I have encountered this problem somehow, and narrowed down the problem a bit, so I will give some examples and the outcome of it. Hope this helps fix some weird bugs out there maybe.

Steps to reproduce

In any rails application,

  1. create any helper method that satisfies the following conditions under app/helpers.
  • Write a condition with if true ... else ... OR if false ... else ...
  • Write the process under the statement that will never be used. For example, if you write it with if true then you must write your process under the else statement and vice versa.
  • Write a path using the link_to helper and a url_helper which contains a request parameter.

For example I will create a helper method like below.

# app/helpers/application_helper.rb

def helper_method
  if true
  else
    link_to 'somewhere', web_pages_some_view_path(foo: 'false')
  end
end

OR you can write it like this.

def helper_method
  if false
    link_to 'somewhere', web_pages_some_view_path(foo: 'true')
  else
  end
end
  1. use the helper method inside any view files under app/views/.... Create a controller and route it if necessary.
    For this example I will create some_view erb template and a WebPagesController, and route it accordingly.
# app/views/web_pages/some_view.html.erb

<%= helper_method %>
# app/controllers/web_pages_controller.rb

class WebPagesController < ApplicationController
  def some_view
  end
end
# app/config/routes.rb

get 'web_pages_some_view'
  1. start your rails server.

  2. Access the view.
    For this instance, localhost:3000/web_pages/some_view

Expected behavior

It should not crash, but instead, should render the process under either if or else depending on the true or false condition that you have specified.

Actual behavior

The server will crash with a log similar to the one below.

Started GET "/web_pages/some_view?foo=bar" for 127.0.0.1 at 2018-07-28 19:20:33 +0900
/Users/***/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.3.0/lib/bootsnap/compile_cache/iseq.rb:12: [BUG] Segmentation fault at 0x0000000000000000
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin14]

...(skip)...

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html

[IMPORTANT]
Don't forget to include the Crash Report log file under
DiagnosticReports directory in bug reports.

Abort trap: 6

System configuration

Rails version: Rails 5.2.0

Ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin14]

Side Note

This bug will not occur in the following conditions. So the following program will work correctly.

  1. Write the true false condition statement in any variable like the following.
def helper_method
  condition = true (instance variable or Constants is OK too.)
  if condition
    link_to ...
  else 
    link_to ...
  end
end
  1. Do not write the process under the statement that will never be used.
def helper_method
  if true
    link_to 'somewhere', web_pages_some_view_path(foo: 'true')
  else
    # link_to 'somewhere', web_pages_some_view_path(foo: 'false')  #<= comment out
  end
end
  1. Do not give the url_helper with a request parameter.
def helper_method
  if true
    link_to 'somewhere', web_pages_some_view_path
  else
    link_to 'somewhere', web_pages_some_other_view_path
  end
end
  1. Writing the method's process directly in the view template.
# app/view/web_pages/some_view.html.erb

<%=  if true
       link_to 'somewhere', web_pages_some_view_path(foo: 'true')
      else
        link_to 'somewhere', web_pages_some_view_path(foo: 'false')
      end %>

Status update

  • 7/31/2018 21:00 JST
    It seems that when you start up the server (which loads the helper method), and this specific method is not commented out, it will crash the server when you try to render any views. Even if that view does not invoke the said method.
    On the other hand, if you write the helper method after you have started up your server(like you would normally when developing the application), it is possible to explore other pages, until you try to access the said method in a view, causing it to reload the helper, thus wiping the application.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions