One time, I experienced an exception being thrown when trying to set a secure value. This value was an updated refresh/oauth token which only gets updated occasionally, and it happened on an iOS simulator when I deployed an updated version of the app to the simulator after it sat idle overnight. Additionally I had done a full git clean git clean -xdf and rebuild after switching branches.
I'm unclear if this is a normal use case that I need to handle or if it is just a "Development Artifact" that I can safely ignore for production... right now I'm leaning toward it might be a rare scenario I need to handle... possibly during app updates?
I don't have reliable steps to reproduce unfortunately, nor a minimum reproduction sample. If it happens again I'll try to make one. The rough steps are
git clean -xdfSecure storage continues to work
Exception is thrown:
System.Exception: Error adding record: DuplicateItem
at Xamarin.Essentials.KeyChain.SetValueForKey (System.String value, System.String key, System.String service) [0x00071] in <80b8ae4e16374c93b74637a8a9a92836>:0
at Xamarin.Essentials.SecureStorage.SetAsync (System.String key, System.String value, Security.SecAccessible accessible) [0x00027] in <80b8ae4e16374c93b74637a8a9a92836>:0
at Xamarin.Essentials.SecureStorage.PlatformSetAsync (System.String key, System.String data) [0x00007] in <80b8ae4e16374c93b74637a8a9a92836>:0
at Xamarin.Essentials.SecureStorage.SetAsync (System.String key, System.String value) [0x00021] in <80b8ae4e16374c93b74637a8a9a92836>:0
at App.MyApp.Services.LocalStorageServiceBase.set_IdToken (System.String value) [0x0001a] in /Users/kensykora/Workspace/App.myapp/src/App.MyApp/Services/LocalStorageServiceBase.cs:178
at App.MyApp.Services.Login.LoginService+<RefreshCurrentLogin>d__16.MoveNext () [0x0020d] in /Users/kensykora/Workspace/App.myapp/src/App.MyApp/Services/Login/LoginService.cs:123
n/a
LMK if you need one, I can try and spend some time and find more detail here.
I'm not sure I understand the git clean part, that seems like it is your code... It seems like something else though. We can detect this and remove it and then re-add it. Have you tried to do that?
Looking at the code we check to see if it exists and if so then we remove and re-add it. Maybe this is a threading issue? hmmmm
I鈥檝e added tracking to AppCenter for my app for this issue, but I appreciate the pull request #705 to try and address it. I鈥檒l update when it鈥檚 released and see if it continues to pop up.
Yeah, you could search the error message and then remove and re-add it probably, but it is very strange that it didn't remove it because we check that...
Hmmm, I鈥檒l check for a race condition. Since it鈥檚 an oauth token we were trying to refresh, it鈥檚 entirely possible 2 API calls were trying to happen at once.
I鈥檓 guessing the storage methods for encryption aren鈥檛 designed with thread safety?
Hello, we have the latest version of xamarin.essentials (1.5.1) and we still getting this error
System.Exception: Error adding record: DuplicateItem at Xamarin.Essentials.KeyChain.SetValueForKey (System.String value, System.String key, System.String service)
Were you able to resolve this issue?
Same here using Essentials 1.5.3.2. Error does not always occur. Last occurrence was on iPad 6th gen with iOS 13.4.1.
Adding SecureStorage.RemoveAll() before calling SetAsync() to see if it will help.
Same here using Essentials 1.5.3.2. Error does not always occur. Last occurrence was on iPad 6th gen with iOS 13.4.1.
Adding SecureStorage.RemoveAll() before calling SetAsync() to see if it will help.
Did it help? We get this a lot. Our AppCenter logs are full of KeyChain.SetValueForKey exceptions
It appears to have solved the issue for us, but it's a new app with few users so it's too soon to tell for sure.
Most helpful comment
Hello, we have the latest version of xamarin.essentials (1.5.1) and we still getting this error
System.Exception: Error adding record: DuplicateItem at Xamarin.Essentials.KeyChain.SetValueForKey (System.String value, System.String key, System.String service)
Were you able to resolve this issue?