-
Notifications
You must be signed in to change notification settings - Fork 24
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
deferred client initialization #287
Comments
We think this might be a potential solution to googleapis/google-auth-library-nodejs#798 The root problem being that A work around people can test in the meantime is the following: const {Storage} = require('@google-cloud/storage');
const {VisionClient} = require('@google-cloud/vision');
let storage;
let vision;
exports.handler = async (req, res) {
if (!storage) {
storage = new Storage();
vision = new Vision();
}
await storage.someOperation();
await vision.someOperation();
} Lazy initialize any clients you are creating, such that the @adamworrall, @kmilo93sd, @SaschaHeyer, @bdaz, @elihorne, I wanted to put this potential fix on your radar. Also, could I bother you to try the suggestion I link to in this comment? If it makes you're issues go away, it's a good indicator that we're on the right track. |
@bcoe I haven't seen the error pop up for a while but I also made this change for most of my functions just to see if it made a difference. I suspect you're right about what's going on here. The first time I noticed the issue was after adding the google-logging/winston package, and initializing the client in the global scope. It should be noted that initializing the client in global scope is specifically recommended in the GCF docs:
I wonder if something has changed since those docs were written, maybe in the way Node 10 functions are executed? Docs should probably be updated, though. Thanks very much for your diligence, this error has been a source of anxiety for quite a while. I'll followup if it returns for a function with no async work in the global scope. |
@adamworrall we haven't yet been able to update some of the libraries, as we're holding out to drop Node 8 support, before releasing any that need a major bump. However, for a healthy number of our client libraries we've released Alex's deferred client initialization fix. This should mean that, in the near future, you would be able to go back to initializing a client in the global scope, and as long as the first operation is performed int he handler function, things will behave as expected. |
Still getting this Every function I have that uses firestore crashes on first run, they randomly then crash on subsequent runs. I have even put together a simple test to just write and trigger from firestore, removing any business logic or custom code.
|
@dknedlik This might not be the best place to discuss Firestore since it has a lot of code wrapping the generated library. Let's summon @schmidt-sebastian here and he might decide to move this discussion to the Firestore repository :) |
@alexander-fenster Thanks appreciate it. I have been chasing this for quite awhile and I ended up here from another issue that was also discussing firestore but the issue has been locked. |
@dknedlik I might be able to look at this over in https://github.com/googleapis/nodejs-firestore. Please include log messages (obtained via |
@schmidt-sebastian thank you. I was able to get it working. Turned out to be a globally constructed cloud Logger instance in another file that was causing the issues. I was able to move construction of the cloud logger into a function scope and that finally cleared up the last of the issues. |
@dknedlik That explains a lot. Logger ( |
Hi, this and the other thread have been useful! I've moved @google-cloud/logging to lazy init. However my issue is with the no default creds error. What is the best practice for this standard pattern:
Appreciate any guidance! |
@markterrill From your snippet it looks like you're using |
Let me investigate! Tha
Let me investigate, thanks! I've been using it for:
ok, found this: https://googleapis.dev/nodejs/iot/latest/google.cloud.iot.v1.DeviceManager.html#modifyCloudToDeviceConfig1 |
Cheers. Updated, working, pushed to beta. |
We need to move all asynchronous code (promises creation) from the client constructor to the separate
init
method that will be called by all class methods (once, at the first request). Cloud functions don't like asynchronous stuff in constructors.To be discussed:
async
(possibly dropping callbacks)The text was updated successfully, but these errors were encountered: