Rack::Response#finish is aliased with to_ary [1]Rack::Response#finish returns an array including selfwhen the response code is anything other than 204, 205, 304 [2]The result is an infinite loop if the result of app.call(env) (assigned to response in Rails' testing framework) is included in an array that is flattened. The reason is that Array#flatten invokes to_ary on each element that responds to to_ary, then does the same to the contents of the result of to_ary, and so on.
Because self is in the array returned by to_ary, it gets to_ary invoked and returns the same array, including self, which then gets to_ary invoked on it, which returns the same array, including self, and so on.
[1] https://github.com/rack/rack/blob/ab67e70d82911c0c86dce737c7064d0bd6daf3d4/lib/rack/response.rb#L83
[2] https://github.com/rack/rack/blob/ab67e70d82911c0c86dce737c7064d0bd6daf3d4/lib/rack/response.rb#L79
Be nice to release a version with this included. Ran into this yesterday with the latest available version of rack (by mistake of course) and had to restart my machine(!).
I'm having trouble understanding the resolution. Is this addressed in Rack 1.4.5? I'm running into some big memory leaks when running some specs.
The root issue here is that Response defines #to_ary at all.
The response object should be _explicitly_ converted to an Array using #to_a. That allows for explicit splatting, too: status, headers, body = *response.
A response is not itself Array-like, though. [response, response].flatten does not make sense. response + response does not makes sense.
By eliminating Response#to_ary, this problem goes away, and the proxy workaround can be unwound, taking us back to simpler times :heart:
@jeremy we will implement your solution.
Most helpful comment
The root issue here is that
Responsedefines#to_aryat all.The response object should be _explicitly_ converted to an
Arrayusing#to_a. That allows for explicit splatting, too:status, headers, body = *response.A response is not itself Array-like, though.
[response, response].flattendoes not make sense.response + responsedoes not makes sense.By eliminating
Response#to_ary, this problem goes away, and the proxy workaround can be unwound, taking us back to simpler times :heart: