Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulator Functions Regression - Initial Function Requests Time Out #6765

Closed
dereekb opened this issue Feb 13, 2024 · 14 comments
Closed

Emulator Functions Regression - Initial Function Requests Time Out #6765

dereekb opened this issue Feb 13, 2024 · 14 comments

Comments

@dereekb
Copy link

dereekb commented Feb 13, 2024

[REQUIRED] Environment info

firebase-tools:13.2.1

Platform:macOS

[REQUIRED] Test case

I recently updated a number of firebase dependencies on my library and noticed that hitting the emulator's Firebase functions took a long time and would often time out and be left with an internal error.

I realized that if I turned my wifi/internet connection off that they would run quickly again. I kept noticing the following line:

⚠  External network resource requested!
   - URL: "http://169.254.169.254/computeMetadata/v1/universe/universe_domain"

This is followed by the function timing out:

i  Your function timed out after ~60s. To configure this timeout, see
      https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
i  Request to function failed: Error: socket hang up

I dug around and found out that this request was being made through gcp-metadata and timing out since it can't reach the specified IP which I'm guessing is an internal IP to Google Cloud.

I run the emulators in a Docker container and think that the environment is triggering some part of gcp-metadata or the firebase emulators to believe it is running on a Google Cloud instance and is timing out. I didn't notice this behavior while on firebase-tools version 11.17.0.

I checked the versions of gcp-metadata:

dereekb@dbMBP dbcomponents % npm ls gcp-metadata google-auth-library 
@dereekb/dbx-components@10.0.17 /Users/dereekb/development/git/dbcomponents
├─┬ @google-cloud/firestore@7.3.0
│ └─┬ google-gax@4.3.0
│   └─┬ google-auth-library@9.6.3
│     └── gcp-metadata@6.1.0
├─┬ @google-cloud/pubsub@4.2.0
│ └─┬ google-auth-library@9.6.3
│   └── gcp-metadata@6.1.0
├─┬ firebase-admin@12.0.0
│ └─┬ @google-cloud/storage@7.7.0
│   └─┬ google-auth-library@9.6.3
│     └── gcp-metadata@6.1.0
├─┬ firebase-tools@13.2.1                            <---- imports older dependencies
│ ├─┬ @google-cloud/pubsub@3.7.5
│ │ ├─┬ google-auth-library@8.9.0
│ │ │ └── gcp-metadata@5.3.0
│ │ └─┬ google-gax@3.6.1
│ │   └─┬ google-auth-library@8.9.0
│ │     └── gcp-metadata@5.3.0
│ └─┬ google-auth-library@7.14.1
│   └── gcp-metadata@4.3.1          <---- version 4.3.1
├── gcp-metadata@5.3.0
└─┬ google-auth-library@8.9.0
  └── gcp-metadata@5.3.0 deduped

For now as a work around I am using the environment variable GCE_METADATA_HOST to set the host to 0.0.0.0 for the docker container so that the request fails immediately.

⚠  External network resource requested!
   - URL: "http://0.0.0.0/computeMetadata/v1/universe/universe_domain"
 - Be careful, this may be a production service.

In the newer versions of gcp-metadata (^5.3.0) I'd be able to use METADATA_SERVER_DETECTION=none.

[REQUIRED] Steps to reproduce

You can run the project at https://github.com/dereekb/dbx-components on version v10.0.17 (version v10.0.18 will have the workaround added to the Dockerfile). Run both the server ./serve-server.sh and web client ./serve-web.sh and go to http://localhost:9010/demo/app/profile/view. Change the profile info to kick off a firebase functions request.

[REQUIRED] Expected behavior

Emulator functions should not attempt to connect to or time out while trying to reach "http://169.254.169.254/computeMetadata/v1/universe/universe_domain".

[REQUIRED] Actual behavior

The first emulator function invocation attempts to reach 169.254.169.254 and times out after 60 seconds, causing the initial invocation to return an internal error.

@dereekb dereekb changed the title Emulator Functions Regression - Taking Long Time To Respond At First Emulator Functions Regression - Initial Function Requests Time Out Feb 13, 2024
@naoina
Copy link
Contributor

naoina commented Feb 13, 2024

I encountered the same issue and probably it is caused by this changes of google-gax v4.3.0.
I backed to use google-gax v4.2.1, then it works well even if firebase-tools v13.2.1.

@joehan
Copy link
Contributor

joehan commented Feb 15, 2024

