Closed alex-breen closed 11 months ago
Hi @alex-breen 👋 thanks for opening this issue. Sounds like this issue might be difficult to find the right conditions to reproduce. Is there a particular reason why you clear data on signin rather than signout?
Also, when DataStore hangs on clearing, are you able to capture any logs? That might give some insight into why it's hanging.
Hi @chrisbonifacio. I clear on both sign-in and sign-out. I do that in part because I interpreted the docs as saying that was recommended. Also, I assumed that if Amplify's Authentication component forces a sign-out due to tokenRefresh_failure, my code might not be able to reliably catch the event and invoke DataStore.clear(). Or other cases that I can't think of that might result in a signed-out state but with data still present in the indexedDb.
BTW, I've also run into problems invoking DataStore.clear() while in a signed-in state where it sometimes throws the error: DataStore State Error: Tried to execute
DataStore.observe()while DataStore was "Clearing"
. I'm pretty sure this was triggered when DataStore had a sync in progress while 'clear' was invoked. My solution is to a) check if Hub's sync status is 'ready' b) if not ready then I retry every 500ms. I'm not proud of that solution, but I couldn't think of anything better - if you can suggest a better solution, I'm all ears.
Regarding more logs, what should I do to pull or generate more verbose logs? I have the events from the Hub listener for Auth and DataStore (which I attached). My hunch is this is typically triggered when there is a tokenRefresh_failure.
This is related, but might be worth a separate issue.
Condition:
await DataStore.clear()
Error is thrown:
datastore.ts:1484 Uncaught Error: DataStoreStateError: Tried to execute
DataStore.observeQuery()while DataStore was "Clearing". This can only be done while DataStore is "Started" or "Stopped". To remedy: Ensure all calls to
stop()and
clear()have completed first. If this is not possible, retry the operation until it succeeds. at handler (datastore.ts:1484:1)
If I add a 1000ms pause between the "sign-in" event and invoking clear, everything works fine. Note, Hub listener does not report any DataStore events during this phase.
Thank you for the reproduction steps, @alex-breen! We will look into this. In the meantime, I think performing a retry and/or delay is fine until we implement a permanent solution.
Thanks @chrisbonifacio. Based on a few tests, if I remove the Hub
listener for DataStore
events, the error I was seeing after entering the sign-in confirmation code no longer appears.
Error:
datastore.ts:1484 Uncaught Error: DataStoreStateError: Tried to execute DataStore.observeQuery()while DataStore was "Clearing". This can only be done while DataStore is "Started" or "Stopped". To remedy: Ensure all calls tostop()andclear() have completed first. If this is not possible, retry the operation until it succeeds. at handler (datastore.ts:1484:1)
Listener code (which I've now removed):
useEffect(() => {
// Create listener for datastore sync events
const listener = Hub.listen('datastore', async hubData => {
const { event, data } = hubData.payload;
console.log('Hub listener Datastore event: ' + event)
})
// Remove listener
return () => { listener() }
}, [])
I have noticed that if you have multiple tabs open, this causes DataStore.clear() to fail to terminate its processes because the other tab is where DataStore is started or working with the data. I close others tabs and works pretty well, just one tab opened. Working with DataStore is clearly a pain in the head.
I follow this post as well for clearing properly via promise https://stackoverflow.com/questions/74314691/aws-amplify-datastorestateerror-tried-to-execute-datastore-query-while-datast
If anyone is experiencing similar issues with DataStore.clear
please refer to @crpozo's comment regarding multiple tab behaviors as well as this comment describing how to ensure that clear has finished resolving before querying.
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
Authentication, GraphQL API, DataStore, Storage
Amplify Categories
auth, storage, function, api, hosting
Environment information
Describe the bug
About 10% of the time DataStore hangs when executing
DataStore.clear
.After a user logs in, I invoke
DataStore.clear
beforeDataStore.start
, as recommended in the Amplify docs. Clear is invoked after thesign-in
event is received.Note, I have tried invoking "DataStore.start" before clear, but that usually triggers an ugly unhandled promise error on login.
Expected behavior
DataStore quickly clears.
Reproduction steps
It's possible that this typically occurs after the user has been signed out automatically due to inactivity. E.g., waking up the next day.
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