diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-03-10 18:06:32 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-10 18:06:32 -0500 |
commit | c56c4e4121635ec1221d1a83dd53cedf92ffa283 (patch) | |
tree | de9fc4a216073018802fd2a30f758233eaca26fc | |
parent | 56f23c5132a748488da4a2ed58b1aff4bf2b0f6b (diff) | |
parent | e63fb2407a1dd5de1bfe8d4d8b1976ce51022f0c (diff) | |
download | rails-c56c4e4121635ec1221d1a83dd53cedf92ffa283.tar.gz rails-c56c4e4121635ec1221d1a83dd53cedf92ffa283.tar.bz2 rails-c56c4e4121635ec1221d1a83dd53cedf92ffa283.zip |
Merge pull request #28373 from godfat/fix-cache-middleware-with-throw
Make sure local cache cleared even it's throwing:
-rw-r--r-- | activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb | 8 | ||||
-rw-r--r-- | activesupport/test/caching_test.rb | 11 |
2 files changed, 15 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb b/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb index 174cb72b1e..4c3679e4bf 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb @@ -28,13 +28,13 @@ module ActiveSupport response[2] = ::Rack::BodyProxy.new(response[2]) do LocalCacheRegistry.set_cache_for(local_cache_key, nil) end + cleanup_on_body_close = true response rescue Rack::Utils::InvalidParameterError - LocalCacheRegistry.set_cache_for(local_cache_key, nil) [400, {}, []] - rescue Exception - LocalCacheRegistry.set_cache_for(local_cache_key, nil) - raise + ensure + LocalCacheRegistry.set_cache_for(local_cache_key, nil) unless + cleanup_on_body_close end end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index c543122d91..c67ffe69b8 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -47,6 +47,17 @@ module ActiveSupport assert_raises(RuntimeError) { middleware.call({}) } assert_nil LocalCacheRegistry.cache_for(key) end + + def test_local_cache_cleared_on_throw + key = "super awesome key" + assert_nil LocalCacheRegistry.cache_for key + middleware = Middleware.new("<3", key).new(->(env) { + assert LocalCacheRegistry.cache_for(key), "should have a cache" + throw :warden + }) + assert_throws(:warden) { middleware.call({}) } + assert_nil LocalCacheRegistry.cache_for(key) + end end end end |