App crashes in production – NSInvalidArgumentException (NaN in JSON write)
Description
In iOS the app often crashes when I try to save something in the Amplify DataStore. I couldn't find a reliable pattern to reproduce the issue, but I see the following error logged in Sentry:
NSInvalidArgumentException InternalFlutterGpu_Texture_AsImage - Invalid number value (NaN) in JSON write
I could not reproduce this problem in debug mode—it seems only happen in release builds and makes my app unusable. Howerver I'm sure that it has to do with the amplify library because if I remove it from the app, no crashes appear. In the following the crashlog (crashlog.txt):
Last Exception Backtrace:
0 CoreFoundation 0x1a7d9cf20 __exceptionPreprocess + 164 (NSException.m:249)
1 libobjc.A.dylib 0x19fc4b2b8 objc_exception_throw + 60 (objc-exception.mm:356)
2 Foundation 0x1a6c043e4 _writeJSONNumber + 1084 (NSJSONSerialization.m:0)
3 Foundation 0x1a6c02f68 ___writeJSONObject_block_invoke + 388 (NSJSONSerialization.m:588)
4 libswiftCore.dylib 0x1a6988bac specialized _SwiftDeferredNSDictionary.enumerateKeysAndObjects(options:using:) + 652 (DictionaryBridging.swift:365)
5 libswiftCore.dylib 0x1a6704ae0 @objc _SwiftDeferredNSDictionary.enumerateKeysAndObjects(options:using:) + 48 (<compiler-generated>:349)
6 Foundation 0x1a6c03c08 _writeJSONObject + 516 (NSJSONSerialization.m:680)
7 Foundation 0x1a6be5288 -[_NSJSONWriter dataWithRootObject:options:] + 96 (NSJSONSerialization.m:385)
8 Foundation 0x1a6be5114 +[NSJSONSerialization dataWithJSONObject:options:error:] + 112 (NSJSONSerialization.m:3204)
9 *****Prod 0x100dbcfa0 static FlutterDataStoreRequestUtils.getJSONValue(_:) + 88 (FlutterDataStoreRequestUtils.swift:8)
10 *****Prod 0x100dbcfa0 specialized SwiftAmplifyDataStorePlugin.onSave(args:flutterResult:) + 2024 (SwiftAmplifyDataStorePlugin.swift:452)
11 *****Prod 0x100daf5ec specialized SwiftAmplifyDataStorePlugin.onSave(args:flutterResult:) + 20 (AtomicResult.swift:224)
12 *****Prod 0x100daf5ec implicit closure #1 in AtomicResult(_:_:) + 20 (AtomicResult.swift:11)
13 *****Prod 0x100daf5ec AtomicResult(_:_:) + 20 (<compiler-generated>:0)
14 *****Prod 0x100daf5ec SwiftAmplifyDataStorePlugin.handle(_:result:) + 1988
15 *****Prod 0x100dafa88 @objc SwiftAmplifyDataStorePlugin.handle(_:result:) + 108 (<compiler-generated>:0)
16 Flutter 0x103a620e8 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 164 (FlutterChannels.mm:313)
17 Flutter 0x1034e6be0 invocation function for block in flutter::PlatformMessageHandlerIos::HandlePlatformMessage(std::_fl::unique_ptr<flutter::PlatformMessage, std::_fl::default_delete<flutter::PlatformMessage>>) + 116 (platform_message_handler_ios.mm:70)
18 libdispatch.dylib 0x1afc4113c _dispatch_call_block_and_release + 32 (init.c:1530)
19 libdispatch.dylib 0x1afc42dd4 _dispatch_client_callout + 20 (object.m:576)
20 libdispatch.dylib 0x1afc515a4 _dispatch_main_queue_drain + 988 (queue.c:7898)
21 libdispatch.dylib 0x1afc511b8 _dispatch_main_queue_callback_4CF + 44 (queue.c:8058)
22 CoreFoundation 0x1a7d6f710 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1780)
23 CoreFoundation 0x1a7d6c914 __CFRunLoopRun + 1996 (CFRunLoop.c:3149)
24 CoreFoundation 0x1a7d6bcd8 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420)
25 GraphicsServices 0x1ec7b91a8 GSEventRunModal + 164 (GSEvent.c:2196)
26 UIKitCore 0x1aa3a5ae8 -[UIApplication _run] + 888 (UIApplication.m:3713)
27 UIKitCore 0x1aa459d98 UIApplicationMain + 340 (UIApplication.m:5303)
28 UIKitCore 0x1aa5d3504 UIApplicationMain(_:_:_:_:) + 104 (UIKit.swift:539)
29 *****Prod 0x1009cc728 specialized static UIApplicationDelegate.main() + 28 (<compiler-generated>:4)
30 *****Prod 0x1009cc728 static AppDelegate.$main() + 28 (AppDelegate.swift:0)
31 *****Prod 0x1009cc728 main + 120
32 dyld 0x1cb543154 start + 2356 (dyldMain.cpp:1298)
Categories
- [ ] Analytics
- [ ] API (REST)
- [ ] API (GraphQL)
- [ ] Auth
- [ ] Authenticator
- [x] DataStore
- [ ] Notifications (Push)
- [ ] Storage
Steps to Reproduce
I cannot find a pattern that reproduce this problem but it appears whenever the app saves elements in the datastore with a high frequency
Screenshots
No response
Platforms
- [x] iOS
- [ ] Android
- [ ] Web
- [ ] macOS
- [ ] Windows
- [ ] Linux
Flutter Version
3.27.2
Amplify Flutter Version
2.6.0
Deployment Method
Amplify CLI (Gen 1)
Schema
Hello @giulitu95, thanks for taking the time to open this issue. Could you please provide a sample schema and code snippet to help us reproduce this issue.
I have identified the cause of the crash.
One of my models includes a Float attribute, which is translated into a double in the generated Flutter model. The crash occurs when attempting to save a double.nan value to the DataStore.
Here are the exact steps that lead to the crash:
In Flutter, I call Amplify.DataStore.save(myModel), where myModel includes a parameter set to double.nan.
During the .save call, the model is serialized to JSON. At this point, the JSON becomes invalid because it contains a key-value pair like:
"myModelDoubleParameter": nan
This invalid JSON is then passed through the method channel to the native iOS plugin. Specifically, the Swift onSave method is triggered, which attempts to deserialize the JSON string using the following code:
https://github.com/aws-amplify/amplify-flutter/blob/cc36ecc9f246eff99d90963cec50a39fd66ea3c0/packages/amplify_datastore/ios/Classes/SwiftAmplifyDataStorePlugin.swift#L168-L170
The app crashes at this point because the getJSONValue(...) function cannot handle the nan value during deserialization.
The nan value was actually not expected and was due to a bug of the statistics package. I also signaled the problem in a dedicated issue for that package.
Hello @giulitu95, thank you for the update. Since gmpassos has provided a fix for the statistics package and Amplify DataStore is providing the expected error message, I will close this issue unless you have any additional questions.
Thank you @tyllark for your answer, I don't think that the crash or the freeze are expected behaviors of this library.
In flutter a double value could be nan but the library does not handle this value causing a crash of the app. Furthermore the library does not provide any evident log, the message NSInvalidrgumentException has been discovered only digging in the crashlog and analyzing the sentry logs.
Hello @giulitu95, you are correct. I will keep this issue open as a bug to create validate our models via an assert or exception if an unsupported values such as double.nan,double.infinity, or double.negativeInfinity is provided.
This validation will need to be included in our model generation, so we will need to coordinate with the Amplify CLI team to implement this fix.