You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auth error 401 once per day for admin messaging #579
I run a function every 15 minutes which sends out topic notifications via admin.messaging(), which works extremely well 95% of the time. But just once per day I get authentication error 401 (Unauthorized). It always occurs around the same time 730-930am UTC. I'm using the same global default admin initializeApp().
If there is a way to determine the admin auth status or if a cold start is required, then I might be able to re-intialise before the error?
This issue seems to have slipped through the cracks; we're very sorry about that. Can you confirm whether the issue is still happening? It's possible that there's a bug in the token refresh code.
Hi - thanks for response, yes this is still happening for our live app. I actually forgot about it myself- but just checked the cloud functions logs, and it still occurs once or twice per day- doesn't seem to be the same fixed UTC time anymore.
For context, I'm sending out 200-300 topic notifications every 15 minutes. The majority of these send fine, but at least once per day it fails with the same 401 auth error* and does not send. When the next 15 minute comes, it goes back to normal / auth + sends fine.
Would love some advice.
'An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions.'
Are you using any other Firebase APIs in this function, or just FCM? About how long does your function execute before it hits the 401 error? I'm wondering if there's a token that's not getting refreshed fast enough somewhere.
Also, how are you sending these notifications? Does your code look like:
for (let topic in topics) {
await admin.messaging.sendToTopic(topic);
}
or
let promises = []
for (topic in topics) {
topics.push(admin.messaging().sendToTopic(topic));
}
await Promise.all(promises);
My suspicion in the latter case would be that the SDK to gets a cached token for those 200-300 topics synchronously and then queues the HTTP requests. Eventually a request gets a chance to run but the token has expried. The workaround would be something like:
// something close to a reasonable connection pool size
const MAX_BATCH_SIZE = 10;
let batch = []
for (topic in topics) {
topics.push(admin.messaging().sendToTopic(topic));
if (batch.length === MAX_BATCH_SIZE) {
await Promise.all(batch);
batch = [];
}
}
await Promise.all(batch);
This would still let you run ~10 requests in parallel at a time, but would keep the token freshness check happening around when the HTTP request actually gets processed by the networking stack.
I am only using the admin.messaging.sendToTopic() call in the function. Takes about 5 seconds to run (on both the error calls, and successful). And I am using the latter example with promises.
I am going to try the batch example you posted- thanks for the tip. If it is an expired token, is there a way to increase the expiration timeout (eg. google cloud console)?
If it only takes 5s that reduces the chances that my suspicion is accurate. There's no way to increase the TTL of tokens, but that's not the right approach anyway. Any number you come up with (within reason) could be too short for a reasonable workload. From what I read in the credential section of the Admin SDK we're fetching the token every time for this type of Credential. IIRC the metadata service that provides you your credential can return credentials that expire within a few seconds. Its implementation is itself a cached credential that may expire within 1 day. That could explain your daily behavior, though the chances are slimmer if this is only a 5s request storm.
I also run another identical function on the 1H (eg. send 200-300 topic notifications every hour), and there are no problems. Both functions work exactly the same, but only the 15 minute frequency seems to throw the 401 errors.
Originally I thought the 15m was catching the errors off a cold startup, leaving the other functions to run fine. But now the error frequency is more sporadic, I'm not sure what else it could be? Would gladly test any alternative suggestions you might have?
With your permission I'd like to look at some production metrics for your functions. Rather than posting personally identifiable information here, can you contact support with the name of your project, the function that runs every 15m & fails, and the function that runs every hour and succeeds? If you give me your case ID I can look into things further.
I'm having trouble looking up the case on my side; there's lots of privacy protection measures at Google that are blocking me from searching for your case. Mind telling them to assign the bug to "inlined"?
Hey @inlined, not much activity on this yet so thought i'd ask if there was a temporary fix I could apply to circumvent this error? eg. can i check the admin auth status before sending messages and reconnect if required?
I'm bypassing some of the communication channels by responding both internally and externally, but I found out that your functions are crashing much more than once per day. Some are crashing more frequently than once per hour, and tokens have a 6hour expiry, so my suspicion is unlikely correct. I kicked the bug over to another org, but if my suspicion is wrong, this is going to be much harder to diagnose or fix.
Also, as an aside, I noticed some timeout errors. You'll want to make sure you're handling both success and error cases in your promises correctly. I don't know what the bug is since I never saw your code, but that's something to look into.
Yes- I am only trying to solve the 401 auth error at the moment. As mentioned, this used to occur once per day on the 15 minute scheduled function, but now occurs multiple times per day. Same exact function triggers on the 1 hour, 4 hour, daily, weekly- with no problem. I've been scouring through docs, examples, and source code the last few weeks- but cannot find anything actionable in regards to the 401, and/or how to refresh the admin token.
What do you mean 'my suspicion is unlikely correct'? And where in the source/docs does it say tokens expire after 6 hours?
Getting desperate at this point- will try any suggestions. I have been running these functions on a live app for just over a year, but the consistency of alerts on the 15 minute- has now become a serious issue.
I'm very sorry; apparently I forgot to hit "Comment" on this
My suspicion is unlikely correct
I mean that my theory that you have a token that's expiring while you have queueing requests doesn't seem to match the fact that the token should last for 6hrs and you're crashing more often than that. RE that the token lasts for 6hrs, I misspoke. They last 1hr (most documentation will mention expires_in: 3600.
Your function is crashing more often than 1/hr IIRC and it's being scheduled in the same set of machines, so it should share a token cache. If that's the case I don't know how the token would expire in time.
I do think that I might have found a bug in the admin SDK for credentials that are about to expire though. Can you try deploying with the following code before your own?
letnow=Date.now();lettoken=awaitadmin.app().INTERNAL.getToken();functions.logger.debug(`Before force-refresh, token would expire in ${(token.expirationTime-now)/1000} seconds`);token=awaitadmin.app().INTERNAL.getToken(/*force_refresh=*/true);functions.logger.debug(`After force-refresh, token would expire in ${(token.expirationTime-now)/1000} seconds`);
IF I'm right about there being an edge case that's not handled in the Admin SDK code, this could work around it. Can you let me know:
Does this solve your problem?
What do your debug-level logs over a 2hr period say? If we're printing that the expiration is within 119 seconds then you might have been encountering this bug.
Thank you @inlined. I have implemented your code fix on the 15m scheduled function. Will let it run for 24 hours and report back.
First logs show token expiration is 1 hour:
Before force-refresh, token would expire in 2700.534 seconds
After force-refresh, token would expire in 3599.619 seconds
@inlined I ran the 15m function with your suggested fix over the weekend. Not a single 401 auth error - this is great!
I am happy to keep this force token refresh fix in place for now. Would you consider this something that may get fixed upstream at some point, or is this more of an outlier situation?
We should definitely transfer this to the firebase-admin-sdk and get some of these bugs fixed. To help us with debugging, can you let us know how long the function typically runs?
Activity
inlined commentedon Feb 11, 2021
This issue seems to have slipped through the cracks; we're very sorry about that. Can you confirm whether the issue is still happening? It's possible that there's a bug in the token refresh code.
trentbrooks commentedon Feb 11, 2021
Hi - thanks for response, yes this is still happening for our live app. I actually forgot about it myself- but just checked the cloud functions logs, and it still occurs once or twice per day- doesn't seem to be the same fixed UTC time anymore.
For context, I'm sending out 200-300 topic notifications every 15 minutes. The majority of these send fine, but at least once per day it fails with the same 401 auth error* and does not send. When the next 15 minute comes, it goes back to normal / auth + sends fine.
Would love some advice.
inlined commentedon Feb 12, 2021
Are you using any other Firebase APIs in this function, or just FCM? About how long does your function execute before it hits the 401 error? I'm wondering if there's a token that's not getting refreshed fast enough somewhere.
Also, how are you sending these notifications? Does your code look like:
or
My suspicion in the latter case would be that the SDK to gets a cached token for those 200-300 topics synchronously and then queues the HTTP requests. Eventually a request gets a chance to run but the token has expried. The workaround would be something like:
This would still let you run ~10 requests in parallel at a time, but would keep the token freshness check happening around when the HTTP request actually gets processed by the networking stack.
trentbrooks commentedon Feb 12, 2021
I am only using the admin.messaging.sendToTopic() call in the function. Takes about 5 seconds to run (on both the error calls, and successful). And I am using the latter example with promises.
I am going to try the batch example you posted- thanks for the tip. If it is an expired token, is there a way to increase the expiration timeout (eg. google cloud console)?
inlined commentedon Feb 12, 2021
If it only takes 5s that reduces the chances that my suspicion is accurate. There's no way to increase the TTL of tokens, but that's not the right approach anyway. Any number you come up with (within reason) could be too short for a reasonable workload. From what I read in the credential section of the Admin SDK we're fetching the token every time for this type of Credential. IIRC the metadata service that provides you your credential can return credentials that expire within a few seconds. Its implementation is itself a cached credential that may expire within 1 day. That could explain your daily behavior, though the chances are slimmer if this is only a 5s request storm.
trentbrooks commentedon Feb 12, 2021
I also run another identical function on the 1H (eg. send 200-300 topic notifications every hour), and there are no problems. Both functions work exactly the same, but only the 15 minute frequency seems to throw the 401 errors.
Originally I thought the 15m was catching the errors off a cold startup, leaving the other functions to run fine. But now the error frequency is more sporadic, I'm not sure what else it could be? Would gladly test any alternative suggestions you might have?
inlined commentedon Feb 12, 2021
With your permission I'd like to look at some production metrics for your functions. Rather than posting personally identifiable information here, can you contact support with the name of your project, the function that runs every 15m & fails, and the function that runs every hour and succeeds? If you give me your case ID I can look into things further.
trentbrooks commentedon Feb 12, 2021
Thank you @inlined. I have submitted a minimal code example with the project name to support. The case ID is: 00115392
inlined commentedon Feb 16, 2021
I'm having trouble looking up the case on my side; there's lots of privacy protection measures at Google that are blocking me from searching for your case. Mind telling them to assign the bug to "inlined"?
trentbrooks commentedon Feb 16, 2021
Done - thanks again @inlined
trentbrooks commentedon Feb 27, 2021
Hey @inlined, not much activity on this yet so thought i'd ask if there was a temporary fix I could apply to circumvent this error? eg. can i check the admin auth status before sending messages and reconnect if required?
inlined commentedon Mar 1, 2021
I'm bypassing some of the communication channels by responding both internally and externally, but I found out that your functions are crashing much more than once per day. Some are crashing more frequently than once per hour, and tokens have a 6hour expiry, so my suspicion is unlikely correct. I kicked the bug over to another org, but if my suspicion is wrong, this is going to be much harder to diagnose or fix.
Also, as an aside, I noticed some timeout errors. You'll want to make sure you're handling both success and error cases in your promises correctly. I don't know what the bug is since I never saw your code, but that's something to look into.
trentbrooks commentedon Mar 1, 2021
Yes- I am only trying to solve the 401 auth error at the moment. As mentioned, this used to occur once per day on the 15 minute scheduled function, but now occurs multiple times per day. Same exact function triggers on the 1 hour, 4 hour, daily, weekly- with no problem. I've been scouring through docs, examples, and source code the last few weeks- but cannot find anything actionable in regards to the 401, and/or how to refresh the admin token.
What do you mean 'my suspicion is unlikely correct'? And where in the source/docs does it say tokens expire after 6 hours?
Getting desperate at this point- will try any suggestions. I have been running these functions on a live app for just over a year, but the consistency of alerts on the 15 minute- has now become a serious issue.
inlined commentedon Mar 5, 2021
I'm very sorry; apparently I forgot to hit "Comment" on this
I mean that my theory that you have a token that's expiring while you have queueing requests doesn't seem to match the fact that the token should last for 6hrs and you're crashing more often than that. RE that the token lasts for 6hrs, I misspoke. They last 1hr (most documentation will mention
expires_in: 3600.Your function is crashing more often than 1/hr IIRC and it's being scheduled in the same set of machines, so it should share a token cache. If that's the case I don't know how the token would expire in time.
I do think that I might have found a bug in the admin SDK for credentials that are about to expire though. Can you try deploying with the following code before your own?
IF I'm right about there being an edge case that's not handled in the Admin SDK code, this could work around it. Can you let me know:
trentbrooks commentedon Mar 5, 2021
Thank you @inlined. I have implemented your code fix on the 15m scheduled function. Will let it run for 24 hours and report back.
First logs show token expiration is 1 hour:
Before force-refresh, token would expire in 2700.534 seconds
After force-refresh, token would expire in 3599.619 seconds
trentbrooks commentedon Mar 7, 2021
@inlined I ran the 15m function with your suggested fix over the weekend. Not a single 401 auth error - this is great!
I am happy to keep this force token refresh fix in place for now. Would you consider this something that may get fixed upstream at some point, or is this more of an outlier situation?
inlined commentedon Mar 10, 2021
We should definitely transfer this to the firebase-admin-sdk and get some of these bugs fixed. To help us with debugging, can you let us know how long the function typically runs?