# Rack::Response#body vs ActionDispatch::Response#body

**URL:** <https://discuss.rubyonrails.org/t/rack-response-body-vs-actiondispatch-response-body/56015>\
**Category:** rubyonrails-core\
**Created:** [November 30, 2010, 10:52pm UTC](https://discuss.rubyonrails.org/t/rack-response-body-vs-actiondispatch-response-body/56015 "2010-11-30T22:52:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Firebaugh](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@John\_Firebaugh](https://discuss.rubyonrails.org/u/John_Firebaugh)\
**Post date:** [November 30, 2010, 10:52pm UTC](https://discuss.rubyonrails.org/t/rack-response-body-vs-actiondispatch-response-body/56015/1 "2010-11-30T22:52:17Z")

</div>

The definitions of #body in Rack::Response and ActionDispatch::Response are competing and causing performance problems with streaming responses. Rack::Response provides attr\_accessor :body, while ActionDispatch::Response overrides that as follows:

&nbsp;&nbsp;&nbsp;&nbsp;def body &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str = '' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each { |part| str \<\< part.to\_s } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;str &nbsp;&nbsp;&nbsp;&nbsp;end

This is problematic in instances where a streaming body is used, due to the definition of Rack::Response#close:

&nbsp;&nbsp;&nbsp;&nbsp;def close &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body.close if body.respond\_to?(:close) &nbsp;&nbsp;&nbsp;&nbsp;end

Given these definitions, executing the Rack protocol on an ActionDispatch::Response instance (calling '#each', then '#close') causes the body to be enumerated twice -- once via the expected call to #each, and a second time via the call chain #close -\> #body -\> #each. See [#4554 render :text =\> proc { ... } regression - Ruby on Rails - rails](https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4554) for a user report of this problem.

This could be fixed by using the instance variable directly:

&nbsp;&nbsp;&nbsp;&nbsp;def close &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@body.close if @body.respond\_to?(:close) &nbsp;&nbsp;&nbsp;&nbsp;end

However, I think ActionDispatch::Response's #body override is dangerous and violates the principle of least surprise by removing the symmetry between #body and #body=. IMHO, it would be better to remove the override. If the concatenating behavior is necessary, supply a method named #body\_text or a similar name that makes it clear that it disables streaming. This option causes numerous Rails test failures, however, as many tests are written to expect #body to return a string.

Thoughts?

---

<div class="post-metadata">

**Author:** ![John\_Firebaugh](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@John\_Firebaugh](https://discuss.rubyonrails.org/u/John_Firebaugh)\
**Post date:** [December 1, 2010, 12:52am UTC](https://discuss.rubyonrails.org/t/rack-response-body-vs-actiondispatch-response-body/56015/2 "2010-12-01T00:52:11Z")

</div>

I agree that would fix this particular issue. My reluctance with that is that AD::Response#body would remain a dangerous method on its own, because it does not obey the semantics established by the base class. It's a Liskov Substitution Principle violation -- AD::Response cannot be safely substituted where Rack::Response is expected.

---

<div class="post-metadata">

**Author:** ![Yehuda\_Katz](https://avatars.discourse-cdn.com/v4/letter/y/f14d63/32.png) [@Yehuda\_Katz](https://discuss.rubyonrails.org/u/Yehuda_Katz)\
**Post date:** [December 1, 2010, 6:39am UTC](https://discuss.rubyonrails.org/t/rack-response-body-vs-actiondispatch-response-body/56015/3 "2010-12-01T06:39:46Z")

</div>

ActionDispatch::Response has two modes:

- A mode for building up a response that will then be passed to a Rack server. The only interface on this object is #each
- A mode for taking a tuple of [status, headers, body] and making it available for inspection using the Response API. The #body method is for the **second** mode, which is mostly used in testing and debugging. The fact that #close calls the #body method is a mistake, causing a (slow) debugging method to get called in the (fast) Rack response mode.

We should probably be using a different method for the debugging version of body, if Rack is calling #close on the result from #body (maybe something like string\_body). Alternatively, we may want to create a debugging response object, and a to\_something method to convert a response to that, but I worry that it has untold potential for breakage.

Yehuda Katz Architect | Strobe (ph) 718.877.1325
