title: Timeout in Firebase Functions
body: <p>I am testing a set of Cloud Functions locally and have emulators for Database, Firestore, Hosting, PubSub and Functions setup as required. I am trying to do the following:</p>
  
  <ol>
  <li>In the <code>functions/index.js</code> file, I am importing modules that are used as HTTP trigger for different endpoints.</li>
  <li>I am using Dependency Injection (DI) for allowing the modules to access all common dependencies from the <code>functions/index.js</code> file. </li>
  </ol>
  
  <p>My approach in code so far:</p>
  
  <p>functions/index.js</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  const express = require('express');
  const cors = require('cors');
  const session = require('express-session');
  const firestoreSession = require('connect-session-firebase')(session);
  const cookieParser = require('cookie-parser');
  const { validationResult } = require('express-validator');
  
  const ref = admin.initializeApp({
      databaseURL: 'http://localhost:8080',
      credential: admin.credential.cert('path/to/credential.js')
  });
  
  // services
  const UserService = require('./user/index')({
      express, cookieParser, cors, ref,
      session, firestoreSession, validationResult
  });
  
  exports.user = functions.https.onRequest(UserService);
  </code></pre>
  
  <p>The HTTP request triggers are instances of express. I am injecting the dependencies (as in UserService) and then exposing the trigger under the URL BASE_URL/user. There are few other services like this. Inside the UserService, I have the following:</p>
  
  <pre><code>module.exports = function({
      express, cors,
      session, firestoreSession,
      cookieParser, validationResult, ref
  }) {
      const Validators = require('./validators');
  
      const userserv = express();
      userserv.use(cors({ origin: true }));
      userserv.use(session({
          name: 'user_session',
          resave: true,
          saveUninitialized: true,
          secret: 'TOKEN_SECRET_HERE',
          store: new firestoreSession({
              database: ref.database()
          })
      }));
  
      userserv.post('/login', (req, res, nxt) =&gt; {
          res.json({
              status: 200,
              message: 'Logged In'
          });
      });
      // ... other route handlers
      return userserv;
  };
  </code></pre>
  
  <p>functions/user/index.js</p>
  
  <p>Now when I start the emulators, everything starts fine. When I send a POST request using curl, the function just freezes until a timeout occurs. Here's a sample of the curl request:</p>
  
  <pre><code>curl -X POST -H 'Content-Type:application/json' -d '{}' http://localhost:5001/&lt;App&gt;/&lt;region&gt;/user/login
  </code></pre>
  
  <p>When the timeout occurs or when I manually stop the emulators, I see the JSON response as expected. Not sure what's causing the function to freeze. My guess would be something related to the database setup for the session store that's causing the function to enter an infinite loop of requests. Some suggestions will be greatly appreciated. </p>
  
  <p>Note: I am using DI because I want to manage the imports in one file and also share the session store among the services. The way I have seen to setup firebase session store uses the app ref from <code>admin.initializeApp</code> and so, it would be easy to pass that as an argument to the all the imported service modules. </p>
  
  <p><strong>Update 1</strong></p>
  
  <p>The problem is definitely with the session middleware. Commenting out the session setup code in <code>functions/user/index.js</code> doesn't cause the server to freeze when the request is made and a response is sent promptly as expected.</p>
  
  <p><strong>Update 2</strong>
  After looking at the debug logs, this is what seemed "problematic" from the <code>database-debug.log</code>:</p>
  
  <pre><code>WARNING: An illegal reflective access operation has occurred
  WARNING: Illegal reflective access by io.netty.util.internal.ReflectionUtil (file:/home/zerocool/.cache/firebase/emulators/firebase-database-emulator-v4.3.1.jar) to field sun.nio.ch.SelectorImpl.selectedKeys
  WARNING: Please consider reporting this to the maintainers of io.netty.util.internal.ReflectionUtil
  WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
  WARNING: All illegal access operations will be denied in a future release
  </code></pre>