title: Firebase Cloud Function Set Timeout 540s but ended at 60s
body: <p>I have my cloud function</p>
  <pre><code>import * as functions from 'firebase-functions';
  const runtimeOpts = {
      timeoutSeconds: 540,
      memory: &quot;1GB&quot; as &quot;1GB&quot;
  
  }
  ...
  export const helloWorldAllowCORS = functions.runWith(runtimeOpts).https.onRequest(async (request, response) =&gt; {
  
      response.set('Access-Control-Allow-Origin', '*');
      response.set('Access-Control-Allow-Credentials', 'true'); // vital
  
      response.set('Keep-Alive', 'timeout=5, max=1000');
  
      if (request.method === 'OPTIONS') {
          // Send response to OPTIONS requests
          response.set('Access-Control-Allow-Methods', 'GET');
          response.set('Access-Control-Allow-Headers', 'Content-Type');
          response.set('Access-Control-Max-Age', '3600');
          response.status(204).send('');
      } else {
          let run = async (ms: any) =&gt; {
              await new Promise(resolve =&gt; setTimeout(resolve, ms));
          }
          await run(request.body.data.wait);
  
          response.send({
              data: {
                  status: true
                  , message: 'message v2 '
              }
          })
      }
  });
  </code></pre>
  <p>And trigger from angular/fire plugin</p>
  <pre><code> const callable = this.afFnc.httpsCallable(&quot;helloWorldAllowCORS&quot;);
      
      console.log(new Date());
   
      // wait for 3 min
      this.data = await callable({ wait: 180 * 1000 });
  
      this.data.subscribe(
        res =&gt; {
          console.log(new Date());
          console.log(res);
        },
        err =&gt; {
          console.log(new Date());
          console.error(err);
        }
  </code></pre>
  <p>end up chrome console showing timeout error in 1min</p>
  <pre><code>calling onTestCloudFunc()
  2020-06-26T03:42:08.387Z
  2020-06-26T03:43:18.401Z
  Error: deadline-exceeded
      );
  </code></pre>
  <p>I have been stuck on the issue for few days. The official firebase doc doesn't tell much on handling CORS. Once integrated with angular/fire, the httpCallable func failed. And after i resolved the CORS issue by adding header. Then again, the new CORS bypass logic break the Timeout which supposed to run the process for 9mins.</p>
  <p>I also tested, the firebase offical <a href="https://firebase.google.com/docs/functions" rel="nofollow noreferrer">https://firebase.google.com/docs/functions</a> can run more than 1 min if i increase the timeout. But the code can only run by manually copy &amp; paste into chrome browser.</p>
  <p>What i noticed, when firebase + angular/fire + cloud function + bypass CORS, the defined Timeout 540s will failed. Anyone have a fully integrated code references?</p>
  <p>Appreciated in million~~ T_T...</p>
  <p><strong>Updates:</strong>
  I create a new onCall function
  Angular</p>
  <pre><code>console.log('Start Time: ', new Date());
  // wait for 3 min
      const callable = this.afFnc.httpsCallable(&quot;onCWaitASec&quot;);
      this.data = await callable({ wait: 3 * 60 * 1000 });
  this.data.subscribe(
    res =&gt; {
      console.log(new Date());
      console.log(res);
    },
    err =&gt; {
      console.log(new Date());
      console.error(err);
    }
  );
  </code></pre>
  <p>Firebase Cloud Func, onCall method:</p>
  <pre><code>export const onCWaitASec = functions.runWith(runtimeOpts).https.onCall(async (data, context) =&gt; {
      
          let run = async (ms: any) =&gt; {
              await new Promise(resolve =&gt; setTimeout(resolve, ms));
          }
          await run(data.wait);
  
   return {
       status: 'success',
       message: `this is onCWaitASec() return msg waited ${data.wait} `
   }
  })
  </code></pre>
  <p>Chrome Console</p>
  <pre><code>Start Time:  Fri Jun 26 2020 14:42:56 GMT+0800 (Singapore Standard Time)
  login.component.ts:128 Fri Jun 26 2020 14:44:07 GMT+0800 (Singapore Standard Time)
  login.component.ts:129 Error: deadline-exceeded
      at new HttpsErrorImpl (index.cjs.js:58)
      at index.cjs.js:373
      at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
      at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
      at push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask (zone.js:503)
      at ZoneTask.invoke (zone.js:492)
      at timer (zone.js:3034)
  </code></pre>
  <p>Firebase console also showing the timeout as 540s. And chrome console's two timestamp can proved that it timeout in 1 min
  <a href="https://i.stack.imgur.com/b4BsT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b4BsT.png" alt="enter image description here" /></a></p>
  <p><strong>Update as of 1 Jul</strong>
  Thankfully i got helps from Mau from Firebase support team, he managed to narrowed down the issue is with AngularFire package and not Firebase SDK itself. Here the latest working codes (using official Firebase JS SDK instead)</p>
  <p>app.module.ts</p>
  <pre><code>import * as firebase from &quot;firebase/app&quot;; // official JS SDK
  import { environment } from &quot;../environments/environment&quot;;
  if (!firebase.apps.length) {
     firebase.initializeApp(environment.firebaseConfig);
  }
  </code></pre>
  <p>app.component.ts</p>
  <pre><code>import * as firebase from &quot;firebase/app&quot;;
  import &quot;firebase/functions&quot;;
  ...
    async callCloudFnOfficial() {
      console.log(&quot;callCloudFnOfficial() Start Trigger At: &quot;, new Date());
  var callable = firebase.functions().httpsCallable(&quot;onCWaitASec&quot;, {timeout: 540000});
      callable({ wait: 500 * 1000 })
        .then(function(res) {
          console.log(&quot;success at &quot;, new Date());
          console.log(res);
        })
        .catch(function(error) {
          console.log(&quot;Error at &quot;, new Date());
          console.error(error);
        });
    }
  </code></pre>
  <p>Chrome console log</p>
  <pre><code>Angular is running in the development mode. Call enableProdMode() to enable the production mode.
  callCloudFnOfficial() Start Trigger At:
  2020-07-01T01:04:21.130Z
  success at
  2020-07-01T01:12:41.990Z
  {data: {…}}
  data: Object
  message: &quot;this is onCWaitASec() return msg waited 500000 &quot;
  status: &quot;success&quot;
  __proto__: Object
  __proto__: Object
  </code></pre>
  <p>As it finally able to execute for 500 seconds and return successfully. As compare to angular/fire's always return in 1 min</p>
  <pre><code>import { AngularFireFunctions } from &quot;@angular/fire/functions&quot;;
  ...
    async callCloudFn() {
      console.log(&quot;Start Trigger At: &quot;, new Date());
      const callable = this.fns.httpsCallable(&quot;onCWaitASec&quot;);
      // delay return for X seconds
      let cloudFncResp: Observable&lt;any&gt; = await callable({ wait: 500 * 1000 });
      cloudFncResp.subscribe(
        res =&gt; {
          console.log(&quot;success at &quot;, new Date());
          console.log(res);
        },
        error =&gt; {
          console.log(&quot;Error at &quot;, new Date());
          console.error(error);
        }
      );
    }
  </code></pre>
  <p>Chrome console</p>
  <pre><code>Start Trigger At:
  2020-07-01T01:13:30.025Z
  Error at
  2020-07-01T01:14:40.037Z
  Error: deadline-exceeded
  </code></pre>
  <p>Will also report as bug in AngularFire as it doesn't allow us to set timeout in client app. It's <a href="https://github.com/firebase/angularfire/issues/983" rel="nofollow noreferrer">reported in firebase GitHub before</a>. Here <a href="https://github.com/firebase/angularfire/issues/983#issuecomment-618083153" rel="nofollow noreferrer">the resolution</a> which i referred.</p>