Skip to content

V2 Functions crashing when deployed with 12.6.1 #6414

Closed

Description

@milo-

After deploying using firebase-tools 12.6.1, our functions are crashing at runtime with the following stacktrace:

Error: TypeError: Cannot read properties of undefined (reading 'headers')
    at /layers/google.nodejs.yarn/yarn_modules/node_modules/firebase-functions/lib/v2/trace.js:12:68
    at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:141:25
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at console.error (/layers/google.nodejs.yarn/yarn_modules/node_modules/firebase-functions/lib/logger/compat.js:31:23)
    at sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:27:17)
    at sendResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:37:40)
    at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:34:40
    at bound (node:domain:433:15)
    at runBound (node:domain:444:12)
    at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:142:60
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

We're also crashing in our own code when trying to read request.rawBody.toString() as rawBody is undefined. It looks like something is wrong with the request objects being passed to the http functions.

We saw a very similar issue in 12.5.0 (#6290)

It looks like with this PR #6376 the issue might have been reintroduced.

@blidd-google can you take a look at this please?

[REQUIRED] Environment info

firebase-functions v2
Node 18.12.1

firebase-tools:

12.6.1

Platform:

[REQUIRED] Test case

Deploy a V2 http function with 12.6.1

Redeploy with 12.4.8 and functions work correctly

[REQUIRED] Steps to reproduce

Deploy a V2 http function with 12.6.1

[REQUIRED] Expected behaviour

It should not crash, correct properties should be defined on the request/response object

[REQUIRED] Actual behaviour

Crash, request object properties are undefined

Activity

  1. google-oss-bot commented on Sep 28, 2023

    @google-oss-bot
    Collaborator

    This issue does not seem to follow the issue template. Make sure you provide all the required information.

  2. aalej commented on Sep 28, 2023

    @aalej
    Contributor

    Hey @milo-, sorry to hear you’re encountering this issue. I’m currently trying to reproduce the error you mentioned but so far I am unable to replicate this. I tried deploying a mixture of v1 and v2 HTTP functions:

    const functionv1 = require("firebase-functions/v1");
    const functionv2 = require("firebase-functions/v2");
    const logger = require("firebase-functions/logger");
    
    exports.helloWorld1 = functionv1.https.onRequest((request, response) => {
      const rawBody = request.rawBody.toString();
      logger.info(`rawBody: ${rawBody}`, { structuredData: true });
      response.send(`Hello from Firebase v1! ${rawBody}`);
    });
    
    exports.helloWorld2 = functionv2.https.onRequest((request, response) => {
      const rawBody = request.rawBody.toString();
      logger.info(`rawBody: ${rawBody}`, { structuredData: true });
      response.send(`Hello from Firebase v2 2! ${rawBody}`);
    });
    
    exports.helloWorld3 = functionv2.https.onRequest((request, response) => {
      const rawBody = request.rawBody.toString();
      logger.info(`rawBody: ${rawBody}`, { structuredData: true });
      response.send(`Hello from Firebase v2 3! ${rawBody}`);
    });

    Making a POST request to any of the HTTP functions above does not raise any errors. In case I’m missing anything here, please let me know.

    Could you provide additional information here such as if you’re deploying a mixture of v1 and v2 functions or just multiple v2 functions? By any chance, if you can provide a sample code snippet of the function(s) you’re deploying, we would highly appreciate it.

  3. added
    Needs: Author Feedback
    on Sep 28, 2023
  4. milo- commented on Sep 28, 2023

    @milo-
    Author

    Hi @aalej, yep we're deploying ~60 functions, 50/50 mixture of v1 functions and v2 functions.

    We were seeing this issue in the following v2 functions:

    • beforeUserSignedIn
    • onSchedule
    • onRequest

    All these functions are group functions. Ill try and get a smaller repro for you.

  5. added and removed
    Needs: Author Feedback
    on Sep 28, 2023
  6. lelimit commented on Sep 28, 2023

    @lelimit

    I also got problems at runtime after deploying onCall v2 functions with with 12.6.1.
    TypeError: res.on is not a function

  7. milo- commented on Sep 29, 2023

    @milo-
    Author

    I've been unable to create a small reproduction. But can confirm this issue happens every time we deploy with 12.6.1.

    Deploying the individual function fixes the issue, but when all functions are deployed together, the runtime error res.headers is undefined starts getting thrown again.

    @blidd-google I've been digging into the source and the logs. Something that seems strange is there's a v1 and a v2 scraper, so I'd expect a source token for v1, source token for v2. However during deployment, it logs 4 different source tokens:

    3 x UUID source tokens
    1 x 384 character source token

  8. blidd-google commented on Oct 2, 2023

    @blidd-google
    Contributor

    Hi @milo-, very sorry for all the disruptions you've run into. I'm investigating the issue now and am also having trouble creating a repro of the bug you've described, but I'll keep this thread updated.

  9. blidd-google commented on Oct 3, 2023

    @blidd-google
    Contributor

    A couple of follow up questions:

    • Is the correct grouped function being invoked before crashing at runtime due to the missing request object?
    • In addition to grouped functions, are you using multiple codebases to organize your functions? This would explain why more than 2 source tokens.
    • What is the shape of the HTTP request you are sending to the onRequest function?
    • Can you provide the logs from running the deploy command with the --debug flag?
  10. added
    Needs: Author Feedback
    and removed on Oct 3, 2023
  11. 23 remaining items

  12. lox commented on Oct 22, 2023

    @lox

    Firebase folks, feel free to book me here for a quick video call to assist with reproducing the issue:

    https://zcal.co/lox/firebase-6414

  13. ChromeQ commented on Oct 22, 2023

    @ChromeQ

    I also ran into this since 12.6.0 (and 12.6.1 and 12.7.0)
    I can also confirm from my logs that it is calling the incorrect functions after deployment. This is because the incorrect functions are potentially being deployed somehow, I have all v2 functions (3 firestore documents handlers + 2 scheduled functions) and I can see the following in the logs when deploying:
    image

    It shows the incorrect function was (potentially?) deployed, in this case "towntime" is the name of a new scheduled function added - this can be seen more clearly when searching for "Default STARTUP TCP probe succeeded after 1 attempt for container" and it deployed and worked correctly until the 20th October (when I upgraded)
    image

    Happy to get on that call with you I just booked using your link @lox

  14. lox commented on Oct 22, 2023

    @lox

    @ChromeQ I am experiencing the issue myself, not a Firebase dev 😅

  15. blidd-google commented on Oct 23, 2023

    @blidd-google
    Contributor

    Hi all, we've been able to reproduce the error and have identified the underlying bug. We're currently working on a fix and will keep folks updated on this issue thread. Thanks for your continued patience as we iron out the remaining kinks with the single builds feature - we believe that the improvements in deploy speed & reliability that single builds offer are worth the effort, and you should be able to take advantage of those improvements without problems very soon!

  16. lox commented on Oct 27, 2023

    @lox

    @blidd-google are you able to address these questions:

    1. Did this affect all V2 functions? What was the identified issue?
    2. If functions were being incorrectly invoked, could there have been security impact? For instance onDocumentUpdate functions triggered by http without auth checks?
    3. Why was a change of this magnitude introduced in a minor version? Will the release regime in future change?

    My confidence in Firebase Functions is at an all time low after this issue and the recent multi-hour deployment outage that didn't even merit a status page update.

    Would really help to see a nuanced response here, are there any product managers involved in Firebase any more or is it more best-effort internally?

  17. joehan commented on Oct 31, 2023

    @joehan
    Member

    First off, a quick update: We have a backend fix for this issue rolling out to production. It should land this week.

    To answer @lox 's questions:

    Did this affect all V2 functions? What was the identified issue?
    Though there were a couple issues related to this launch, this specific issue only affected deployments with multiple V2 functions using different trigger types.

    A brief bit of background: previously, deployments of many functions would generate some nearly identical artifacts for each function. This update used 'source tokens' to tell Cloud Functions to reuse these artifacts where possible to make deploys faster, cheaper, and more reliable.

    The cause of this issue is a bug where functions deployed with a source token used the trigger type of the function that created that source token instead of the correct trigger type. This trigger type controls how the Cloud Function decodes requests and responses. So, for example, if a source token from a Firestore triggered function was used for a HTTPS function, it would error out when trying to decode a HTTPS request as a request from Firestore.

    If functions were being incorrectly invoked, could there have been security impact? For instance onDocumentUpdate functions triggered by http without auth checks?
    This issue did not have any security impact. The correct functions are being called, this was just causing errors with how requests were decoded.

    Why was a change of this magnitude introduced in a minor version? Will the release regime in future change?
    We added this in a minor version because it is not an 'incompatible API change', per https://semver.org/.

    We do understand the frustration here, and we definitely need to do a better job of catching issues like this before they reach you. This issue exposed some gaps in our test coverage, and we're currently working on a revamp the functions integration test suite. Additionally, while Github is a great place to report issues, please reach out to Firebase Support for help with any billing issues.

  18. lox commented on Oct 31, 2023

    @lox

    Thanks for the detailed update @joehan!

    Very much looking forward to being able to benefit from artifact re-use (and speed up our deploys).

  19. ChromeQ commented on Nov 2, 2023

    @ChromeQ

    Thanks for the update @joehan , I gave this a try and it seemed to work for the most part (although I only deployed two functions of the same type so may not have been a problem as per your description above)
    However there is an issue within the logs which gave me the impression it was not resolved:

    image

    Ignore the error (that was me prematurely deleting a storage bucket!) but you can see the bit above saying the function named "scrape-happiest-hour" was deployed and successfully probed the container, this was using [email protected].
    After the upgrade to [email protected] this morning and another deploy at 10.36am - you can see the probe on the container named "scrape-towntime" (the other function I deployed at the same time as this one).
    But the next lines are the invocation of the function and the correct name comes through "scrape-happiest-hour" - so it seems it is working correctly.

    But as I mentioned the container name is incorrect and mixed up with the first named function, this is the command I ran:
    firebase deploy --only functions:scrapeTowntime,functions:scrapeHappiestHour so it looks like the first function listed is the name coming through all the others as described in your analysis earlier.

    Hope this helps track down any other lingering problem that may still exist.

  20. wokkaflokka commented on Nov 2, 2023

    @wokkaflokka
    Contributor

    @joehan thank you for the clear explanation -- very well done.

    @ChromeQ -- If i take the explanation from @joehan, they've stated "We have a backend fix for this issue rolling out to production. It should land this week."

    Based on that comment, it's not clear that the proposed fix has been released. The name mismatch is characteristic of the initial reported error and is more generally characteristic of the class of error as disclosed by maintainers. I hope your issue is fixed and wish I could help more.

  21. arnoutvandervorst commented on Nov 2, 2023

    @arnoutvandervorst

    Based on that backend fix, any version after 12.5.4 won't work in our cases since that version started using single builds. When the backend fix has been released, we can safely upgrade to any new version.

  22. milo- commented on Nov 5, 2023

    @milo-
    Author

    @joehan has the backend fix landed in production? Thanks!

  23. blidd-google commented on Nov 7, 2023

    @blidd-google
    Contributor

    The backend fix for the single builds feature has been fully released; functions deployed with CLI versions >=12.6.2 will be able to correctly decode requests. We will continue to monitor this thread in case anything else comes up, but thank you all for your patience as we ironed out the kinks for this feature. As @joehan mentioned, the Firebase functions team is working on overhauling our integration tests to mitigate potential future issues as we continue to make improvements to 2nd gen functions.

  24. joehan commented on Nov 7, 2023

    @joehan
    Member

    I'm going to close this issue for now since AFAIK, it is fixed. If you've had any issues with your functions, the quickest way to fix them should be to update to the latest version of firebase-tools and redeploy them. Please don't hesitate to ping here again (or open new issues) if you are still having trouble with functions deploy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions