Skip to content
This repository
Browse code

Cleanup application after #close has been called on the Rack response…

… body, not when AC::Reload#call is done.

The Rack body might lazily evaluate its output, which is for example the case
if one calls 'render :text => lambda { ... }'. The code which lazily evaluates
the output might use other application classes. So we will want to defer
cleanup until the Rack request is completely finished.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit bc2c4a45959be21e6314fba7876b32c1f04cd08a 1 parent 29c5985
Hongli Lai authored July 06, 2009 NZKoz committed July 08, 2009
37  actionpack/lib/action_controller/reloader.rb
... ...
@@ -1,14 +1,45 @@
1 1
 module ActionController
2 2
   class Reloader
  3
+    class BodyWrapper
  4
+      def initialize(body)
  5
+        @body = body
  6
+      end
  7
+
  8
+      def close
  9
+        @body.close if @body.respond_to?(:close)
  10
+      ensure
  11
+        Dispatcher.cleanup_application
  12
+      end
  13
+
  14
+      def method_missing(*args, &block)
  15
+        @body.send(*args, &block)
  16
+      end
  17
+
  18
+      def respond_to?(symbol, include_private = false)
  19
+        symbol == :close || @body.respond_to?(symbol, include_private)
  20
+      end
  21
+    end
  22
+
3 23
     def initialize(app)
4 24
       @app = app
5 25
     end
6 26
 
7 27
     def call(env)
8 28
       Dispatcher.reload_application
9  
-      @app.call(env)
10  
-    ensure
11  
-      Dispatcher.cleanup_application
  29
+      status, headers, body = @app.call(env)
  30
+      # We do not want to call 'cleanup_application' in an ensure block
  31
+      # because the returned Rack response body may lazily generate its data. This
  32
+      # is for example the case if one calls
  33
+      #
  34
+      #   render :text => lambda { ... code here which refers to application models ... }
  35
+      #
  36
+      # in an ActionController.
  37
+      #
  38
+      # Instead, we will want to cleanup the application code after the request is
  39
+      # completely finished. So we wrap the body in a BodyWrapper class so that
  40
+      # when the Rack handler calls #close during the end of the request, we get to
  41
+      # run our cleanup code.
  42
+      [status, headers, BodyWrapper.new(body)]
12 43
     end
13 44
   end
14 45
 end
97  actionpack/test/controller/reloader_test.rb
... ...
@@ -0,0 +1,97 @@
  1
+require 'abstract_unit'
  2
+
  3
+class ReloaderTests < ActiveSupport::TestCase
  4
+  Reloader   = ActionController::Reloader
  5
+  Dispatcher = ActionController::Dispatcher
  6
+
  7
+  class MyBody < Array
  8
+    def initialize(&block)
  9
+      @on_close = block
  10
+    end
  11
+
  12
+    def foo
  13
+      "foo"
  14
+    end
  15
+
  16
+    def bar
  17
+      "bar"
  18
+    end
  19
+
  20
+    def close
  21
+      @on_close.call if @on_close
  22
+    end
  23
+  end
  24
+
  25
+  def setup_and_return_body(app = lambda { })
  26
+    Dispatcher.expects(:reload_application)
  27
+    reloader = Reloader.new(app)
  28
+    headers, status, body = reloader.call({ })
  29
+    body
  30
+  end
  31
+
  32
+  def test_it_reloads_the_application_before_the_request
  33
+    Dispatcher.expects(:reload_application)
  34
+    reloader = Reloader.new(lambda {
  35
+      [200, { "Content-Type" => "text/html" }, [""]]
  36
+    })
  37
+    reloader.call({ })
  38
+  end
  39
+
  40
+  def test_returned_body_object_always_responds_to_close
  41
+    body = setup_and_return_body(lambda {
  42
+      [200, { "Content-Type" => "text/html" }, [""]]
  43
+    })
  44
+    assert body.respond_to?(:close)
  45
+  end
  46
+
  47
+  def test_returned_body_object_behaves_like_underlying_object
  48
+    body = setup_and_return_body(lambda {
  49
+      b = MyBody.new
  50
+      b << "hello"
  51
+      b << "world"
  52
+      [200, { "Content-Type" => "text/html" }, b]
  53
+    })
  54
+    assert_equal 2, body.size
  55
+    assert_equal "hello", body[0]
  56
+    assert_equal "world", body[1]
  57
+    assert_equal "foo", body.foo
  58
+    assert_equal "bar", body.bar
  59
+  end
  60
+
  61
+  def test_it_calls_close_on_underlying_object_when_close_is_called_on_body
  62
+    close_called = false
  63
+    body = setup_and_return_body(lambda {
  64
+      b = MyBody.new do
  65
+        close_called = true
  66
+      end
  67
+      [200, { "Content-Type" => "text/html" }, b]
  68
+    })
  69
+    body.close
  70
+    assert close_called
  71
+  end
  72
+
  73
+  def test_returned_body_object_responds_to_all_methods_supported_by_underlying_object
  74
+    body = setup_and_return_body(lambda {
  75
+      [200, { "Content-Type" => "text/html" }, MyBody.new]
  76
+    })
  77
+    assert body.respond_to?(:size)
  78
+    assert body.respond_to?(:each)
  79
+    assert body.respond_to?(:foo)
  80
+    assert body.respond_to?(:bar)
  81
+  end
  82
+
  83
+  def test_it_doesnt_clean_up_the_application_after_call
  84
+    Dispatcher.expects(:cleanup_application).never
  85
+    body = setup_and_return_body(lambda {
  86
+      [200, { "Content-Type" => "text/html" }, MyBody.new]
  87
+    })
  88
+  end
  89
+
  90
+  def test_it_cleans_up_the_application_when_close_is_called_on_body
  91
+    Dispatcher.expects(:cleanup_application)
  92
+    body = setup_and_return_body(lambda {
  93
+      [200, { "Content-Type" => "text/html" }, MyBody.new]
  94
+    })
  95
+    body.close
  96
+  end
  97
+end

2 notes on commit bc2c4a4

Michael Koziarski
Owner
NZKoz commented on bc2c4a4 July 30, 2009

Thanks for the report, I've reopened the original one.

I believe this is going to need to be reverted.

Please sign in to comment.
Something went wrong with that request. Please try again.