<commit_before>module Chillout
  module Middleware
    class CreationsMonitor
      def initialize(app, client)
        @app = app
        @client = client
      end

      def call(env)
        response = @app.call(env)
      ensure
        if Thread.current[:creations]
          @client.logger.info "Non-empty creations container found"
          @client.enqueue(Thread.current[:creations])
          Thread.current[:creations] = nil
        end
        response
      end
    end
  end
end
<commit_msg>Apply safeware patterns for middleware.<commit_after>module Chillout
  module Middleware
    class CreationsMonitor
      def initialize(app, client)
        @app = app
        @client = client
      end

      def call(env)
        dup._call(env)
      end

      def _call(env)
        status, headers, body = @app.call(env)
        return status, headers, body
      ensure
        if Thread.current[:creations]
          @client.logger.info "Non-empty creations container found"
          @client.enqueue(Thread.current[:creations])
          Thread.current[:creations] = nil
        end

        body.close if body && body.respond_to?(:close) && $!
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08585858585858586, 'fraction_numerical': 0.0, 'mean_word_length': 2.040920716112532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3944407', 'n_tokens_mistral': 338, 'n_tokens_neox': 330, 'n_words': 87}