title: Spurious MaxListenersExceededWarning EventEmitter memory leak when running consecutive firestore queries
body: <p>I have a firebase HTTP function which in turns calls some firestore operations. If I call the HTTP function several times, letting each call finish before calling the next, I get the following error in the firebase functions log:</p>
  
  <pre><code>(node:2) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added. Use emitter.setMaxListeners() to increase limit
  </code></pre>
  
  <p>The firebase function is an import task which takes the data to import, check for duplicates by calling a firestore query, and if there is none, it adds the data to the firestore DB by another DB operation.</p>
  
  <p>Here is the firebase function, with parts removed for brevity:</p>
  
  <pre class="lang-js prettyprint-override"><code>module.exports = functions.https.onCall(async (obj, context) =&gt; {
    // To isolate where the problem is
    const wait = (ms: number) =&gt; new Promise(resolve =&gt; setTimeout(resolve, ms))
    try {
      const photo = getPhoto(obj)
      // Query to look for duplicates
      const query = db
        .collection(`/Users/${context.auth.uid}/Photos`)
        .where('date', '==', photo.date)
        .where('order', '==', photo.order)
        .limit(1)
  
      await wait(300)
      log.info('Before query')
      const querySnap = await query.get()
      log.info('After Query')
      await wait(300)
  
      // And then the rest of the code, removed for brevity
    } catch (error) {
      throw new functions.https.HttpsError('internal', error.message)
    }
  })
  </code></pre>
  
  <p>I inserted a pause before and after the <code>const querySnap = await query.get()</code> to show that it really is this invocation that causes the error message.</p>
  
  <p>I also set the firestore logger to output its internal logging to help debug the issue, by doing this:</p>
  
  <pre class="lang-js prettyprint-override"><code>import * as admin from 'firebase-admin'
  admin.initializeApp()
  admin.firestore.setLogFunction(log =&gt; {
      console.log(log)
  })
  </code></pre>
  
  <p>So the more complete log output I get is this: (read it bottom to top)</p>
  
  <pre><code>12:50:10.087 pm: After Query
  12:50:10.087 pm: Firestore (2.3.0) 2019-09-13T19:50:10.087Z RTQ7I [Firestore._initializeStream]: Received stream end
  12:50:10.084 pm: Firestore (2.3.0) 2019-09-13T19:50:10.084Z RTQ7I [Firestore._initializeStream]: Releasing stream
  12:50:10.084 pm: Firestore (2.3.0) 2019-09-13T19:50:10.084Z RTQ7I [Firestore.readStream]: Received response: {"document":null,"transaction":{"type":"Buffer","data":[]},"readTime":{"seconds":"1568404210","nanos":76771000},"skippedResults":0}
  12:50:10.026 pm: (node:2) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added. Use emitter.setMaxListeners() to increase limit
  12:50:10.020 pm: Firestore (2.3.0) 2019-09-13T19:50:10.020Z RTQ7I [Firestore.readStream]: Sending request: {"parent":"[redacted]/documents/Users/SpQ3wTsFzofj6wcsF7efRrSMrtV2","structuredQuery":{"from":[{"collectionId":"Photos"}],"where":{"compositeFilter":{"op":"AND","filters":[{"fieldFilter":{"field":{"fieldPath":"date"},"op":"EQUAL","value":{"stringValue":"2019-06-26"}}},{"fieldFilter":{"field":{"fieldPath":"order"},"op":"EQUAL","value":{"integerValue":0}}}]}},"limit":{"value":1}}}
  12:50:10.019 pm: Firestore (2.3.0) 2019-09-13T19:50:10.019Z RTQ7I [ClientPool.acquire]: Re-using existing client with 100 remaining operations
  12:50:10.012 pm: Before query
  </code></pre>
  
  <p>The interesting thing is that I usually run these imports in batches of 10. I seem to only get the error during the first batch of 10. If I then quickly run more batches, I don't seem to get the error again. But if I wait some time, it returns. Also, it is not consistent in which invocation within a batch the error occurs. It may be the 9th or 2nd or invocation, or any other.</p>
  
  <p>Finally, the error doesn't stop execution. In fact, the imports seem to never fail. But, I don't like have unaccounted for errors in my logs! I won't be able to sleep at night with them there. :-)</p>
  
  <p>I'm grateful for any help you can offer.</p>