realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.74k stars 565 forks source link

Realm.create Crashes Android Emulator with a SIGABRT fatal error 6 when called from inside Redux Thunk #3968

Closed jimfoambox closed 3 years ago

jimfoambox commented 3 years ago

How frequently does the bug occur?

All the time

Description

I have been attempting to set up an environment when upon a User Logging into a React Native App, a Redux thunk makes an API call that authenticates the user, and then the local user instance is either newly created or instantiated from the local Realm Database. Other thunks populate this Realm local database with relevant info when their API calls are made. So far my set up works 100% fine in IOS simulator, and the realm.default file for IOS reflects all the information as desired from my schema and calls, etc. With Android, however, the app silently crashes 100% of the time, with no relevant error messages in Metro, but a SIGABRT Fatal Error 6 message in Android Studio and Flipper.

Calling other Realm functions in the thunk, like deleteAll(), causes no issues. It is only when realm.create() is invoked.

In addition, realm.create invoked in other parts of the App, like within React components, causes no issues and behaves as expected.

Here is the Thunk :

export const fetchUser = (params: userLoginParams) => {
  return async (dispatch: Dispatch<AppActions>): Promise<any> => {
    try {
      const response = await axios.post(
        `${BASE_URL}amf/gateway/`,
        params, 
        axiosConfig,
      )

      //Check and log results of login attempt(s), append Bugfender user info, create local Realm instance, and if successful navigate user to home page.
      if (response.data.status === 'Authenticated') {
        RootNavigation.navigate('LoggedInWrapper', { screen: 'Screens', params: { screen: 'Home' }});
        setBugFenderDetails(params.parameters[0], 'email', true);
        logger.log('LOG IN ATTEMPT SUCCESSFUL');
        //create new Realm User local database instance.
        realm.write(()=> {
          realm.create(
            "User", 
            {
              _id: Number(response.data.id),
            },
            UpdateMode.Modified
          )
        });
        //execute logged in server request sequence.
        const loginNetworkQueue = new LoginActivityInitializer;
        if (!newLoginCounter.counter) {
          loginNetworkQueue.executeRequests(newLoginCounter.counter);
          loginNetworkQueue.executeBatchRequests(newLoginCounter.counter);
          newLoginCounter.add(1);
        }
      } else {
        logger.log(`LOG IN ATTEMPT FAILED: ${response.data.status}`);
      }

      return dispatch(sendUserToReducer(response.data));

    } catch (error: any) {
      logger.error('LOG IN ATTEMPT ERROR',error);
      if (error.message === 'Network Error') {
        return dispatch(sendUserToReducer({ id: 'null', token: 'null', pic: 'null', status: 'Network Error' }));
      }
    }
  };
};Here is my schema:
export default class User {

  public id!: number;

  public userLogin?: Realm.Dictionary<UserLogin>;
  public userConversations?: Realm.Dictionary<UserConversations>;
  public userPreferences?: Realm.Dictionary<UserPreferences>;
  public userModel?: Realm.Dictionary<UserModel>;
  public userTasks?: Realm.Dictionary<UserTasks>;
  public openWorkOrders?: Realm.Dictionary<OpenWorkOrders>;
  public completedWorkOrders?: Realm.Dictionary<CompletedWorkOrders>;
  public maintenanceStatus?: Realm.Dictionary<MaintenanceStatus>;
  public generalSystemInfo?: Realm.Dictionary<GeneralSystemInfo>;
  public forgotPassword?: Realm.Dictionary<ForgotPassword>;

  public static schema: Realm.ObjectSchema = {
    name: "User",
    primaryKey: "_id",
    properties: {
      _id: "int",

      userPreferences: "UserPreferences?",
      userModel: "UserModel?",
      userLogin: "UserLogin?",
      userTasks: "UserTasks?",
      userConversations: "UserConversations?",
      openWorkOrders: "OpenWorkOrders?",
      completedWorkOrders: "CompletedWorkOrders?",
      maintenanceStatus: "MaintenanceStatus?",
      generalSystemInfo: "GeneralSystemInfo?",
      forgotPassword: "ForgotPassword?",
    }
  }
};

which is then exported in a new instance via an index file (which is what is imported into the thunk):

