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

Firestore refusing to connect with errors on grpc #4557

Closed
kidsonfilms-python-rules opened this issue Mar 1, 2021 · 11 comments
Closed

Firestore refusing to connect with errors on grpc #4557

kidsonfilms-python-rules opened this issue Mar 1, 2021 · 11 comments

Comments

@kidsonfilms-python-rules

[REQUIRED] Describe your environment

  • Operating System version: MacOS Big Sur 11.2.2
  • Browser version: Chrome Version 88.0.4324.192
  • Firebase SDK version: 8.2.9
  • Firebase Product: Firestore (auth, database, storage, etc)
  • Electron: 11.2.3
  • Node.js: 14.6.0

[REQUIRED] Describe the problem

Whenever I try to get documents (any method) it gives the following error twice:

Uncaught TypeError: this.timerId.unref is not a function
    at BackoffTimeout.unref (backoff-timeout.ts:117)
    at new ResolvingLoadBalancer (resolving-load-balancer.ts:199)
    at new ChannelImplementation (channel.ts:242)
    at new Client (client.ts:146)
    at new ServiceClientImpl (make-client.ts:128)
    at GrpcConnection.ensureActiveStub (grpc_connection.ts:90)
    at GrpcConnection.openStream (grpc_connection.ts:175)
    at PersistentListenStream.startRpc (persistent_stream.ts:571)
    at PersistentListenStream.PersistentStream.startStream (persistent_stream.ts:443)
    at persistent_stream.ts:420

and then it gives the following error 10 seconds after trying to get the document:

[2021-03-01T00:41:28.296Z]  @firebase/firestore: Firestore (8.2.9): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

I have tried to reset our Wifi Network, tried on different days in case it's Firebase's server fault.

Steps to reproduce:

  1. Create a Node.js app and install Firebase using:
npm i firebase@latest
  1. Connect to a Firebase Project with Firestore setup
  2. Run the code in Relevant Code inside index.js

Relevant Code:

// This snippet assumes you already connected Firestore and initialized the variable as `db`
db.collection("053467").doc("Guest Picks").get()
// You can edit the location of the file you are trying to get from your own Firestore database and result will not change
@kidsonfilms-python-rules
Copy link
Author

UPDATE: I tried it on a Windows machine and got the same problem, I have deduced its either my Wifi, Firebase, or my init code for Firestore. Down below I will put my initialization code for the database. (this is copy-pasted with sensitive data redacted, it worked before and I haven't touched that code in ages so its not wrong config info)

var firebase = require("firebase/app");

// Add the Firebase products that you want to use
require("firebase/auth");
require("firebase/firestore");
require("firebase/analytics");

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
var firebaseConfig = {
    apiKey: "redacted",
    authDomain: "redacted",
    databaseURL: "redacted",
    projectId: "redacted",
    storageBucket: "redacted",
    messagingSenderId: "redacted",
    appId: "redacted",
    measurementId: "redacted"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
var db = firebase.firestore();

@hsubox76
Copy link
Contributor

hsubox76 commented Mar 1, 2021

The timeout.unref() function is a native Node method so if you don't have it, is it possible you have some library or framework that is polyfilling this, like jsdom? Some users have reported similar errors here where they're using various frameworks and tooling that use jsdom, which overrides the Nodes timers definitions.

If that's the case, putting

const timer = setTimeout(() => {});
timer.unref();

or

const timer = setInterval(() => {}, 1);
timer.unref();

anywhere in your code (without any Firebase) will get the same error, if you want to check quickly.

@kidsonfilms-python-rules
Copy link
Author

@hsubox76 Yes, it does give mostly the same error, but I am not using jsdom in either Node.js package or a CDN import. I will list all the packages that I am using below if that helps. I checked the issue that you mentioned this thread in (#4552) and tried the solutions (except ones related to jest) and they all seem to not work.

Packages Used

"devDependencies": {
    "electron": "^11.2.3"
  },
  "testEnvironment": "node",
  "dependencies": {
    "electron-store": "^7.0.2",
    "firebase": "^8.2.9",
    "grpc": "^1.24.5",
    "node-mp3-player": "^1.0.3",
    "on-change": "^2.2.3",
    "play-sound": "^1.1.3",
    "youtube-mp3-downloader": "^0.7.6",
    "yt-search": "^2.7.3",
    "ytdl-core": "^4.4.5"
  }

@hsubox76
Copy link
Contributor

hsubox76 commented Mar 1, 2021

It sounds like the broad issue is that you're using a Node method (in grpc) in a non-Node or not-completely-Node environment somehow. It might have something to do with Electron, which certainly has an environment like that in at least one of its contexts. You mentioned the code is in a "Node app", is it a simple js file that you run with node [name-of-file].js or if not, what kind of environment is it? How do you bundle and/or run it?

Interestingly when I run the code you provided, I only get an error from Analytics (because it's not supported in Node) and if I remove it, everything else is fine. I feel like this isn't a native Node environment, and as such you don't want to be using the Node bundle at all.

If this code is in Electron, maybe try reading this (I wrote it for users that were having trouble with Firebase and Electron):
https://medium.com/firebase-developers/using-firebase-in-electron-tips-and-tricks-24ac5b44bf5a

@kidsonfilms-python-rules
Copy link
Author

@hsubox76 I am currently running ALL Firebase-related code inside index.js. The way Electron starts my code is by running electron index.js which is the equivalent of running node index.js. I believe this is a fully Node.js env since it doesn't directly talk to any HTML or non-Node.js files (To talk to other files, I use IPC which is basically an Event emitter or an entire app).

About the analytics error, I do have it commented out but for some reason, it didn't copy over into GitHub.

I will try the article you sent and update it if it doesn't work or has new errors.

@hsubox76
Copy link
Contributor

hsubox76 commented Mar 1, 2021

I'm not familiar with command-line use of Electron but I know Electron has some unusual Node-like environments that aren't 100% compatible with Node. In general Electron seems to have some issues with that unref Node API grpc/grpc-node#1077 . You might have better luck resolving the issue by searching for conflicts between Electron and grpc. I can look into it but I'm not an Electron expert so it might take a while to get any answers.

@kidsonfilms-python-rules
Copy link
Author

Just to try something, I upgraded Electron (using npm i electron@latest) and it just started working! I checked the version number and its still at ^11.2.3 so I think my installation was faulty. After this fix, I tried running a Firestore command in the renderer process and it doesn't work while running a Firestore command in the main process works.

If anyone looking at this thread in the future needs more information on running Firebase in the renderer process, I recommend this article that @hsubox76 has written: https://medium.com/firebase-developers/using-firebase-in-electron-tips-and-tricks-24ac5b44bf5a

@nicograef
Copy link

nicograef commented Mar 11, 2021

Hey @kidsonfilms-python-rules and @hsubox76 ,

this is a real error in grpc-js!

It was introduced in grpc-js v1.2.7 here: grpc/grpc-node#1688

And it is fixed with this PR: grpc/grpc-node#1709

Please reopen this issue and include the fixed version (as soon as released) into the next firebase release :)

@kidsonfilms-python-rules
Copy link
Author

Hi @nicograef ,
So I am new to GitHub and don't know how to add a fix to a public repo, I'll reopen this issue in case @hsubox76 can create a PR for this.

@hsubox76
Copy link
Contributor

Thanks for the update.

See: #4552 (comment)

We auto-update our dependencies as soon as the fix in @grpc/grpc-js has been released. Thank you for reporting this and following up. Let's continue this discussion in grpc/grpc-node#1708

@nicograef
Copy link

Cool, thanks! :)

@firebase firebase locked and limited conversation to collaborators Apr 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants