title: Multiple curl leads to "Too many open files error" on Windows
body: <p>I have an external REST API which handles storing data in "Data Store".</p>
  
  <p>On a file upload, their is a Ruby library which calls this API and passes it the data array which then gets stored in the database by the external API.</p>
  
  <p>I try to pass small chunks of array to the API so as to limit the post body content length in any curl call.</p>
  
  <p>The library call looks like this</p>
  
  <pre><code>def add_data(table_name, table_data)
  
    url    =  "#{ExternalAPI::URL}/addData"
    m_curl = Curl::Multi.new
  
    begin
  
      chunks = table_data.each_slice(ExternalAPI::BATCH_SIZE).to_a
  
      chunks.each do |data_chunk|
  
        data = {
            "tableName" =&gt; table_name,
            "data"      =&gt; data_chunk
        }.to_json
  
        curl = Curl::Easy.new(url)
        curl.headers = {}
        curl.headers['Content-type'] = 'text/plain'
        curl.timeout = 300
        curl.post_body = data
        m_curl.add(curl)
      end
  
      m_curl.perform
      true
  
    rescue Exception =&gt; e
      puts "Curl Failed #{e.message}"
      puts "#{e.backtrace}"
      Rails.logger.error "Curl Failed #{e.message}"
      return false
    end
  
  end
  </code></pre>
  
  <p>This causes too many open connections error in Webrick in development mode.
  I assumed <code>Multi::Curl</code> either recycles the connections but I'm not sure whether that happens internally. </p>
  
  <p>I also tried to create a new curl connection in the <code>for</code> loop and close it at the end of the loop (I know its inefficient) but it still led to the same error.</p>
  
  <p>Can anyone please shed some light on this?</p>