Thanks for reporting this! This call is to the compute metadata server, which will only ever work on cloud compute instances. Ideally, google-gax would smoothly handle uses outside of cloud, so I've opened googleapis/gax-nodejs#1566

inlined added a commit that referenced this issue Mar 8, 2024
Fix for #6765. The latest version of google-gax attempts
to read from the metadata server (on an internal IP address).
Setting the environment variable METADATA_SERVER_DETECTION=none
by default will turn this behavior off. This is overridable with
.env.local.
@inlined
Copy link
Member

inlined commented Mar 8, 2024

A fix will hopefully come out in next week's CLI release. In the meantime you can work around the issue by putting METADATA_SERVER_DETECTION=none in $YOUR_FUNCTIONS_DIRECTORY/.env.local

inlined added a commit that referenced this issue Mar 10, 2024
* Disable metadata service detection in the emulator.

Fix for #6765. The latest version of google-gax attempts
to read from the metadata server (on an internal IP address).
Setting the environment variable METADATA_SERVER_DETECTION=none
by default will turn this behavior off. This is overridable with
.env.local.

* changelog
@inlined
Copy link
Member

inlined commented Mar 10, 2024

Closing as the fix is in next week's release.

@apeiniger
Copy link

I'm seeing the same behavior in the newest version (13.6.1):

image

Tried to fix it via the env variable mentioned above but it doesn't work.

Any ideas what could cause this?

@Akronae
Copy link

Akronae commented Apr 8, 2024

Same issue with firebase-tools@13.0.3 on Windows WSL.
However it's not happening on MacOS.
image

@Akronae
Copy link

Akronae commented Apr 8, 2024

I forgot to npx firebase login on my machine. This solved the error.
Errors messages should really be improved.

@EnduringBeta
Copy link

EnduringBeta commented May 10, 2024

A fix will hopefully come out in next week's CLI release. In the meantime you can work around the issue by putting METADATA_SERVER_DETECTION=none in $YOUR_FUNCTIONS_DIRECTORY/.env.local

The timeout this issue refers to is still happening to me in GitHub Actions, and this .env.local suggestion did not fix it.

Edit: This issue does not occur if I'm logged in via firebase login. The error does occur if I'm logged out (using FIREBASE_TOKEN), like in a CI environment.

Even though METADATA_SERVER_DETECTION="none" should be valid, and a search in my repo shows 6.1.0, it doesn't work for me. I've decided to go with OP's suggestion of GCE_METADATA_HOST="0.0.0.0".

Maybe my repo's 6.1.0 version doesn't matter since firebase-tools is global.

This is withfirebase-tools 13.8.3.

@glorat
Copy link

glorat commented Jun 30, 2024

Just to note I have also run into this issue if I'm using a local development environment with no GCP (firebase nor application default) credentials and am running the emulator under a demo project. The GCE_METADATA_HOST="0.0.0.0" trick also works for me.

I'm on latest firebase-tools of 13.12.0

Since I have this reproduced on my local laptop, ping me if further diagnostics are needed to figure out what's up

@james-dellow
Copy link

I'm encountering this on firebase-tools 13.15.0 running on Windows 11. Neither METADATA_SERVER_DETECTION="none" or GCE_METADATA_HOST="0.0.0.0" worked. I'm only using functions, firestore, hosting in the emulator.

@EnduringBeta
Copy link

@inlined Should this issue be reopened? I and several others are reporting that we're still dealing with the same behavior.

@afmeirelles
Copy link

@inlined Should this issue be reopened? I and several others are reporting that we're still dealing with the same behavior.

I do think so. In my case, I'm running the emulator inside a docker container that has no credentials as well, so even with the ENV VARs set, I get the error 'GaxiosError: request to http://0.0.0.0/computeMetadata/v1/universe/universe-domain failed, reason: connect ECONNREFUSED 0.0.0.0:80'

@james-dellow
Copy link

james-dellow commented Aug 16, 2024

The METADATA_SERVER_DETECTION override isn't working for me. I've resolved this temporarily:

  • Not calling functions.config() when running on the emulator.
  • Editing google-auth-library\build\src\auth\googleauth.js to return false on _checkIsGCE(), as it was trigging the call to the metadata endpoint.

I'll open a separate issue for this.

@jirawatee
Copy link

jirawatee commented Sep 21, 2024

I also found this issue with Firebase CLI v13.18.0 and I fixed it by adding "GCE_METADATA_HOST=localhost:8080" to .env.local

Hope this help :)

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

No branches or pull requests