Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Using Sinatra as middleware fails

#4367

When trying to use Sinatra (or any rack middleware that is capable of running as a singleton) the app stack fails to initialize. This is due to the middleware stack using #respond_to?(:call) to test if a piece of middleware is a Proc. Since Sinatra can respond to call without being initialized, and because it requires the Rack environment to be passed to it, the initialization.

Attached is a proposed patch to replace #respond_to?(:call) with #is_a?(Proc). Tests still pass after this change, and I have made the change using 2-3-stable as my base.

Reported by jaswope · April 12th, 2010 @ 02:58 AM

State: stale
Milestone: 2.3.10
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. jaswope
    jaswope
    • Tag changed from patch to 2-3-stable, patch

    April 12th, 2010 @ 02:59 AM

  2. jaswope
    jaswope
    • Title changed from Using Sinatra as middleware fails to [PATCH] Using Sinatra as middleware fails

    April 13th, 2010 @ 02:46 AM

  3. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • Milestone set to 2.3.6
    • Assigned user set to Yehuda Katz (wycats)

    This is handled in master by first checking for respond_to?(:new), the middleware convention:

    def klass
      if @klass.respond_to?(:new)
        @klass
      elsif @klass.respond_to?(:call)
        @klass.call
      else
        ActiveSupport::Inflector.constantize(@klass.to_s)
      end
    end
    

    Perhaps do the same for 2.3? Maybe just backport the entire middleware stack?

    April 13th, 2010 @ 04:06 AM

  4. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2-3-stable, patch to 2-3-stable, bugmash, patch

    May 16th, 2010 @ 02:41 AM

  5. Anil Wadghule
    Anil Wadghule

    I am able to do this

    λ cat config.ru

    require "config/environment"
    require 'sinatra'
    
    use Rack::Reloader
    use Sinatra::Application
    use Rails::Rack::LogTailer
    use Rails::Rack::Static
    use Rack::CommonLogger
    run ActionController::Dispatcher.new
    

    Attached is a app showing it's running properly.

    Am I missing something here?

    May 16th, 2010 @ 02:55 PM

  6. José Valim
    José Valim
    • Assigned user changed from Yehuda Katz (wycats) to José Valim

    Can someone provide a patch with the fix mentioned by Yehuda on 2-3-stable?

    May 16th, 2010 @ 03:55 PM

  7. Rizwan Reza
    Rizwan Reza
    • State changed from new to open

    May 16th, 2010 @ 04:18 PM

  8. Anil Wadghule
    Anil Wadghule

    Attached is a patch to backport middleware stack from Rails 3 to Rails 2-3. All the tests pass fine.

    May 16th, 2010 @ 04:54 PM

  9. Rizwan Reza
    Rizwan Reza

    Hey Anil,

    Nice work. There are some tests in middleware_stack_test you might want backport as well. Just the ones we need.

    May 16th, 2010 @ 04:57 PM

  10. José Valim
    José Valim

    There is no need to backport the whole middleware stack. And as Rizwan said, it would be good to backport the relative tests as well.

    May 16th, 2010 @ 05:01 PM

  11. Anil Wadghule
    Anil Wadghule

    Attached is a patch which backports a test from Rails 3 middleware stack to Rails 2-3

    Also I don't see any code which I should remove from the 'backport_middleware_stack' patch.

    May 16th, 2010 @ 05:33 PM

  12. José Valim
    José Valim

    Anil, your patch, for instance, change the build method to allow a block to be given. But there are no tests for it in your patch. Also there are not tests for the case described in this ticket. So if we backport all these, we need to add the respective tests. Thanks!

    May 16th, 2010 @ 05:41 PM

  13. Anil Wadghule
    Anil Wadghule

    Okay I will add patch with necessary tests.

    May 16th, 2010 @ 05:50 PM

  14. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.6 to 2.3.7

    May 23rd, 2010 @ 05:54 PM

  15. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.7 to 2.3.8

    May 24th, 2010 @ 09:40 AM

  16. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.8 to 2.3.9

    May 25th, 2010 @ 11:45 PM

  17. José Valim
    José Valim
    • State changed from open to incomplete

    Anil, you just need to backport the part relative to the naming (that Yehuda posted) and not the whole middleware stack. Still willing to try out a patch?

    June 7th, 2010 @ 10:03 AM

  18. Anil Wadghule
    Anil Wadghule

    okay Jose, I will try to provide the patch soon.

    June 7th, 2010 @ 10:52 AM

  19. Anil Wadghule
    Anil Wadghule

    José, I have attached a patch which fixes this issue.

    I could not find way to write tests for it. Even in Rails 3 master seems that there are no tests which tests @klass.respond_to?(:call) which I could just refer to. If there are any, can you tell me where are those.

    June 8th, 2010 @ 05:44 PM

  20. Neeraj Singh
    Neeraj Singh

    @Anil Can you post an app which uses Sinatra as a middleware and fails without the patch. I don't fully understand rack stuff and an app demonstrating the problem would be helpful.

    I am close to finishing writing test case for the patch you added. However a deeper understanding of the problem would help me write better test. Thanks.

    June 11th, 2010 @ 08:31 PM

  21. Anil Wadghule
    Anil Wadghule

    Neeraj, Attached is a rails app which fails on Rails 2.3.8 without my fix (try to access http://localhost:3000/).
    I am eager to see the test patch you have writteen for the fix :)

    June 11th, 2010 @ 09:31 PM

  22. Anil Wadghule
    Anil Wadghule

    Neeraj, any update on the test patch?

    June 23rd, 2010 @ 04:53 PM

  23. Neeraj Singh
    Neeraj Singh

    Hey Anil,

    Thanks for providing the app but I couldn't create the tests.

    June 23rd, 2010 @ 05:22 PM

  24. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10
    • Importance changed from to Low

    August 30th, 2010 @ 02:28 AM

  25. José Valim
    José Valim
    • State changed from incomplete to stale

    September 2nd, 2010 @ 10:56 AM