export default new Realm({schema: 
  [
    User, 
    CompletedWorkOrders, 
    ForgotPassword, 
    GeneralSystemInfo, 
    MaintenanceStatus, 
    OpenWorkOrders, 
    UserConversations, 
    UserLogin, 
    UserModel, 
    UserPreferences, 
    UserTasks,
    SingleOpenWorkOrder,
    UserMenuArray,
    UserModelFirstKey,
    WorkOrderStatus,
    Message,
    StatusList,
    OrderList,
    GeneralTags,
    UserList,
    Properties,
    SingleProperty,
    UnreadMessages,
    Circles,
    MessageFollow,
    ConversationTasks,
    SenderDetails,
    UserDetails,
  ],
  deleteRealmIfMigrationNeeded: true,  //comment out when schema building done
});

Hermes has been disabled in both IOS and Android.

Because the stack trace from Flipper is so huge, I thought it might be easier to read from these screenshots:

I am completely new to Realm, about 6 months experience in React Native. Thanks in advance for any help, I realize this is a lot of code and info, and had I not been banging my head against the wall for the last 2 days without finding anything else online that worked I wouldn't have resorted to opening a new issue.

Stacktrace & log output

Fatal signal 6 (SIGABRT), code -6 in tid 3972 (mqt_js)

Cannot figure out the cause

Fatal signal 6 (SIGABRT), code -6 in tid 3972 (mqt_js)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'google/sdk_gphone_x86/generic_x86:8.0.0/OSR1.180418.026/6741039:userdebug/dev-keys'
Revision: '0'
ABI: 'x86'
pid: 3920, tid: 3972, name: mqt_js  >>> com.reactmobile <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
    eax 00000000  ebx 00000f50  ecx 00000f84  edx 00000006
    esi 00000f84  edi 8dbcf028
    xcs 00000073  xds 0000007b  xes 0000007b  xfs 0000003b  xss 0000007b
    eip b34d2ac4  ebp 8dbcf048  esp 8dbcefdc  flags 00000296

backtrace:
    #00 pc 00000ac4  [vdso:b34d2000] (__kernel_vsyscall+16)
    #01 pc 00075b3c  /system/lib/libc.so (tgkill+28)
    #02 pc 0001f04e  /system/lib/libc.so (abort+110)
    #03 pc 0005c9b6  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (_Z16WTFCrashWithInfoiPKcS0_i+38)
    #04 pc 002eb15d  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #05 pc 000819d9  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #06 pc 00455f1b  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #07 pc 0045648f  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #08 pc 0013a533  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #09 pc 002a0457  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #10 pc 00394b7d  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #11 pc 00394f6a  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #12 pc 000099d9  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #13 pc 0013c6df  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/librealm.so
    #14 pc 0013bf43  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/librealm.so
    #15 pc 00179538  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/librealm.so
    #16 pc 0016feb8  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/librealm.so
    #17 pc 00055781  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (_ZN3JSC19APICallbackFunction4callINS_18JSCallbackFunctionEEExPNS_9ExecStateE+401)
    #18 pc 001540b9  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #19 pc 00168af2  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #20 pc 0013a5ec  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #21 pc 002a05bb  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #22 pc 00005d5d  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #23 pc 0017a314  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/librealm.so
    #24 pc 00170128  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/librealm.so
    #25 pc 00055781  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (_ZN3JSC19APICallbackFunction4callINS_18JSCallbackFunctionEEExPNS_9ExecStateE+401)
    #26 pc 001540b9  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #27 pc 00168af2  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #28 pc 0013a5ec  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #29 pc 002a0457  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #30 pc 00310875  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #31 pc 00153f88  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #32 pc 00168af2  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #33 pc 0013a5ec  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #34 pc 002a0457  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #35 pc 00310875  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #36 pc 00140bfd  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #37 pc 00168b42  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #38 pc 0013a63a  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #39 pc 002a05bb  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #40 pc 00005d5d  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjsc.so (offset 0x63000)
    #41 pc 0002963a  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so (_ZN8facebook3jsc10JSCRuntime4callERKNS_3jsi8FunctionERKNS2_5ValueEPS7_j+330)
    #42 pc 00021f43  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so (_ZNK8facebook3jsi8Function4callIJRKNSt6__ndk112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESB_NS0_5ValueEEEESC_RNS0_7RuntimeEDpOT_+227)
    #43 pc 00021d9b  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so
    #44 pc 0001a197  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so
    #45 pc 0001a472  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so
    #46 pc 0001a39a  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so
    #47 pc 0001dc57  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libjscexecutor.so (_ZN8facebook5react11JSIExecutor12callFunctionERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEESA_RKN5folly7dynamicE+1463)
    #48 pc 000a5e74  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libreactnativejni.so
    #49 pc 000a78aa  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libreactnativejni.so
    #50 pc 0006614f  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libreactnativejni.so
    #51 pc 000545e2  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libreactnativejni.so (_ZN8facebook3jni6detail13MethodWrapperIMNS_5react15JNativeRunnableEFvvEXadL_ZNS4_3runEvEES4_vJEE8dispatchENS0_9alias_refIPNS1_8JTypeForINS0_11HybridClassIS4_NS3_8RunnableEE8JavaPartESB_vE11_javaobjectEEE+50)
    #52 pc 00054548  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libreactnativejni.so (_ZN8facebook3jni6detail15FunctionWrapperIPFvNS0_9alias_refIPNS1_8JTypeForINS0_11HybridClassINS_5react15JNativeRunnableENS6_8RunnableEE8JavaPartES8_vE11_javaobjectEEEEXadL_ZNS1_13MethodWrapperIMS7_FvvEXadL_ZNS7_3runEvEES7_vJEE8dispatchESE_EESD_vJEE4callEP7_JNIEnvP8_jobject+72)
    #53 pc 000544f3  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/lib/x86/libreactnativejni.so (_ZN8facebook3jni6detail13MethodWrapperIMNS_5react15JNativeRunnableEFvvEXadL_ZNS4_3runEvEES4_vJEE4callEP7_JNIEnvP8_jobject+35)
    #54 pc 00038178  /data/app/com.reactmobile-a4bptVEODa7JqzzJAPwnLw==/oat/x86/base.odex (offset 0x37000)

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.7.0, 10.5.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Realm JS 10.7.0, I am on a Macbook Pro Big Sur, the Android emulator is failing on multiple APIs (tested on 26,29,30)

