Page MenuHomePhabricator

Transform C++ error to JSError on the main thread
ClosedPublic

Authored by marcin on Aug 29 2022, 4:43 AM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Dec 17, 1:50 AM
Unknown Object (File)
Sat, Dec 14, 5:30 AM
Unknown Object (File)
Sat, Dec 14, 5:30 AM
Unknown Object (File)
Sat, Dec 14, 5:30 AM
Unknown Object (File)
Sat, Dec 14, 5:30 AM
Unknown Object (File)
Sat, Dec 14, 5:30 AM
Unknown Object (File)
Sat, Dec 14, 5:29 AM
Unknown Object (File)
Sat, Dec 14, 5:24 AM
Subscribers

Details

Summary

In https://phab.comm.dev/D4650 we enabled informative JSError to be thrown when SQLite query fails on databaseThread of CommCoreModule. I tested it only on emulators and it worked. However on physical devices I found out that I cannot access jsi::Runtime passed to function from auxiliary thread. Context is here https://github.com/react-native-community/discussions-and-proposals/issues/196. This diff updates the code so that we only catch C++ error in database thread and transform it to JSError on the main thread. This change also solves the crash described here: https://linear.app/comm/issue/ENG-1714/ashoat-experienced-5-crashes-in-one-day-on-build-142

Test Plan

Place temporary 'throw std::system_error(ECANCELED, std::generic_category(), "error");' at the begining of SQLiteWueryExecutor::getAllMessages. Build and start the app. Without this diff it will crash. With this diff it will throw informative JSError.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Harbormaster returned this revision to the author for changes because remote builds failed.Aug 29 2022, 4:47 AM
Harbormaster failed remote builds in B11671: Diff 16047!
tomek requested changes to this revision.Aug 30 2022, 3:57 AM
tomek added inline comments.
native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
168–173 ↗(On Diff #16048)

It seems like we have a similar approach in both places. Maybe we can create a function that takes a function or lambda and returns a generic T (so it probably should be a pointer to a function that takes no arguments and returns T)? We could use this util in both places.

This revision now requires changes to proceed.Aug 30 2022, 3:57 AM
native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
168–173 ↗(On Diff #16048)

Or instead of pointer, we could use std::function

native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
168–173 ↗(On Diff #16048)

In my honest opinion I would be an overkill to extract those try{}catch{}into functions. The logic here is not that complex, we are just storing error in a promise. Also if you look at the code of CommCoreModule.cpp you can see that there are multiple examples of try{}catch{} statements that look almost exactly the same - they differ only by the function executed inside try{}. In order to be consistent we would need to create such a function for every try{}catch{} pattern that is repeated at least twice since there is not reason to specially treat those two.

Extract common logic into generic function

This revision is now accepted and ready to land.Aug 30 2022, 9:07 AM

Great work, this looks really clean!!

Initially we created this revision to do some code review before making the changes above part of the release build. Can I assume that we can land it now?

Defer to you and @tomek, but it looks good to me. Can't see why we wouldn't land it

Retrigger Ci before landing