diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb
index <HASH>..<HASH> 100644
--- a/actionpack/lib/action_view/helpers/cache_helper.rb
+++ b/actionpack/lib/action_view/helpers/cache_helper.rb
@@ -51,7 +51,9 @@ module ActionView
           # This dance is needed because Builder can't use capture
           pos = output_buffer.length
           yield
-          fragment = output_buffer.slice!(pos..-1)
+          safe_output_buffer = output_buffer.to_str
+          fragment = safe_output_buffer.slice!(pos..-1)
+          self.output_buffer = ActionView::OutputBuffer.new(safe_output_buffer)
           controller.write_fragment(name, fragment, options)
         end
       end
Fragment caching needs to operate on the pure output, not the
safebuffer.