Build environment

"react-native": "0.64.1" "react-native-reanimated": "^2.2.1" "realm": "^10.7.0" "redux": "^4.1.0" "redux-logger": "^3.0.6" "redux-thunk": "^2.3.0" "react": "17.0.1" "@react-native-community/masked-view": "^0.1.11", "@react-native-community/netinfo": "^6.0.0", "@react-native-firebase/app": "^12.2.0", "@react-native-firebase/crashlytics": "^12.2.0",

kneth commented 3 years ago

Thank you for repoting it. Let us read your report once more - and digest it - before answering.

jimfoambox commented 3 years ago

Thanks @kneth for the prompt reply. I just edited the original report to include the written stack trace of the Flipper Crash Report, which I accidentally left out initially.

takameyer commented 3 years ago

@jimfoambox I'm not sure if this is related to the issue, but we have many problems being with being compatible with react-native-reanimated v2, which should clear up after we have hermes support. Can you try version 1 and see if the issue persists? See issue #3837

jimfoambox commented 3 years ago

@takameyer thank you so much for this: downgrading react-native-reanimated to version 1.13.3 worked!

The downgrade was a little finnicky, so in case anyone else is attempting this work-around these are the steps that worked for me:

1.) Npm install react-native-reanimated@1.13.3 2.) delete node_modules 3.) Npm install 4.) cd ios && pod install 5.) Added supportLibVersion = '28.0.0' to project level build.gradle in:

buildscript {
    ext {
        buildToolsVersion = "30.0.0"
        minSdkVersion = 21
        compileSdkVersion = 30
        targetSdkVersion = 30
        ndkVersion = "20.1.5948944"
        playServicesVersion = "17.0.0"
        androidMapsUtilsVersion = "2.2.5"
        supportLibVersion = '28.0.0'
    }

6.) Removed:

import com.facebook.react.bridge.JSIModulePackage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;

and

@Override
protected JSIModulePackage getJSIModulePackage() {
  return new ReanimatedJSIModulePackage(); // <- add
}

in MainApplication.java in the android directory.

These steps are a mix-and-match that I pulled from here: https://github.com/software-mansion/react-native-reanimated/issues/399 and it appears somewhat project dependent so trying some other ideas from that thread may work in case what worked for me doesn't work for someone else.

Big thanks to @kneth , @takameyer , @realm-support: very much appreciate your patience and enthusiasm to help. Very much looking forward to the new Hermes supported version of Realm!

takameyer commented 3 years ago

@jimfoambox thanks for providing the steps to downgrade! Hopefully that helps others in the future.