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

Avoid infinite recursion when there is a redirect #338

Merged
merged 1 commit into from
Feb 28, 2015

Conversation

stormsilver
Copy link
Contributor

After upgrading to Rails 4.2.0, I began receiving a SystemStackError when Rails boots. After quite a bit of debugging, I traced it to the fact that I am using the api! method in my controllers to auto-detect the route for a given description. After more debugging, I traced it to the fact that I have several "catchall" redirects in my routes.rb file.

Apparently when the method Apipie::Application#route_app_controller attempts to recursively figure out the controller for a given app, the app generated from redirect satisfies none of the conditions and it will recurse until Ruby stops it.

I'm not sure of the exact fix, someone with more domain knowledge will have to say whether my patch is right or not. I was able to cause the problem under Rails 4.2 by adding the redirect route; I was able to stop the problem by adding another check to the condition.

Let me know if there is something more I can do to help with this PR.

@iNecas
Copy link
Member

iNecas commented Feb 27, 2015

Hi, thanks for reporting the issue, as well as taking time to investigating it and proposing patch. We might probably generalize that a bit more, not detecting only the direct app.app == app, but every already visited app in the stack (probably passing the list of visited apps as another argument of the method). I'm not sure, if this kind of loop can occur, but better safe than sorry.

@stormsilver
Copy link
Contributor Author

No problem! Are you thinking of something like this?

    def route_app_controller(app, route, visited_apps = [])
      visited_apps << app
      if app.respond_to?(:controller)
        return app.controller(route.defaults)
      elsif app.respond_to?(:app) && !visited_apps.include?(app.app)
        return route_app_controller(app.app, route, visited_apps)
      end
    rescue ActionController::RoutingError
      # some errors in the routes will not stop us here: just ignoring
    end

@iNecas
Copy link
Member

iNecas commented Feb 27, 2015

Exactly!

@stormsilver
Copy link
Contributor Author

@iNecas updated per your comments.

@iNecas
Copy link
Member

iNecas commented Feb 28, 2015

Thanks a lot @stormsilver . Meging

iNecas added a commit that referenced this pull request Feb 28, 2015
Avoid infinite recursion when there is a redirect
@iNecas iNecas merged commit e44f527 into Apipie:master Feb 28, 2015
@iNecas
Copy link
Member

iNecas commented Mar 18, 2015

apipie-rails 0.3.2 with this fix was just release. Thanks @stormsilver for the contribution!

@stormsilver stormsilver deleted the rails42 branch May 24, 2023 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants