Skip to content

occur crash when switch DB #1202

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
1 task
udyr-woo opened this issue Jan 16, 2025 · 11 comments
Open
1 task

occur crash when switch DB #1202

udyr-woo opened this issue Jan 16, 2025 · 11 comments
Assignees

Comments

@udyr-woo
Copy link

udyr-woo commented Jan 16, 2025

Is there an existing issue?

Build info

  • ObjectBox version: 4.0.3
  • OS: Android 13
  • Device/ABI/architecture: odroid-m2 | arm64-v8a

Steps to reproduce

I used normal db and test db(both are objectBox).
When changing isTestMode of a single-toned testMode Manager, there is a logic that the dataSource changes accordingly. Before changing, the work related to the db is terminated, but for some reason, a crash occurs.

Expected behavior

Don't occur crash

Actual behavior

Occur crash

Code

Code
@HiltViewModel
class HiddenSettingsViewModel @Inject constructor(
    private val stopCcuCommUseCase: StopCcuCommUseCase,
    private val testModeManager: TestModeManager,
    private val workCcuCommUseCase: WorkCcuCommUseCase,
    private val initializeLinkTableItemsUseCase: InitializeLinkTableItemsUseCase,
    private val removeTestModeItemsUseCase: RemoveTestModeItemsUseCase,
    private val stopTestCcuCommUseCase: StopTestCcuCommUseCase,
    private val workTestCcuCommUseCase: WorkTestCcuCommUseCase,
    private val stopMakeOutputRequestUseCase: StopMakeOutputRequestUseCase,
    private val workMakeOutputRequestUseCase: WorkMakeOutputRequestUseCase
): ViewModel() {

    private val _testModeStateFlow = MutableStateFlow(
        if (testModeManager.isTestMode) TestModeState.TestMode else TestModeState.NormalMode
    )
    val testModeStateFlow: StateFlow<TestModeState> = _testModeStateFlow

    private val _isTestMode = MutableStateFlow(testModeManager.isTestMode)
    val isTestMode: StateFlow<Boolean> = _isTestMode

    fun setTestMode(enabled: Boolean) {
        viewModelScope.launch {
            _testModeStateFlow.value = TestModeState.Loading
            try {
                stopMakeOutputRequestUseCase()
                workMakeOutputRequestUseCase()
                if (enabled) {
                    when (initializeLinkTableItemsUseCase()) {
                        is LinkTableRepositoryState.Success -> {
                            stopCcuCommUseCase()
                            workTestCcuCommUseCase()
                            _testModeStateFlow.value = TestModeState.TestMode
                            testModeManager.setTestMode(true)
                            _isTestMode.value = testModeManager.isTestMode
                        }
                        is LinkTableRepositoryState.InsertError -> {
                            _testModeStateFlow.value = TestModeState.Error
                            testModeManager.setTestMode(false)
                            _isTestMode.value = testModeManager.isTestMode
                        }
                    }
                } else {
                    stopTestCcuCommUseCase()
                    removeTestModeItemsUseCase()
                    workCcuCommUseCase()
                    _testModeStateFlow.value = TestModeState.NormalMode
                    testModeManager.setTestMode(false)
                    _isTestMode.value = testModeManager.isTestMode
                }
            } catch (e: Exception) {
                _testModeStateFlow.value = TestModeState.Error
                testModeManager.setTestMode(!enabled)
                _isTestMode.value = testModeManager.isTestMode
            }
        }
    }
}

Class CcuCommRepositoryImpl @Inject constructor(
    @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
    @DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher,
    private val allBox: AllBox,
    private val repeaterBox: RepeaterBox,
    private val detectorBox: DetectorBox,
    private val serialPortManager: SerialPortManager
) : CcuCommRepository {

override fun commProcess() {
        val serialPort0 = serialPortManager.serialPort0 ?: return
        val serialPort1 = serialPortManager.serialPort1 ?: return
        val serialPort3 = serialPortManager.serialPort3 ?: return

        if (ioScope == null || ioScope?.isActive == false) {
            ioScope = CoroutineScope(ioDispatcher + SupervisorJob())
        }
        if (defaultScope == null || defaultScope?.isActive == false) {
            defaultScope = CoroutineScope(defaultDispatcher + SupervisorJob())
        }

        defaultScope?.let { scope ->
            startMakeResponseByte(scope)
            workParsing(scope, requestBufferChannels)
        }
                                    .
                                    .
                                    .
}
                   .
                   .
                   .
override fun stop() {
        ioScope?.cancel()
        defaultScope?.cancel()
        ioScope = null
        defaultScope = null
    }
}

class AllBoxImpl @Inject constructor(
    @Impermanence private val impermanenceInternalCommBox: Box<InternalCommEntity>,
    @Impermanence private val impermanenceStationCommBox: Box<StationCommEntity>,
    @Impermanence private val impermanenceStationPowerBox: Box<StationPowerEntity>,
    @Impermanence private val impermanenceCcuBox: Box<CcuEntity>,
    @Impermanence private val impermanenceDetectorBox: Box<DetectorEntity>,
    @Impermanence private val impermanenceRepeaterBox: Box<RepeaterEntity>,
    @Impermanence private val impermanenceRepeaterInputBox: Box<RepeaterInputEntity>,
    @Impermanence private val impermanenceRepeaterOutputBox: Box<RepeaterOutputEntity>,
    @TestMode private val testModeInternalCommBox: Box<InternalCommEntity>,
    @TestMode private val testModeStationCommBox: Box<StationCommEntity>,
    @TestMode private val testModeStationPowerBox: Box<StationPowerEntity>,
    @TestMode private val testModeCcuBox: Box<CcuEntity>,
    @TestMode private val testModeDetectorBox: Box<DetectorEntity>,
    @TestMode private val testModeRepeaterBox: Box<RepeaterEntity>,
    @TestMode private val testModeRepeaterInputBox: Box<RepeaterInputEntity>,
    @TestMode private val testModeRepeaterOutputBox: Box<RepeaterOutputEntity>,
    private val switchBox: Box<SwitchEntity>,
    private val switchBoardBox: Box<SwitchBoardEntity>,
    private val pumpBox: Box<PumpEntity>,
    private val pumpBoardBox: Box<PumpBoardEntity>,
    private val mccRelayBox: Box<MccRelayEntity>,
    private val broadcastRelayBox: Box<BroadcastRelayEntity>,
    private val ebcBox: Box<EbcEntity>,
    private val do24Box: Box<Do24Entity>,
    private val tbBox: Box<TbEntity>,
    @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : AllBox {

    private val internalCommQueryByInternalCommIds =
        impermanenceInternalCommBox.query().inValues(InternalCommEntity_.internalCommId, longArrayOf()).build()
    private val stationPowerQueryByStationId =
        impermanenceStationPowerBox.query().inValues(StationPowerEntity_.stationId, longArrayOf()).build()
    private val stationCommQueryByStationId =
        impermanenceStationCommBox.query().inValues(StationCommEntity_.stationId, longArrayOf()).build()
    private val ccuQueryByFullCompositeKeys = impermanenceCcuBox.query().inValues(
        CcuEntity_.fullCompositeKey,
        arrayOf(), QueryBuilder.StringOrder.CASE_INSENSITIVE
    ).build()
    private val detectorQueryByFullCompositeKeys = impermanenceDetectorBox.query().inValues(
        DetectorEntity_.fullCompositeKey,
        arrayOf(),
        QueryBuilder.StringOrder.CASE_INSENSITIVE
    ).build()
    private val repeaterQueryByFullCompositeKeys = impermanenceRepeaterBox.query().inValues(
        RepeaterEntity_.fullCompositeKey,
        arrayOf(),
        QueryBuilder.StringOrder.CASE_INSENSITIVE
    ).eager(RepeaterEntity_.inputs).eager(RepeaterEntity_.outputs).build()

    override suspend fun addAllInstances(
        internalCommInstances: List<InternalCommEntity>,
        stationCommInstances: List<StationCommEntity>,
        stationPowerInstances: List<StationPowerEntity>,
        ccuInstances: List<CcuEntity>,
        detectorInstances: List<DetectorEntity>,
        repeaterInstances: List<RepeaterEntity>,
        repeaterInputInstances: List<RepeaterInputEntity>,
        repeaterOutputInstances: List<RepeaterOutputEntity>,
    ) = withContext(ioDispatcher) {
        impermanenceInternalCommBox.store.runInTxAsync({
            impermanenceInternalCommBox.put(internalCommInstances)
            impermanenceStationCommBox.put(stationCommInstances)
            impermanenceStationPowerBox.put(stationPowerInstances)
            impermanenceCcuBox.put(ccuInstances)
            impermanenceDetectorBox.put(detectorInstances)
            impermanenceRepeaterInputBox.put(repeaterInputInstances)
            impermanenceRepeaterOutputBox.put(repeaterOutputInstances)
            impermanenceRepeaterBox.put(repeaterInstances)
        }, null)
        impermanenceInternalCommBox.store.closeThreadResources()
    }
    
    override suspend fun getAllInstances(
        internalCommIds: LongArray,
        stationIds: LongArray,
        ccuFullCompositeKeys: Array<String>,
        deviceFullCompositeKeys: Array<String>,
    ): AllInstances = withContext(ioDispatcher) {
        var result: AllInstances? = null
        impermanenceInternalCommBox.store.runInReadTx {
            val internalComms = internalCommQueryByInternalCommIds.copy()
                .setParameter(InternalCommEntity_.internalCommId, internalCommIds).find()
            val stationComms = stationCommQueryByStationId.copy()
                .setParameter(StationCommEntity_.stationId, stationIds).find()
            val stationPowers = stationPowerQueryByStationId.copy()
                .setParameter(StationPowerEntity_.stationId, stationIds).find()
            val ccus = ccuQueryByFullCompositeKeys.copy()
                .setParameter(CcuEntity_.fullCompositeKey, ccuFullCompositeKeys).find()
            val detectors = detectorQueryByFullCompositeKeys.copy()
                .setParameter(DetectorEntity_.fullCompositeKey, deviceFullCompositeKeys).find()
            val repeaters = repeaterQueryByFullCompositeKeys.copy()
                .setParameter(RepeaterEntity_.fullCompositeKey, deviceFullCompositeKeys).find()
            result =
                AllInstances(internalComms, stationComms, stationPowers, ccus, detectors, repeaters)
        }
        result ?: throw IllegalStateException("Transaction failed to return result")
    }
}

Log

Logs crash1 crash2 crash3
@udyr-woo udyr-woo added the bug Something isn't working label Jan 16, 2025
@greenrobot-team
Copy link
Member

Thanks for reporting! However, as is I can't help much. It is possible that your code does not properly close BoxStore before creating a new instance. But I can't really tell from what you have shared, for example what happens when testModeManager.setTestMode(boolean) is called.

To give you better support, please:

  • share the code that closes and builds the BoxStore,
  • post the error logs as text. Otherwise, it is impossible for me to analyze.

Note: I labeled this issue with "more info required" so it will auto-close in a few days if there are no follow-up comments.

@greenrobot-team greenrobot-team added more info required Further information is requested and removed bug Something isn't working labels Jan 20, 2025
@udyr-woo
Copy link
Author

udyr-woo commented Jan 20, 2025

  1. this app won't close the BoxStore until it shuts down.
@Module
@InstallIn(SingletonComponent::class)
object ObjectBoxModule {

    @Provides
    @Singleton
    @Impermanence
    fun provideImpermanenceObjectBox(@ApplicationContext context: Context): BoxStore {
        BoxStore.deleteAllFiles(context, "impermanenceDB")

        return MyObjectBox.builder()
            .androidContext(context.applicationContext)
            .name("impermanenceDB")  
            .build()
    }

    @Provides
    @Singleton
    @TestMode
    fun provideTestModeObjectBox(@ApplicationContext context: Context): BoxStore {
        BoxStore.deleteAllFiles(context, "testModeDB")

        return MyObjectBox.builder()
            .androidContext(context.applicationContext)
            .name("testModeDB") 
            .build()
    }
}
  1. error logs as text, like this??
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  Build fingerprint: 'hardkernel/odroidm2/odroidm2:13/TQ3C.230805.001.B2/23:userdebug/release-keys'
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  Revision: '0'
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  ABI: 'arm64'
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  Timestamp: 2024-10-20 10:59:44.298474028+0900
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  Process uptime: 16s
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  Cmdline: kr.co.leaders_tech.uitest10
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  pid: 32461, tid: 32513, name: ObjectBox-1-Thr  >>> kr.co.leaders_tech.uitest10 <<<
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  uid: 10087
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x000000775e0b4b10
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x0  b4000078a6e408f0  x1  000000776bd97d00  x2  000000775e0b4b10  x3  0000000000000008
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x4  00000077c6f1ecc8  x5  b400007966e2c7f8  x6  ea05000018000018  x7  ea05000018000018
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x8  b400007936e13ef0  x9  0000000000000024  x10 0000000018000018  x11 0000000000000020
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x12 0000007884a10858  x13 0000007884a108a0  x14 0000007884a10900  x15 0000000000000000
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x16 00000078072ebee0  x17 0000007b1c089820  x18 000000776aa12000  x19 b4000078b6e47628
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x20 b400007946e15870  x21 b400007946e05f90  x22 b400007966e2c780  x23 b4000078b6e47610
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x24 000000776bd97cd8  x25 00000000000000a9  x26 000000776bd97fb8  x27 000000776bd99000
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      x28 b4000078e6df4b50  x29 000000776bd97e00
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A      lr  00000078071a4100  sp  000000776bd97ca0  pc  000000775e0b4b10  pst 0000000060001000
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A  backtrace:
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #00 pc 0000000000021b10  [anon:dalvik-CompilerMetadata]
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #01 pc 00000000000dc0fc  /data/app/~~O--CVc3LVP8A2RqdUDTN0w==/kr.co.leaders_tech.uitest10-hcZ4ZLteBJ7XbiQ2zTgEIw==/base.apk!libobjectbox-jni.so (offset 0x11c000) (Java_io_objectbox_query_Query_nativeFind+3468) (BuildId: b6e69c8f04ba39df)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #02 pc 000000000021a354  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+148) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #03 pc 000000000020b02c  /apex/com.android.art/lib64/libart.so (nterp_helper+7468) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #04 pc 00000000002e358a  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~O--CVc3LVP8A2RqdUDTN0w==/kr.co.leaders_tech.uitest10-hcZ4ZLteBJ7XbiQ2zTgEIw==/base.apk!classes16.dex] (io.objectbox.query.Query.lambda$find$2$io-objectbox-query-Query+22)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #05 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #06 pc 00000000002e1208  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~O--CVc3LVP8A2RqdUDTN0w==/kr.co.leaders_tech.uitest10-hcZ4ZLteBJ7XbiQ2zTgEIw==/base.apk!classes16.dex] (io.objectbox.query.Query$$ExternalSyntheticLambda11.call+4)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #07 pc 00000000020924ac  /memfd:jit-cache (deleted) (offset 0x2000000) (io.objectbox.BoxStore.callInReadTx+428)
2024-10-20 10:59:44.528 32536-32536 DEBUG                   pid-32536                            A        #08 pc 000000000020a2b0  /apex/com.android.art/lib64/libart.so (nterp_helper+4016) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #09 pc 00000000002cbaa2  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~O--CVc3LVP8A2RqdUDTN0w==/kr.co.leaders_tech.uitest10-hcZ4ZLteBJ7XbiQ2zTgEIw==/base.apk!classes16.dex] (io.objectbox.BoxStore.callInReadTxWithRetry+6)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #10 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #11 pc 00000000002e334c  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~O--CVc3LVP8A2RqdUDTN0w==/kr.co.leaders_tech.uitest10-hcZ4ZLteBJ7XbiQ2zTgEIw==/base.apk!classes16.dex] (io.objectbox.query.Query.callInReadTx+20)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #12 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #13 pc 00000000002e346a  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~O--CVc3LVP8A2RqdUDTN0w==/kr.co.leaders_tech.uitest10-hcZ4ZLteBJ7XbiQ2zTgEIw==/base.apk!classes16.dex] (io.objectbox.query.Query.find+10)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #14 pc 00000000020a7bbc  /memfd:jit-cache (deleted) (offset 0x2000000) (io.objectbox.query.QueryPublisher.run+668)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #15 pc 000000000020b0d4  /apex/com.android.art/lib64/libart.so (nterp_helper+7636) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #16 pc 000000000020f9e8  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.Executors$RunnableAdapter.call+4)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #17 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #18 pc 0000000000215260  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.FutureTask.run+56)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #19 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #20 pc 0000000000222570  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.ThreadPoolExecutor.runWorker+156)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #21 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #22 pc 0000000000221374  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.ThreadPoolExecutor$Worker.run+4)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #23 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #24 pc 0000000000003090  [anon:dalvik-/apex/com.android.art/javalib/core-oj.jar-transformed] (java.lang.Thread.run+8)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #25 pc 000000000021096c  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+556) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #26 pc 000000000027975c  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+184) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #27 pc 0000000000603a10  /apex/com.android.art/lib64/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValues<art::ArtMethod*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, jvalue const*)+460) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #28 pc 000000000064ac98  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1288) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #29 pc 00000000000b63b0  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+208) (BuildId: 4e07915368c859b1910c68c84a8de75f)
2024-10-20 10:59:44.529 32536-32536 DEBUG                   pid-32536                            A        #30 pc 00000000000530b8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 4e07915368c859b1910c68c84a8de75f)

@github-actions github-actions bot removed the more info required Further information is requested label Jan 21, 2025
@greenrobot-team
Copy link
Member

Thanks for the details! I could symbolize the backtrace.

The error is signal 11 (SIGSEGV), code 2 (SEGV_ACCERR).

According to the sigaction manual SEGV_ACCERR is Invalid permissions for mapped object..

And the second to last line of the backtrace is Java_io_objectbox_query_Query_nativeFind.

Based on that: does your code make sure, that all transactions (like queries) are finished and references to all ObjectBox objects (like MyObjectBox and Box) are cleared? Note that each of these classes has a reference to a native counterpart. So it is for example not possible to use a Box instance of one BoxStore with that of another BoxStore.

For our reference, this is internal issue objectbox-java#251.

@greenrobot-team greenrobot-team self-assigned this Jan 21, 2025
@udyr-woo
Copy link
Author

udyr-woo commented Jan 21, 2025

Thank you for your kind reply, I'll try to solve it, and if it doesn't work, I'll ask you again.

@udyr-woo
Copy link
Author

udyr-woo commented Jan 31, 2025

 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  Build fingerprint: 'hardkernel/odroidm2/odroidm2:13/TQ3C.230805.001.B2/23:userdebug/release-keys'
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  Revision: '0'
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  ABI: 'arm64'
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  Timestamp: 2024-11-27 02:37:22.020705740+0000
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  Process uptime: 5s
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  Cmdline: kr.co.leaders_tech.uitest10
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  pid: 30712, tid: 30767, name: ObjectBox-1-Thr  >>> kr.co.leaders_tech.uitest10 <<<
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  uid: 10086
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0000000000000000
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  Cause: null pointer dereference
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x0  b400006f99d8e150  x1  0000006e22a55bf0  x2  0000000000000000  x3  0000000000000008
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x4  0000006ebebe87a8  x5  b400007059d77b48  x6  6004000018000018  x7  6004000018000018
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x8  0000006e34a08110  x9  0000000000000024  x10 0000000018000018  x11 000000000000001c
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x12 0000006f77e10858  x13 0000006f77e108a0  x14 0000006f77e10900  x15 0000000000000000
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x16 0000006eff10dee0  x17 000000720a2f0820  x18 0000006e11bc8000  x19 b400006fa9d94728
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x20 b400007039d609f0  x21 b400007039d4d8b0  x22 b400007059d77ad0  x23 b400006fa9d94710
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x24 0000006e22a55bc8  x25 00000000000000b5  x26 0000006e22a55ea8  x27 0000006e22a57000
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      x28 b400006fd9d52a30  x29 0000006e22a55cf0
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A      lr  0000006efefc6100  sp  0000006e22a55b90  pc  0000000000000000  pst 0000000060001000
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A  backtrace:
2024-11-27 11:37:22.255 30789-30789 DEBUG                   pid-30789                            A        #00 pc 0000000000000000  <unknown>
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #01 pc 00000000000dc0fc  /data/app/~~XzrlB8LUj9fl0evTFBRwhQ==/kr.co.leaders_tech.uitest10-HPu7gUvR6FiD2gR6AinyVg==/base.apk!libobjectbox-jni.so (offset 0x130000) (Java_io_objectbox_query_Query_nativeFind+3468) (BuildId: b6e69c8f04ba39df)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #02 pc 000000000021a354  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+148) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #03 pc 000000000020b02c  /apex/com.android.art/lib64/libart.so (nterp_helper+7468) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #04 pc 00000000002e348e  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.query.Query.lambda$find$2$io-objectbox-query-Query+22)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #05 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #06 pc 00000000002e110c  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.query.Query$$ExternalSyntheticLambda11.call+4)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #07 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #08 pc 00000000002cb8a2  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.BoxStore.callInReadTx+42)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #09 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #10 pc 00000000002cb9a6  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.BoxStore.callInReadTxWithRetry+6)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #11 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #12 pc 00000000002e3250  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.query.Query.callInReadTx+20)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #13 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #14 pc 00000000002e336e  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.query.Query.find+10)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #15 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #16 pc 00000000002e28f4  /data/data/kr.co.leaders_tech.uitest10/code_cache/.overlay/base.apk/classes16.dex (io.objectbox.query.QueryPublisher.run+108)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #17 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #18 pc 000000000020f9e8  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.Executors$RunnableAdapter.call+4)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #19 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #20 pc 0000000000215260  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.FutureTask.run+56)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #21 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #22 pc 0000000000222570  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.ThreadPoolExecutor.runWorker+156)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #23 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #24 pc 0000000000221374  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.ThreadPoolExecutor$Worker.run+4)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #25 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #26 pc 0000000000003090  [anon:dalvik-/apex/com.android.art/javalib/core-oj.jar-transformed] (java.lang.Thread.run+8)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #27 pc 000000000021096c  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+556) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #28 pc 000000000027975c  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+184) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #29 pc 0000000000603a10  /apex/com.android.art/lib64/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValues<art::ArtMethod*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, jvalue const*)+460) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #30 pc 000000000064ac98  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1288) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #31 pc 00000000000b63b0  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+208) (BuildId: 4e07915368c859b1910c68c84a8de75f)
2024-11-27 11:37:22.256 30789-30789 DEBUG                   pid-30789                            A        #32 pc 00000000000530b8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 4e07915368c859b1910c68c84a8de75f)
2024-11-27 11:37:22.310   299-299   tombstoned              tombstoned                           E  Tombstone written to: tombstone_26

Here is another issue. please check this issue.

@OptIn(ExperimentalCoroutinesApi::class)
class DeviceBoxImpl @Inject constructor(
    @Impermanence private val impermanenceDetectorBox: Box<DetectorEntity>,
    @Impermanence private val impermanenceRepeaterBox: Box<RepeaterEntity>,
    @Impermanence private val impermanenceInputBox: Box<RepeaterInputEntity>,
    @Impermanence private val impermanenceOutputBox: Box<RepeaterOutputEntity>,
    @TestMode private val testModeDetectorBox: Box<DetectorEntity>,
    @TestMode private val testModeRepeaterBox: Box<RepeaterEntity>,
    @TestMode private val testModeInputBox: Box<RepeaterInputEntity>,
    @TestMode private val testModeOutputBox: Box<RepeaterOutputEntity>,
    @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
    private val detectorRecordBox: Box<DetectorRecordEntity>,
    private val testModeManager: TestModeManager
) : DeviceBox {

    private val detectorBox: Box<DetectorEntity>
        get() = if (testModeManager.isTestMode) testModeDetectorBox else impermanenceDetectorBox

    private val repeaterBox: Box<RepeaterEntity>
        get() = if (testModeManager.isTestMode) testModeRepeaterBox else impermanenceRepeaterBox

    private val inputBox: Box<RepeaterInputEntity>
        get() = if (testModeManager.isTestMode) testModeInputBox else impermanenceInputBox

    private val outputBox: Box<RepeaterOutputEntity>
        get() = if (testModeManager.isTestMode) testModeOutputBox else impermanenceOutputBox

    override suspend fun addOutputInstance(instance: RepeaterOutputEntity) =
        withContext(ioDispatcher) {
            outputBox.put(instance)
            outputBox.closeThreadResources()
        }

    override suspend fun addOutputInstances(instances: List<RepeaterOutputEntity>) =
        withContext(ioDispatcher) {
            outputBox.put(instances)
            outputBox.closeThreadResources()
        }

    override fun observeDeviceInputInstancesByDetectorFireOn(): Flow<List<DeviceInputEntity>> {
        val query = detectorBox.query().build()

        return query.subscribe().toFlow()
            .zipWithPrevious()
            .map { (oldList, newList) ->
                if (oldList == null) {
                    emptyList()
                } else {
                    val oldMap = oldList.associateBy { it.fullCompositeKey }
                    newList.filter { newItem ->
                        val oldItem = oldMap[newItem.fullCompositeKey]
                        if (oldItem == null) {
                            false
                        } else {
                            oldItem.fire != newItem.fire || oldItem.test != newItem.test
                        }
                    }
                }
            }
            .map { inputList ->
                inputList.filter { it.fire || it.test }
                    .map { DeviceInputEntity.Detector(it) }
            }
            .filter { it.isNotEmpty() }
            .flowOn(ioDispatcher)
            .onCompletion { query.close() }
    }

    override fun observeDeviceInputInstancesByInputSignalOn(): Flow<List<DeviceInputEntity>> {
        val query = inputBox.query().build()

        return query.subscribe().toFlow()
            .zipWithPrevious()
            .map { (oldList, newList) ->
                if (oldList == null) {
                    emptyList()
                } else {
                    val oldMap = oldList.associateBy { it.fullCompositeKey }
                    newList.filter { newItem ->
                        val oldItem = oldMap[newItem.fullCompositeKey]
                        if (oldItem == null) {
                            false
                        } else {
                            oldItem.signal != newItem.signal || oldItem.test != newItem.test
                        }
                    }
                }
            }
            .map { inputList ->
                inputList.filter { it.signal || it.test }
                    .map { DeviceInputEntity.RepeaterInput(it) }
            }
            .filter { it.isNotEmpty() }
            .flowOn(ioDispatcher)
            .onCompletion { query.close() }
    }

    override fun observeOutputFullCompositeKeysByDetectorFireOff(): Flow<Array<String>> {
        val query = detectorBox.query().build()

        return query.subscribe().toFlow()
            .zipWithPrevious()
            .map { (oldList, newList) ->
                if (oldList == null) {
                    emptyList()
                } else {
                    val oldMap = oldList.associateBy { it.fullCompositeKey }
                    newList.filter { newItem ->
                        val oldItem = oldMap[newItem.fullCompositeKey]
                        if (oldItem == null) {
                            false
                        } else {
                            oldItem.fire != newItem.fire || oldItem.test != newItem.test
                        }
                    }
                }
            }
            .map { inputList ->
                inputList.filter { !it.fire && !it.test }
                    .flatMap { it.outputList }
                    .toTypedArray()
            }
            .filter { it.isNotEmpty() }
            .flowOn(ioDispatcher)
            .onCompletion { query.close() }
    }

    override fun observeOutputFullCompositeKeysByInputSignalOff(): Flow<Array<String>> {
        val query = inputBox.query().build()

        return query.subscribe().toFlow()
            .zipWithPrevious()
            .map { (oldList, newList) ->
                if (oldList == null) {
                    emptyList()
                } else {
                    val oldMap = oldList.associateBy { it.fullCompositeKey }
                    newList.filter { newItem ->
                        val oldItem = oldMap[newItem.fullCompositeKey]
                        if (oldItem == null) {
                            false
                        } else {
                            oldItem.signal != newItem.signal || oldItem.test != newItem.test
                        }
                    }
                }
            }
            .map { inputList ->
                inputList.filter { !it.signal && !it.test }
                    .flatMap { it.outputList }
                    .toTypedArray()
            }
            .filter { it.isNotEmpty() }
            .flowOn(ioDispatcher)
            .onCompletion { query.close() }
    }

    override fun observeOutputInstancesByTestOn(): Flow<List<RepeaterOutputEntity>> {
        val query = outputBox.query().build()

        return query.subscribe().toFlow()
            .zipWithPrevious()
            .map { (oldList, newList) ->
                if (oldList == null) {
                    emptyList()
                } else {
                    val oldMap = oldList.associateBy { it.fullCompositeKey }
                    newList.filter { newItem ->
                        val oldItem = oldMap[newItem.fullCompositeKey]
                        if (oldItem == null) {
                            false
                        } else {
                            oldItem.test != newItem.test
                        }
                    }
                }
            }
            .map { outputList ->
                outputList.filter { it.test }
            }
            .filter { it.isNotEmpty() }
            .flowOn(ioDispatcher)
            .onCompletion { query.close() }
    }

    override fun observeOutputFullCompositeKeysByTestOff(): Flow<Array<String>> {
        val query = outputBox.query().build()

        return query.subscribe().toFlow()
            .zipWithPrevious()
            .map { (oldList, newList) ->
                if (oldList == null) {
                    emptyList()
                } else {
                    val oldMap = oldList.associateBy { it.fullCompositeKey }
                    newList.filter { newItem ->
                        val oldItem = oldMap[newItem.fullCompositeKey]
                        if (oldItem == null) {
                            false
                        } else {
                            oldItem.test != newItem.test
                        }
                    }
                }
            }
            .map { outputList ->
                outputList.filter { !it.test }
                    .map { it.fullCompositeKey }
                    .toTypedArray()
            }
            .filter { it.isNotEmpty() }
            .flowOn(ioDispatcher)
            .onCompletion { query.close() }
    }

    override suspend fun getOutputInstancesByFullCompositeKeys(fullCompositeKeys: Array<String>): Map<String, RepeaterOutputEntity> =
        withContext(ioDispatcher) {
            val query = outputBox.query().inValues(
                RepeaterOutputEntity_.fullCompositeKey,
                fullCompositeKeys,
                QueryBuilder.StringOrder.CASE_INSENSITIVE
            ).build()

            query.use { query.find().distinctBy { it.fullCompositeKey }.associateBy { it.fullCompositeKey } }
        }

    override suspend fun getOutputInstanceByFullCompositeKey(fullCompositeKey: String): RepeaterOutputEntity? =
        withContext(ioDispatcher) {
            val query =
                outputBox.query(RepeaterOutputEntity_.fullCompositeKey.equal(fullCompositeKey))
                    .build()

            query.use { query.findUnique() }
        }

    override suspend fun areAllInputSignalsTrueByFullCompositeKeys(fullCompositeKeys: Array<String>): Boolean =
        withContext(ioDispatcher) {
            val query =
                inputBox.query()
                    .inValues(RepeaterInputEntity_.fullCompositeKey, fullCompositeKeys, QueryBuilder.StringOrder.CASE_INSENSITIVE)
                    .build()

            query.use { query.find().all { it.signal || it.test } }
        }

    private fun <T> Flow<T>.zipWithPrevious(): Flow<Pair<T?, T>> = flow {
        var previous: T? = null
        collect { current ->
            emit(previous to current)
            previous = current
        }
    }
}

class CrossCircuitBoxImpl @Inject constructor(
    private val crossCircuitBox: Box<CrossCircuitEntity>,
    @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
): CrossCircuitBox {
    override suspend fun getCrossCircuitByOutputFullCompositeKey(fullCompositeKey: String): List<CrossCircuitEntity> = withContext(ioDispatcher) {
        val query = crossCircuitBox.query(CrossCircuitEntity_.outputList.containsElement(fullCompositeKey)).build()

        query.use { query.find() }
    }

    override suspend fun getOutputFullCompositeKeysByInputFullCompositeKey(fullCompositeKey: String): List<String> = withContext(ioDispatcher) {
        val query = crossCircuitBox.query(CrossCircuitEntity_.inputList.containsElement(fullCompositeKey)).build()

        query.use { query.find().flatMap { it.outputList } }
    }
}

class DeviceRepositoryImpl @Inject constructor(
    private val deviceBox: DeviceBox,
    private val crossCircuitBox: CrossCircuitBox,
    @DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher,
) : DeviceRepository {

    private val scope = CoroutineScope(SupervisorJob() + defaultDispatcher)
    private var commJob: Job? = null

    override fun makeOutputRequest() {
        commJob = scope.launch {
            launch {
                merge(
                    deviceBox.observeDeviceInputInstancesByInputSignalOn(),
                    deviceBox.observeDeviceInputInstancesByDetectorFireOn()
                )
                    .collect { list ->
                        val inputList = list.map { it.fullCompositeKey }
                        val outputList: MutableList<String> = mutableListOf()
                        outputList.addAll(list.flatMap { it.outputList })
                        inputList.forEach { fullCompositeKey ->
                            val crossCircuitOutputList = crossCircuitBox.getOutputFullCompositeKeysByInputFullCompositeKey(fullCompositeKey)
                            if (crossCircuitOutputList.isNotEmpty()) {
                                outputList.addAll(crossCircuitOutputList)
                            }
                        }
                        val outputEntityMap = deviceBox.getOutputInstancesByFullCompositeKeys(outputList.toTypedArray())
                        outputList.forEach { fullCompositeKey ->
                            val outputEntity = outputEntityMap[fullCompositeKey]
                            if(outputEntity != null) {
                                val crossCircuitList = crossCircuitBox.getCrossCircuitByOutputFullCompositeKey(fullCompositeKey)
                                if(crossCircuitList.isNotEmpty()) {
                                    crossCircuitList.forEach { crossCircuit ->
                                        if(deviceBox.areAllInputSignalsTrueByFullCompositeKeys(crossCircuit.inputList.toTypedArray())) {
                                            if (!outputEntity.signal) {
                                                outputEntity.request = true
                                            } else {
                                                outputEntity.test = false
                                            }
                                            outputEntity.inputSignalOnCount++
                                        }
                                    }
                                } else {
                                    if (!outputEntity.signal) {
                                        outputEntity.request = true
                                    } else {
                                        outputEntity.test = false
                                    }
                                    outputEntity.inputSignalOnCount++
                                }
                            }
                        }
                        deviceBox.addOutputInstances(outputEntityMap.values.toList())
                    }
            }

            launch {
                deviceBox.observeOutputInstancesByTestOn().collect { list ->
                    list.forEach { outputEntity ->
                        if (!outputEntity.signal) outputEntity.request = true
                        outputEntity.inputSignalOnCount++
                    }
                    deviceBox.addOutputInstances(list)
                }
            }

            launch {
                merge(
                    deviceBox.observeOutputFullCompositeKeysByInputSignalOff(),
                    deviceBox.observeOutputFullCompositeKeysByTestOff(),
                    deviceBox.observeOutputFullCompositeKeysByDetectorFireOff()
                )
                    .collect { list ->
                        val outputEntityMap = deviceBox.getOutputInstancesByFullCompositeKeys(list)

                        list.forEach { fullCompositeKey ->
                            val outputEntity = outputEntityMap[fullCompositeKey]
                            if (outputEntity != null) {
                                if (outputEntity.inputSignalOnCount > 0) {
                                    outputEntity.inputSignalOnCount--
                                }
                                if (outputEntity.signal && outputEntity.inputSignalOnCount == 0) {
                                    outputEntity.request = true
                                }
                            }
                        }
                        deviceBox.addOutputInstances(outputEntityMap.values.toList())
                    }
            }
        }
    }

    override suspend fun stop() {
        commJob?.cancelAndJoin()
        commJob = null
    }
}

This is a stopable coroutine code(DeviceRepository). When it actually stops, the above error occurs. When I read or put on objectbox, should I not
stop coroutine?(and It operates before switching.)

@greenrobot-team

This comment has been minimized.

@greenrobot-team greenrobot-team added the more info required Further information is requested label Feb 10, 2025
@greenrobot-team
Copy link
Member

greenrobot-team commented Feb 10, 2025

Actually, the above should not be possible if closeThreadResources() is called on the same thread as the query is running. As both can not run at the same time. Edit: so it should be OK to use.

@udyr-woo In your code, is it possible that BoxStore is closed in another thread (scheduled from a coroutine) while a query is observed and fetching results?

@udyr-woo
Copy link
Author

Actually, the above should not be possible if closeThreadResources() is called on the same thread as the query is running. As both can not run at the same time.

@udyr-woo In your code, is it possible that BoxStore is closed in another thread (scheduled from a coroutine) while a query is observed and fetching results?

Thank you for answer. My code is working from scratch now, so I won't be able to test it right now, but I'll try not to use closeThreadResources(). And BoxStore doesn't close until the app shuts down. I just shut down Coroutin.

@github-actions github-actions bot removed the more info required Further information is requested label Feb 11, 2025
@greenrobot-team
Copy link
Member

I'll try not to use closeThreadResources()

Sorry, I was not clear enough. I made a mistake. Using closeThreadResources() is OK.

This crash should only be able to occur when the BoxStore is closed or the instance is garbage collected (finalized) while a query is busy fetching results.

@udyr-woo
Copy link
Author

udyr-woo commented Apr 3, 2025

I'll try not to use closeThreadResources()

Sorry, I was not clear enough. I made a mistake. Using closeThreadResources() is OK.

This crash should only be able to occur when the BoxStore is closed or the instance is garbage collected (finalized) while a query is busy fetching results.

2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  Build fingerprint: 'hardkernel/odroidm2/odroidm2:13/TQ3C.230805.001.B2/23:userdebug/release-keys'
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  Revision: '0'
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  ABI: 'arm64'
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  Timestamp: 2024-08-14 12:16:55.797311778+0000
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  Process uptime: 91s
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  Cmdline: kr.co.leaders_tech.uitest17
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  pid: 23512, tid: 23631, name: ObjectBox-2-Thr  >>> kr.co.leaders_tech.uitest17 <<<
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  uid: 10086
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0000000000000000
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  Cause: null pointer dereference
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x0  0000000000000000  x1  000000700e51ff70  x2  0000000000000002  x3  0000000000000008
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x4  00000070cd34ead0  x5  b4000072b775cfc8  x6  6e0ef5007c000018  x7  6e0ef5007c000018
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x8  00000070cd34ead0  x9  000000000000001c  x10 000000007c000018  x11 0000000000000034
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x12 00000071d4210858  x13 00000071d42108a0  x14 00000071d4210900  x15 0000000000000000
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x16 000000714dbf1498  x17 000000746b898820  x18 000000700dc22000  x19 b4000073477d27a0
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x20 b400007297715af0  x21 b40000729772f350  x22 b4000072b775cf50  x23 000000700e51ff48
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x24 000000000000004d  x25 000000700e5201e0  x26 0000000010380011  x27 b400007237745190
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      x28 000000700e521000  x29 000000700e520000
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A      lr  000000714da229fc  sp  000000700e51ff10  pc  000000714db100a0  pst 0000000060001000
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A  backtrace:
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #00 pc 00000000002950a0  /data/app/~~Tu2tK4F53QWl48fm2kNRpw==/kr.co.leaders_tech.uitest17-YxRXmlC4fgP36THRsox4Ew==/base.apk!libobjectbox-jni.so (offset 0x12c000) (BuildId: 5d951fc16dbbb169)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #01 pc 00000000001a79f8  /data/app/~~Tu2tK4F53QWl48fm2kNRpw==/kr.co.leaders_tech.uitest17-YxRXmlC4fgP36THRsox4Ew==/base.apk!libobjectbox-jni.so (offset 0x12c000) (Java_io_objectbox_query_Query_nativeFind+2532) (BuildId: 5d951fc16dbbb169)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #02 pc 000000000021a354  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+148) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #03 pc 00000000021fe9c4  /memfd:jit-cache (deleted) (offset 0x2000000) (io.objectbox.query.Query.lambda$find$2$io-objectbox-query-Query+212)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #04 pc 000000000020a2b0  /apex/com.android.art/lib64/libart.so (nterp_helper+4016) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #05 pc 00000000002b4658  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~Tu2tK4F53QWl48fm2kNRpw==/kr.co.leaders_tech.uitest17-YxRXmlC4fgP36THRsox4Ew==/base.apk!classes16.dex] (io.objectbox.query.Query$$ExternalSyntheticLambda11.call+4)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #06 pc 000000000203009c  /memfd:jit-cache (deleted) (offset 0x2000000) (io.objectbox.BoxStore.callInReadTx+428)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #07 pc 000000000020a2b0  /apex/com.android.art/lib64/libart.so (nterp_helper+4016) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #08 pc 000000000029eb36  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~Tu2tK4F53QWl48fm2kNRpw==/kr.co.leaders_tech.uitest17-YxRXmlC4fgP36THRsox4Ew==/base.apk!classes16.dex] (io.objectbox.BoxStore.callInReadTxWithRetry+6)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #09 pc 0000000002108798  /memfd:jit-cache (deleted) (offset 0x2000000) (io.objectbox.query.Query.callInReadTx+184)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #10 pc 000000000020a2b0  /apex/com.android.art/lib64/libart.so (nterp_helper+4016) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #11 pc 00000000002b68d2  [anon:dalvik-classes16.dex extracted in memory from /data/app/~~Tu2tK4F53QWl48fm2kNRpw==/kr.co.leaders_tech.uitest17-YxRXmlC4fgP36THRsox4Ew==/base.apk!classes16.dex] (io.objectbox.query.Query.find+10)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #12 pc 00000000020dc1bc  /memfd:jit-cache (deleted) (offset 0x2000000) (io.objectbox.query.QueryPublisher.run+668)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #13 pc 000000000020b0d4  /apex/com.android.art/lib64/libart.so (nterp_helper+7636) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #14 pc 000000000020f9e8  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.Executors$RunnableAdapter.call+4)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #15 pc 000000000212bb78  /memfd:jit-cache (deleted) (offset 0x2000000) (java.util.concurrent.FutureTask.run+376)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #16 pc 000000000020b0d4  /apex/com.android.art/lib64/libart.so (nterp_helper+7636) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #17 pc 0000000000222570  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.ThreadPoolExecutor.runWorker+156)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #18 pc 000000000020a254  /apex/com.android.art/lib64/libart.so (nterp_helper+3924) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #19 pc 0000000000221374  /apex/com.android.art/javalib/core-oj.jar (java.util.concurrent.ThreadPoolExecutor$Worker.run+4)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #20 pc 000000000020b074  /apex/com.android.art/lib64/libart.so (nterp_helper+7540) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #21 pc 0000000000003090  [anon:dalvik-/apex/com.android.art/javalib/core-oj.jar-transformed] (java.lang.Thread.run+8)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #22 pc 000000000021096c  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+556) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #23 pc 000000000027975c  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+184) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #24 pc 0000000000603a10  /apex/com.android.art/lib64/libart.so (art::JValue art::InvokeVirtualOrInterfaceWithJValues<art::ArtMethod*>(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, art::ArtMethod*, jvalue const*)+460) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #25 pc 000000000064ac98  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1288) (BuildId: 499c2699a3444f5f106778c61af92356)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #26 pc 00000000000b63b0  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+208) (BuildId: 4e07915368c859b1910c68c84a8de75f)
2024-08-14 21:16:56.046 23693-23693 DEBUG                   pid-23693                            A        #27 pc 00000000000530b8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 4e07915368c859b1910c68c84a8de75f)
2024-08-14 21:16:56.069 14976-23482 C2SoftVpxEnc            media.swcodec                        D  bytes generated 802
2024-08-14 21:16:58.045     1-1     init                    init                                 I  starting service 'simple_bugreportd'...
2024-08-14 21:16:58.046     1-1     init                    init                                 I  Created socket '/dev/socket/dumpstate', mode 660, user 2000, group 1007
2024-08-14 21:16:56.072   301-301   tombstoned              tombstoned                           E  Tombstone written to: tombstone_16
2024-08-14 21:16:56.072   553-633   RKLOG                   system_server                        D  =========generate tombstone bugreport: {0x20015f6b, 1, 1, 697740}

This is an error from a different business logic. The logic of closing the boxstore still doesn't exist. It's also hard to re-enact. What is causing this error?

@greenrobot-team
Copy link
Member

greenrobot-team commented Apr 14, 2025

The logic of closing the boxstore still doesn't exist. It's also hard to re-enact. What is causing this error?

As I previously said, this can also happen if the BoxStore instance is garbage collected (finalized) while a query is busy fetching results.

For testing, your code could maybe call System.gc() to force a garbage collection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants