Skip to content

App crashes in production – NSInvalidArgumentException (NaN in JSON write) #5891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 14 tasks
giulitu95 opened this issue Feb 3, 2025 · 5 comments
Open
2 of 14 tasks
Assignees
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category requires-cli-fix Issues that require a CLI fix to resolve the root cause of issue.

Comments

@giulitu95
Copy link

giulitu95 commented Feb 3, 2025

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
  • 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

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.27.2

Amplify Flutter Version

2.6.0

Deployment Method

Amplify CLI (Gen 1)

Schema

@github-actions github-actions bot added pending-triage This issue is in the backlog of issues to triage pending-maintainer-response Pending response from a maintainer of this repository labels Feb 3, 2025
@tyllark
Copy link
Member

tyllark commented Feb 4, 2025

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.

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Feb 4, 2025
@tyllark tyllark added the pending-community-response Pending response from the issue opener or other community members label Feb 5, 2025
@tyllark tyllark self-assigned this Feb 10, 2025
@ekjotmultani ekjotmultani added datastore Issues related to the DataStore Category question A question about the Amplify Flutter libraries labels Feb 12, 2025
@github-actions github-actions bot removed the pending-triage This issue is in the backlog of issues to triage label Feb 12, 2025
@giulitu95
Copy link
Author

giulitu95 commented Apr 7, 2025

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:

let serializedModel = FlutterSerializedModel(id: modelID, map: try FlutterDataStoreRequestUtils.getJSONValue(serializedModelData))

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.

@github-actions github-actions bot added pending-maintainer-response Pending response from a maintainer of this repository and removed pending-community-response Pending response from the issue opener or other community members labels Apr 7, 2025
@ekjotmultani ekjotmultani removed the pending-maintainer-response Pending response from a maintainer of this repository label Apr 14, 2025
@tyllark
Copy link
Member

tyllark commented Apr 14, 2025

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.

@tyllark tyllark added the pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. label Apr 14, 2025
@giulitu95
Copy link
Author

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.

@github-actions github-actions bot added the pending-maintainer-response Pending response from a maintainer of this repository label Apr 15, 2025
@tyllark
Copy link
Member

tyllark commented Apr 16, 2025

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.

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Apr 16, 2025
@tyllark tyllark added bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category requires-cli-fix Issues that require a CLI fix to resolve the root cause of issue. and removed question A question about the Amplify Flutter libraries pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. datastore Issues related to the DataStore Category labels Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category requires-cli-fix Issues that require a CLI fix to resolve the root cause of issue.
Projects
None yet
Development

No branches or pull requests

3 participants