1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Stop mutating body response

If @app.call returns an object that is saved (for e.g., in a constant), the mutation results in a continuing cycle of wrapping the body in Rack::BodyProxy, eventually leading to SystemStackError
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
On branch fix-return-response-mutation-rack-logger - Tue  3 Apr 2018 19:54:28 PDT by Geoff Lee <geoff.lee@lendesk.com>
This commit is contained in:
Geoff Lee 2018-04-03 19:54:27 -07:00
parent a07d068078
commit 0ac64470ea
No known key found for this signature in database
GPG key ID: F7124AA51814A2EC
2 changed files with 21 additions and 5 deletions

View file

@ -35,9 +35,9 @@ module Rails
instrumenter = ActiveSupport::Notifications.instrumenter
instrumenter.start "request.action_dispatch", request: request
logger.info { started_request_message(request) }
resp = @app.call(env)
resp[2] = ::Rack::BodyProxy.new(resp[2]) { finish(request) }
resp
status, headers, body = @app.call(env)
body = ::Rack::BodyProxy.new(body) { finish(request) }
[status, headers, body]
rescue Exception
finish(request)
raise

View file

@ -14,14 +14,21 @@ module Rails
attr_reader :logger
def initialize(logger = NULL, taggers = nil, &block)
super(->(_) { block.call; [200, {}, []] }, taggers)
def initialize(logger = NULL, app: nil, taggers: nil, &block)
app ||= ->(_) { block.call; [200, {}, []] }
super(app, taggers)
@logger = logger
end
def development?; false; end
end
class TestApp < Struct.new(:response)
def call(_env)
response
end
end
Subscriber = Struct.new(:starts, :finishes) do
def initialize(starts = [], finishes = [])
super
@ -72,6 +79,15 @@ module Rails
end
end
end
def test_logger_does_not_mutate_app_return
response = []
app = TestApp.new(response)
logger = TestLogger.new(app: app)
assert_no_changes('response') do
logger.call('REQUEST_METHOD' => 'GET')
end
end
end
end
end