Tencent/matrix · error · IllegalStateException

not initialized yet !

Error message

not initialized yet !

What it means

After passing the supervisor-role check, backgroundLruKill() also requires that the SupervisorService instance has been created (e.g. via startService/onCreate). It throws IllegalStateException("not initialized yet !") when instance is null, meaning the service was never started in the supervisor process.

Solutions

  1. Ensure SupervisorService is started in the supervisor process (context.startService) before calling backgroundLruKill.
  2. Check instance availability (or catch the exception) and start the service lazily on first use.
  3. Re-trigger service start on onServiceDisconnected/onTaskRemoved paths so it is always alive in the supervisor.
  4. Defer LRU-kill calls until after Application init where the service has certainly started.

Example fix

// before
SupervisorService.backgroundLruKill(killedCallback)
// after
context.startService(Intent(context, SupervisorService::class.java))
SupervisorService.backgroundLruKill(killedCallback)
Defensive patterns

Strategy: validation

Validate before calling

if (ProcessSupervisor.isSupervisor) {
    context.startService(Intent(context, SupervisorService::class.java))
    SupervisorService.backgroundLruKill(killedCallback)
}

Type guard

fun serviceReady(): Boolean = ProcessSupervisor.isSupervisor // ensure service started via startService beforehand

Try / catch

try {
    SupervisorService.backgroundLruKill(killedCallback)
} catch (e: IllegalStateException) {
    MatrixLog.w(TAG, "SupervisorService not started, starting now", e)
    context.startService(Intent(context, SupervisorService::class.java))
}

Prevention

When it happens

Trigger: Calling backgroundLruKill from the supervisor process before SupervisorService has been started/bound (startService(context) or equivalent init), or after the service was destroyed.

Common situations: Invoking LRU kill very early in app startup before the supervisor service starts; service killed by the system and not restarted while the caller still invokes the static method; forgetting to start the service in the supervisor process.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/d739f3ba37f58f92. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/lifecycle/supervisor/SupervisorService.kt:289

    override fun onDestroy() {
        super.onDestroy()
        MatrixLog.e(TAG, "SupervisorService destroyed!!!")
        instance = null
    }

    internal fun backgroundLruKill(
        killedCallback: (result: Int, process: String?, pid: Int) -> Unit
    ) {
        if (true != ProcessSupervisor.config?.enable) {
            MatrixLog.e(TAG, "supervisor was disabled")
            return
        }
        if (!isSupervisor) {
            throw IllegalStateException("backgroundLruKill should only be called in supervisor")
        }

        if (instance == null) {
            throw IllegalStateException("not initialized yet !")
        }

        targetKilledCallback = killedCallback
        val candidate = backgroundProcessLru.firstOrNull {
            it.name != MatrixUtil.getProcessName(this)
                    && !ProcessSupervisor.config!!.lruKillerWhiteList.contains(it.name)
        }

        if (candidate != null) {
//            DispatchReceiver.dispatchKill(this, candidate.name, candidate.pid)
            ProcessSubordinate.manager.dispatchKill(recentScene, candidate.name, candidate.pid)
        } else {
            killedCallback.invoke(LRU_KILL_NOT_FOUND, null, -1)
        }
    }

    private class TokenRecord {
        private val pidToToken: ConcurrentHashMap<Int, ProcessToken>

View on GitHub (pinned to 3b8293bd65)