Open alex-breen opened 7 months ago
Hello, @alex-breen 👋 and sorry to hear you're running into this. It sounds like this could possibly be related to when DataStore is trying to re-establish the connection, but the tokens for a given user are potentially expired. Can you look into what you've configured for the expiration timeframe for your refresh and access tokens to? Does this happen in a production version of the app as well or just locally when in dev mode?
Hi @cwomack -
My cognito settings are:
Refresh token expiration: 30 day(s) Access token expiration: 1 hour(s) ID token expiration: 1 hour(s)
Does this happen in prod? Yes, I'm pretty sure at least. I'm sure I've seen the browser log errors, and pretty sure I've seen the red error overlay over the screen, but I'll watch for that to be sure.
<<Update: Happens in prod, but overlay with red messages doesn't appear on browser in prod, only console errors. In dev, overlay with red errors appears.>>
Update: On production, I've encountered the issue twice (about every 24 hours). The console logs show the errors:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'cognito:username')
ConsoleLogger.ts:85 [ERROR] 00:45.178 DataStore - Sync processor retry error: Error: No current user
But the browser screen doesn't get the overlay with red error messages. On the dev build, it does.
Another update (reproduction is slow because it seems to occur after several hours of sleep or inactivity):
@cwomack is there something I can do to catch this when it happens and restart Datastore?
I have Hub logs with today's failed sync. I hope this is helpful. .Steps to repro: wake up from sleep. What I see is that the Hub says the model is sync'd. However, it is not processing inbound updates from the backend. However, it is able to process outbound mutations.
Do you recommend I call Datastore.start()
after the Hub event "ready"? My aim is to reset the sync process for cases in which it thinks it is syncing, but it is not. Will this kickstart it up again?
Note, I can't call Datastore.stop()
and then Datastore.start()
as that often results in an uncaught error because there are other observe queries in flight.
@alex-breen As a potential path to a workaround while the repro of this is pending, do any of the auth events indicate the need for a Datastore restart?
https://docs.amplify.aws/react/build-a-backend/auth/auth-events/#listen-to-and-log-auth-events
@svidgen - I don't think so, as no error events are passed. Datastore events indicate everything is working (syncQueriesReady) and Auth is fine.
@cwomack - I've tried cycling a stop and start of Datastore
every time there is a Hub
networkStatus
event. But the sync doesn't get restablished. The websocket connection (eg: wss://abc.appsync-realtime-api.us-east-1.amazonaws.com/graphql?header...) stays unresponsive. The Hub events imply success (proceeds to ready
) and outbound mutations work, but inbound sync updates are not received.
To reproduce (artificially):
accessToken
(remove one letter)Network
to Offline
accessToken
back to its original value. Network
to No Throttling
Sync processor retry error
in Chrome devtools console.Hub
event for ready
appears (if you are logging `Hub events). Otherwise just wait a few seconds. Hi @alex-breen, thank you for the repro steps. Unfortunately, I have not been able to reproduce this issue with the provided steps.
These were the events logged from the Hub during my reproduction. In my case I might've run into a different issue - I never got another ready
event after the first one.
I also observed that updating a record from another browser instance was reflected.
The highlighted record was updated from a second, incognito window.
I am only able to get the Sync process retry error
if I force it by attempting to start the sync process with an invalid access token (calling DataStore.start()
)
Are you sure that those steps are in the correct order?
Hi @chrisbonifacio - I've jotted down a few things that might help shed some light:
ready
event does not follow syncQueriesReady
after a networkStatus
event. However, I haven't seen any issues with syncing in that case.accessToken
value in localstorage
. But I'm only guessing that is addressing the same root cause.stop
.networkStatus
event, I 1) call DataStore.stop()
2) Wait 10 seconds 3) call fetchAuthSession({ forceRefresh: true })
4) call DataStore.start()
. Sometimes the first fetchAuthSession
fails, so I have one retry after 10 seconds. @chrisbonifacio - by any chance is there an update on this?
Even with my hacky workaround, I've seen Safari fail (silently), leading to data loss on an add request (amplify js doesn't throw an error, but sync fails to write, but app doesn't know this, so data is lost). At the very least, just a way to listen for when sync is broken so that I can force a reload would help.
@chrisbonifacio @cwomack - I've reproduced this with a very simple app that mostly uses the example code in Amplify documentation.
Behavior: Wake up computer after sleep. The last refresh of the webpage should be over 12 hours ago. Update data from a separate computer (or incognito browser). Result - The update does not appear on the primary browser. Data is now out of sync. Hub events report "syncQueriesReady" but websocket messages are not received.
I've attached screenshots of the console and network errors that appear right after wake up. I've also attached the relevant code. It's important that authentication is enabled and model schema has @auth(rules: [{allow: owner}])
. I tested this running in build/vite-preview mode, locally (but I've reproduced it many, many times in production and in dev mode.) I don't have a direct way to trigger the error, other than load the page in the morning, put your computer to sleep at night, and wake up your computer the next morning. Also, if you refresh the page, the updated data syncs as the websocket is restored. What makes this issue particularly difficult is the Hub events indicate everything is working and sync'd, when it is not..
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
Authentication, DataStore
Amplify Version
v6
Amplify Categories
auth, api
Backend
None
Environment information
Describe the bug
When waking up computer from sleep state, website throws a series of errors (uncaught, over website screen).
Everything resolves by itself but the user sees the browser error modal and has to close it. I don't have a clear way to reproduce this, but my hunch is that it occurs after the 24 hour "base query" sync from Datastore. I could be seeing a pattern that is not real, but it does seem to occur about once a day, after there has been some idle time (like computer at sleep). I pretty sure this started appearing after I upgraded to v6.
Here is the screenshot of from the browser's console logs.
Here is the screen shot from the browser's network logs:
Browser window screenshot with errors:
Expected behavior
no errors
Reproduction steps
Wake up computer from sleep.
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response